-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathItem12.cpp
More file actions
87 lines (67 loc) · 1.51 KB
/
Item12.cpp
File metadata and controls
87 lines (67 loc) · 1.51 KB
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
// Item 12: Make sure that you copy all parts of an object
#include <iostream>
#include <string>
using namespace::std;
class Animal
{
public:
Animal ( int aAge = 0 );
Animal ( const Animal& rhs );
Animal& operator = ( const Animal& rhs );
int getAge ( ) {return age;}
private:
int age;
};
Animal::Animal ( int aAge ): age ( aAge ) {}
Animal::Animal ( const Animal& rhs ):
age ( rhs.age )
{}
Animal& Animal::operator = ( const Animal& rhs )
{
age = rhs.age;
return *this;
}
class Dog : public Animal
{
public:
Dog ( int aAge=0, string aBreed='\0' );
Dog ( const Dog& rhs );
Dog& operator= ( const Dog& rhs );
void ToString ( );
private:
string breed;
};
Dog::Dog ( int aAge, string aBreed ): Animal(aAge),breed(aBreed) {}
// Call to base class copy constructor intentionally commented to demonstrate the error
Dog::Dog ( const Dog& rhs ):
// Animal ( rhs ),
breed(rhs.breed)
{
}
void Dog::ToString ( )
{
cout << "Age: " << getAge() << "; Breed: " << breed << endl;
}
// Call to base class operator= intentionally commented to demonstrate the error
Dog& Dog::operator= ( const Dog& rhs )
{
// Animal::operator=(rhs);
breed = rhs.breed;
return *this;
}
int main()
{
Dog d1(10,"Bull Dog");
cout << "Dog d: " << endl;
d1.ToString();
// Age should be 10 but remains zero
Dog d2(d1);
cout << endl << "Dog d2: " << endl;
d2.ToString();
Dog d ( 13, "Alsasian");
Dog d3 ( 10, "Labrador" );
// Age should become 13 but remains 10
d3 = d;
cout << endl << "Dog d3: " << endl;
d3.ToString();
}