Last active
December 10, 2015 23:49
-
-
Save pstachula-dev/4512256 to your computer and use it in GitHub Desktop.
Programowanie Obiektowe w4
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
#include <iostream> | |
using namespace std; | |
class B | |
{ | |
int k; | |
public: | |
friend class A; | |
B():k(10){} | |
int getK(){ return k; } | |
}; | |
class A | |
{ | |
int i; | |
B *b; | |
public: | |
A():i(20), b(new B[10]){} | |
A(int x):i(x), b(new B[10]){} | |
~A(){ delete [] b; } | |
friend ostream & operator <<(ostream &oS, A &a); | |
friend istream & operator >>(istream &iS, A &a); | |
friend bool operator ==(A &a1, A &a2); | |
A & operator =(A &a); | |
B * operator ->(); | |
void operator ()(); | |
B & operator [](unsigned int size); | |
bool operator !=(A &a1); | |
}; | |
bool A::operator !=(A &a1) | |
{ | |
if(&a1 != this) | |
return true; | |
else | |
return false; | |
} | |
bool operator==(A &a1, A &a2) | |
{ | |
if(&a1 == &a2) | |
return true; | |
else | |
return false; | |
} | |
B & A::operator [](unsigned int size) | |
{ | |
return b[size]; | |
} | |
void A::operator ()() | |
{ | |
cout << "Wartosc a.i = " << i << endl; | |
} | |
B * A::operator ->() | |
{ | |
if(b == NULL) | |
cout << "Blad wskaznik zerowy!\n"; | |
return b; | |
} | |
A & A::operator =(A& a) | |
{ | |
if(this != &a) { | |
delete b; | |
b = new B; | |
b->k = a.b->k; | |
i = a.i; | |
} | |
return *this; | |
} | |
ostream & operator <<(ostream &oS, A &a) | |
{ | |
oS << a.i << endl; | |
return oS; | |
} | |
istream & operator >>(istream &iS, A &a) | |
{ | |
iS >> a.i; | |
return iS; | |
} | |
int main() | |
{ | |
A a, b, c(90); | |
cout << b[2].getK() << endl; | |
cout << b->getK() << endl; | |
a(); | |
a = b = c; | |
cin >> a; | |
cout << a; | |
if(a == c) | |
cout << "Prawda sa rowne a = c\n"; | |
else | |
cout<< "Falsz a != c\n"; | |
if(a != c) | |
cout << "Prawda sa nierowne a != c\n"; | |
else | |
cout<< "Falsz sa rowne a = c\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment