Kod: Zaznacz cały
#include<iostream>
#include<string>
using namespace std;
class Animal{
private:
string itsName;
int itsAge;
int *pAge;
public:
Animal(string name = 0 ,int age = 0,int page = 0)
:itsName(name),itsAge(age), pAge(new int(page)){
cout<<"Name : ";
cin>>name;
cout<<"itsAge : ";
cin>>age;
cout<<"pAge : ";
cin>>page;
cout<<"-------
";
}
~Animal();
Animal(const Animal& rhs){
pAge = new int;
*pAge = *(rhs.pAge); // ok
itsName = rhs.itsName;
itsAge = rhs.itsAge;
cerr<<"Konstruktor kopiujacy
";
}
Animal &operator=(const Animal&);
/* friend ostream& operator<<(ostream&, Animal&); */
void fnPrint();
};
Animal::~Animal()
{
if(pAge){
delete pAge;
pAge = 0; }
cerr<<"Destruktor
";
}
Animal& Animal::operator =(const Animal & rhs)
{
if(&rhs !=this)
{
pAge = new int;
pAge = rhs.pAge;
itsName = rhs.itsName;
itsAge = rhs.itsAge;
}
return *this;
}
void Animal::fnPrint()
{
cout<<"itsName : "<<itsName<<endl;
cout<<"itsAge : "<<itsAge<<endl;
cout<<"pAge : "<<*pAge<<endl;
cout<<"---fnPrint()---"<<endl;
}
int main()
{
Animal dog;
Animal cat;
cout<<"----------------------"<<endl;
dog.fnPrint();
cat.fnPrint();
cout<<"----------------------"<<endl;
dog = cat;
dog.fnPrint();
system("pause");
return 0;
}
EDIT : juz wiem jaki blad, dziwna deklaracja konstruktora, usuniecie listy incjalizacyjnej i wpisanie w ciele konstr. incjalizacji string name i int age, pomoglo
hmmm dziwnie mysalem ze tak mozna pisac ...