Copy constructor and assignment

I have a problem with using the copy constructor and assignment operator
in main()

How do you use copy constructor in main()?
How do I use the assignment constructor in main()?

if I have :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

#include <iostream>
#include <string>
#include <cctype>
#include <conio.h>

using namespace std;
const int MAX = 20;
class Pet
{
private:
	char* name;
	int age;
	double weight;
public:
	Pet(){ name = 0; age = 0; weight = 0.0;}// default constructor
	Pet(const char*, int, double); // argument constructor
	Pet(const Pet&);// copy constructor
	~Pet();
	Pet& operator = (const Pet&);// assignment constructor
	double getWeight()const{return weight;} // Accessor
	void getInfo() const;// Accessor
	void setInfo(); // pure virtual function
	virtual void getLifespan()= 0;// pure virtual function
};
///////////////////////////////////////////////////////////////////////////////
// Class Pet declerations
Pet::Pet(const char* n, int a, double w)
{
	size_t length = strlen(n)+1;
	name = new char[length];
	strcpy_s(name, length, n);

	age = a;
	weight = w;
	
}
Pet::Pet(const Pet& otherPet)
{
	size_t length = strlen( otherPet.name )+1;
	name = new char[strlen(otherPet.name)+1];
	strcpy_s(name, length, otherPet.name);		

	age = otherPet.age;
	weight = otherPet.weight;
}
Pet::~Pet()
{
	delete[] name;
}

Pet& Pet::operator =(const Pet& otherPet)
{
	delete[]name;

	int length = strlen(otherPet.name)+1;
	name = new char[length];
	strcpy_s(name, length, otherPet.name);

	age = otherPet.age;
	weight = otherPet.weight;

	return *this;	
}

void Pet::setInfo()
{
	char ch[20] = "";
	int a;
	double w;

	cout <<"Enter the name: " ; cin >> ch;
	
	delete[] name;
	int length = strlen(ch)+1;
	name = new char[length];
	strcpy_s(name, length, ch);

	cout <<"Enter the age: "; cin >> a;
	age = a;
	cout <<"Enter the weight: "; cin >> w;
	weight = w;
}
void Pet::getInfo() const
{
	if (name != NULL)
	{
		cout <<"Pet Name:   " << name << endl;
		cout <<"Pet Age:    " << age << endl;
		cout <<"Pet Weight: " << weight << endl;
	}
	else if (name == NULL)
	{
		cout <<"Pet Name:    N/A" << endl;
		cout <<"Pet Age:    " << age << endl;
		cout <<"Pet Weight: " << weight << endl;
	}

}
void Pet::getLifespan()
{
	string s = "Uknown lifesapn ";
	cout << s << endl;
}
// Class Dog derived from Pet
class Dog : public Pet
{
private:
	string breed;
public:
	Dog(): Pet(), breed(" "){}
	Dog(char* n, int a, double w, string b):Pet(n, a, w), breed(b){}
	Dog(const Dog&);
	Dog& operator =(const Dog&);
	void setBreed();
	void getInfo()const;
	void getLifespan();
};
///////////////////////////////////////////////////////////////////////////////
// Class Dog derived from Pet declerations
Dog::Dog(const Dog& otherDog): Pet(otherDog)
{
	breed = otherDog.breed;
}
Dog& Dog::operator =(const Dog& otherDog)
{

	breed = otherDog.breed;
	Pet::operator = (otherDog);

	return *this;	
}

void Dog::setBreed()
{
	string b;

	cout <<"Enter the breed: "; cin >> b;
	breed = b;
}
void Dog::getInfo() const
{
	Pet::getInfo();
	cout <<"Pet breed: " << breed << endl;
}
void Dog::getLifespan()
{
	string s1, s2;
	double w;

	s1 = "The lifespan is under 7 years.";
	s2 = "The lifespan is 13 years.";
	
	w = Pet::getWeight();
	
	if (w > 100)
	{
		cout << s1 << endl;
	}
	else if (w < 100)
	
		cout << s2 << endl;
	else if (w == 100)
	{
		Pet::getLifespan();
	}
}

// Clas Rock derived from Pet
class Rock : public Pet
{
public:
	Rock(): Pet(){}
	Rock(char *n, int a, double w): Pet(n, a, w){}
	void getLifespan();
};

void Rock::getLifespan()
{
	string s = "Thousand  of years";

	cout << s << endl;	
}

int main() 
{
   Pet* p[10];

   // how do I use the copy constructor and assignment constructor????

  return 0;
}


Thanks in advance
Hint:
type name(arguments); //Calls a non-default constructor.

From there, the copy constructor and assignment constructor are pretty well-marked in your code, in the comments. You... shouldn't have to look below line 26 to find them.

Good luck.

-Albatross
Is this about right?
How come the breed is not passed along, only the Pet data types are?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int mian()
{
                Pet *p1[20];// Pet pointer array of 20

///////////////////////////////////////////////////////////////////////////////
	
	// create an objetc with arguments to assign to pet pointer. 
	Dog d("assignment constructor", 1, 120.00, "breed");

	// Exapmle of using the Assignmet Constructor
	p1[0] = &d; // here we assign the Dog object to pointer Pet.
	p1[0] ->getInfo();// here we display the new info
	p1[0] ->getLifespan();// display the getLife span() in regards to the weight
	cout << endl << endl;

///////////////////////////////////////////////////////////////////////////////

	// create an objetc with argument to pass as argument to pet pointer.
	Dog d2("copy constructor", 2, 100.00, "breed");
	Dog d1(d2); // we pass the dog object as an argument
	
	// Example of using the Copy Constructor
	p1[1] = &d1;// here we assign the dog object to Pet pointer
	p1[1] ->getInfo(); // display the new info
	p1[1] ->getLifespan();// display the getLife span() in regards to the weight
	cout << endl << endl;

}
I am heading to bed, brain is dead...

Thanks for the reply Albatross

I hope I did that right, the abstract class kind a confuses me a little bit.

Not beeing able to create a Pet object, and yet we can assign to Pet new information from the derived classes

Comments and advise are appreciated :)
How come the breed is not passed along, only the Pet data types are?

It's because you didn't declare getInfo() as virtual. When you're calling getInfo() now, you're always invoking Pet::getInfo().

I also don't know why Pet::getLifespan() isn't causing a compilation error because it's pure virtual yet you're supplying an implementation for it.

1
2
// Exapmle of using the Assignmet Constructor
p1[0] = &d; // here we assign the Dog object to pointer Pet. 

If you mean assignment operator then your example does not invoke it. It just does simple pointer assignment.

This will invoke the assignment operator, however:

1
2
3
Dog d("assignment constructor", 1, 120.00, "breed");
Dog anotherDog;
anotherDog = d; //invoking assignment operator 

Note how I didn't do Dog anotherDog = d; because that would invoke the copy constructor.
Last edited on
Topic archived. No new replies allowed.