Skip to content

Instantly share code, notes, and snippets.

@elwin013
Created November 14, 2015 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elwin013/466898b134fcf101c5f7 to your computer and use it in GitHub Desktop.
Save elwin013/466898b134fcf101c5f7 to your computer and use it in GitHub Desktop.
c0ffee.pl - Przeciążanie operatorów w C++
/*
* 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;
}
/*
* 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