Need Help With Copy Constructor
Posted: Mon Dec 09, 2002 12:00 am
Can someone help me understand a copy constructor? I am trying to write an example for myself, so I can understand it. What am I doing wrong here?
#include <iostream>
using namespace std;
class Tree
{
public:
Tree();
int setAge(int);
Tree(const Tree&);
void printAge();
private:
int* age;
};
Tree::Tree()
{
*age=30;
}
int setAge(int age)
{
cout << "Please enter the tree's age...\n";
cin >> age;
return age;
}
Tree::Tree(const Tree& firstTree)
{
age = new int;
*age=*(firstTree.age);
}
void printAge()
{
cout << age;
}
int main()
{
Tree first;
first.setAge(30);
first.printAge();
getchar();
return 0;
}
#include <iostream>
using namespace std;
class Tree
{
public:
Tree();
int setAge(int);
Tree(const Tree&);
void printAge();
private:
int* age;
};
Tree::Tree()
{
*age=30;
}
int setAge(int age)
{
cout << "Please enter the tree's age...\n";
cin >> age;
return age;
}
Tree::Tree(const Tree& firstTree)
{
age = new int;
*age=*(firstTree.age);
}
void printAge()
{
cout << age;
}
int main()
{
Tree first;
first.setAge(30);
first.printAge();
getchar();
return 0;
}