Created
November 14, 2015 17:35
-
-
Save elwin013/466898b134fcf101c5f7 to your computer and use it in GitHub Desktop.
c0ffee.pl - Przeciążanie operatorów w C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Prosty przykład przeciążania operatorów w C++ | |
* Wymagamy dostępu do składowej prywatnej. | |
* | |
* Kamil 'elwin013' Banach. | |
* http://c0ffee.pl | |
* | |
*/ | |
#include <iostream> | |
using namespace std; | |
/* | |
* Tworzymy sobie klasę MyInt, która tak naprawdę | |
* nie ma nic magicznego - posiada tylko pole "val" | |
* z wartością. | |
*/ | |
class MyInt { | |
private: | |
/* Pole val */ | |
int val; | |
public: | |
/* Konstruktory */ | |
MyInt(); | |
MyInt(int x); | |
friend ostream& operator<<(ostream &out, const MyInt& myint); | |
/* Przeciążenie operatora + */ | |
MyInt operator+(const MyInt operand); | |
}; | |
/* Definicje konstruktorów */ | |
MyInt::MyInt(){ | |
val = 0; | |
} | |
MyInt::MyInt(int x) { | |
val = x; | |
} | |
/* | |
* Definicja przeciążenia operatora + | |
* Musi być składnikiem klasy. Pierwszy operand to this, | |
* natomiast drugi przekazujemy! | |
*/ | |
MyInt MyInt::operator+(const MyInt operand) { | |
MyInt wynik = *this; | |
wynik.val = operand.val + wynik.val; | |
return wynik; | |
} | |
/* | |
* Przeciążenie operatora << (wypisywanie na wyjście) | |
* Nie może być ono METODĄ KLASY, musi być poza nią! | |
* Przekazujemy dwa operandy - wyjście oraz wskaźnik na zmienną | |
* Patrz też: http://stackoverflow.com/questions/10744787/ | |
*/ | |
ostream& operator<<(ostream &out, const MyInt& myint) { | |
return out << "Wartosc: " << myint.val; | |
} | |
main() { | |
MyInt one(1), two(2); | |
cout << one << endl; | |
cout << two << endl; | |
one = one + two; | |
cout << one << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Prosty przykład przeciążania operatorów w C++ | |
* Wymagamy dostępu tylko do składowych publicznych. | |
* | |
* Kamil 'elwin013' Banach. | |
* http://c0ffee.pl | |
* | |
*/ | |
#include <iostream> | |
using namespace std; | |
/* | |
* Tworzymy sobie klasę MyInt, która tak naprawdę | |
* nie ma nic magicznego - posiada tylko pole "val" | |
* z wartością. | |
*/ | |
class MyInt { | |
public: | |
/* Pole val */ | |
int val; | |
/* Konstruktory */ | |
MyInt(); | |
MyInt(int x); | |
/* Przeciążenie operatora + */ | |
MyInt operator+(const MyInt operand); | |
}; | |
/* Definicje konstruktorów */ | |
MyInt::MyInt(){ | |
val = 0; | |
} | |
MyInt::MyInt(int x) { | |
val = x; | |
} | |
/* | |
* Definicja przeciążenia operatora + | |
* Musi być składnikiem klasy. Pierwszy operand to this, | |
* natomiast drugi przekazujemy! | |
*/ | |
MyInt MyInt::operator+(const MyInt operand) { | |
MyInt wynik = *this; | |
wynik.val = operand.val + wynik.val; | |
return wynik; | |
} | |
/* | |
* Przeciążenie operatora << (wypisywanie na wyjście) | |
* Nie może być ono METODĄ KLASY, musi być poza nią! | |
* Przekazujemy dwa operandy - wyjście oraz wskaźnik na zmienną | |
* Patrz też: http://stackoverflow.com/questions/10744787/ | |
*/ | |
ostream& operator<<(ostream &out, const MyInt& myint) { | |
return out << "Wartosc: " << myint.val; | |
} | |
main() { | |
MyInt one(1), two(2); | |
cout << one << endl; | |
cout << two << endl; | |
one = one + two; | |
cout << one << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment