|
-
September 6th, 2005, 06:10 AM
#1
copy constructor??
Can anyone see what's wrong with this , the print in the Copy Constructor never appears on the output, should it be called when "=" is used in main
#include <iostream.h>
class SimpleCat
{
public:
SimpleCat (int age, int weight);
~SimpleCat() {}
SimpleCat operator = (SimpleCat&);
int GetAge() { return itsAge; }
int GetWeight() { return itsWeight; }
public:
int itsAge;
int itsWeight;
};
SimpleCat::SimpleCat(int age, int weight)
{
itsAge = age;
itsWeight = weight;
}
SimpleCat SimpleCat: perator= (SimpleCat& cat)
{
cout << "Copy constructor called\n";
return SimpleCat(cat.itsAge, cat.itsWeight);
}
int main()
{
SimpleCat Frisky(5, 8);
SimpleCat Cat = Frisky; //Call copy cnstr
SimpleCat& rCat = Frisky; //Call copy cnstr
cout << "Frisky is: ";
cout << Frisky.GetAge() << " years old. \n";
cout << "rCat is " << rCat.GetAge() << " years old. \n";
cout << "And Frisky weighs: ";
cout << Frisky.GetWeight() << " pounds. \n";
cout << "And rCat weighs: ";
cout << rCat.GetWeight() << " pounds. \n\n";
//Change the references values and print original
rCat.itsAge = 25;
rCat.itsWeight = 99;
cout << "Frisky is: ";
cout << Frisky.GetAge() << " years old. \n";
cout << "rCat is " << rCat.GetAge() << " years old. \n";
cout << "And Frisky weighs: ";
cout << Frisky.GetWeight() << " pounds. \n";
cout << "And rCat weighs: ";
cout << rCat.GetWeight() << " pounds. \n";
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|