Guide
User Manual:
Open the PDF directly: View PDF .
Page Count: 7
lab_inheritance
Insidious Inheritance
!"#$%&#'%()*%+,-./%01
!2345#6 78'%986:2";
Assignment Description
<6%;9=>%?8'%42"%@=??%5#;%#3A#B=#6C#%@=;9%>2D#%2E%;9#%=DA?#D#6;8;=26%=>>"#>%86:%C26C#A;"8?%:#;8=?>%2E
=69#B=;86C#F%<69#B=;86C#%=>%8%D#C986=>D%E2B%=6CB#8>=65%;9#%B#">8'=?=;4%86:%B#?=8'=?=;4%2E%GHH%C2:#F%<;%=>%@2B;9
D#6;=26=65%;98;%=69#B=;86C#%=>%8%C98B8C;#B=>;=C%2E%8??%2'I#C;%2B=#6;#:%AB25B8DD=65%?865"85#>F%J"B%528?%=>%;2
5=K#%42"%8%5?=DA>#%2E%;9#%E"6C;=268?=;4%2E%=69#B=;86C#*%>2%;98;%42"%C86%D8L#%=6E2BD#:%:#>=56%:#C=>=26>%=6%;9#
E";"B#F%0?#8>#%B#8:%;9B2"59%;9#%#6;=B#%?8'%'#E2B#%42"%'#5=6F
Lab Insight
M9=>%?8'%;#8C9#>%>2D#%2E%;9#%C98B8C;#B=>;=C>%2'>#BK#:%=6%2'I#C;%2B=#6;#:%AB25B8DD=65F%M9#>#%C98B8C;#B=>;=C>
8B#%">#E"?%=6%>2E;@8B#%:#>=56%@9#6%'"=?:=65%C2:#%'8>#>%E2B%?8B5#%AB2I#C;>*%N0<>*%86:%AB2:"C;=26%C2:#%'8>#>F
O2D#%C?8>>#>%;98;%E"B;9#B%#3;#6:%26%;9#>#%C26C#A;>%=6C?":#%GO%P+Q*%GO%+P+*%86:%GO%P++F%M9=>%?8'%@=??%9#?A
42"%'"=?:%C?#86%C2:#%'8>#>%@9#B#%;9#%JJ0%RJ'I#C;SJB=#6;#:%0B25B8DD=65T%C98B8C;#B=>;=C>%9#?A%D=6=D=U#
98K=65%;2%B#@B=;#%B#:"6:86;%C2:#%8>%@#??%8>%D8L#%">#%2E%K=B;"8?%=69#B=;86C#%;2%;B#8;%C">;2D%>"'SC?8>>#>%;9#
>8D#%@84%8>%;9#=B%'8>#%C?8>>#>%@=;92";%?2>=65%;9#%C">;2D%E"6C;=268?=;4%2E%;9#>#%>"'C?8>>#>F
Getting Set Up
&B2D%42"B%GO%++.%5=;%:=B#C;2B4*%B"6%;9#%E2??2@=65%26%VWO$
X
git fetch release
git merge release/lab_inheritance -m "Merging initial lab_inheritance files"
<E%42"YB#%26%42"B%2@6%D8C9=6#*%42"%D84%6##:%;2%B"6$
X
git fetch release
git merge --allow-unrelated-histories release/lab_inheritance -m "Merging initial
lab_inheritance files"
Assignment
Description
Lab Insight
Getting Set Up
Class
Hierarchy
Exercise 1: Fix
the Virtual
Methods
Exercise 2: Fix
the Destructor
Exercise 3: Fix
the
Constructor
Exercise 4: Fix
the Pure
Virtual Method
Exercise 5: Fix
the Slicing
Testing Your
Code
Cleaning up
files
Submitting
Your Work
ZA26%8%>"CC#>>E"?%D#B5#*%42"B%?8'[=69#B=;86C#%E=?#>%8B#%62@%=6%42"B%lab_inheritance%:=B#C;2B4F
M9#%C2:#%E2B%;9=>%8C;=K=;4%B#>=:#>%=6%;9#%lab_inheritance/%:=B#C;2B4F%\#;%;9#B#%'4%;4A=65%;9=>%=6%42"B%@2BL=65
:=B#C;2B4$
X
cd lab_inheritance/
Class Hierarchy
M2%9#?A%">%"6:#B>;86:%C?8>>%9=#B8BC9=#>%'#;;#B%9#B#%=>%86%#38DA?#%2E%8%>=DA?#%C?8>>%9=#B8BC94%>92@=65%;98;%8
Dog%is a%AnimalF
M9#%C2:#%@2"?:%?22L%>2D#;9=65%?=L#%;9#%E2??2@=65$
class Animal {
public:
string name;
virtual void speak() = 0;
/* The = 0 at the end of the method means that the method is a pure virtual method
* meaning that it does not have an implementation and it delegates the task
* of implementing the method to the classes that is derived from it */
};
class Dog : public Animal {
public:
string breed;
/* Dog inherits speak from Animal */
void speak();
};
void Dog::speak() {
cout << "Woof Woof" << endl;
}
<6%;9=>%#38DA?#%Animals%98K#%8%name%86:%C86%speak%'";%>=6C#%speak%=>%8%A"B#%K=B;"8?%D#;92:%@#%GN]]JM
C26>;B"C;%86%Animal%'4%=;>#?EF%M98;%=>%Animal%=>%86%8'>;B8C;%C?8>>%86:%=;%C86%26?4%'#%C26>;B"C;#:%'4%26#%2E%=;>
:#B=K#:%C?8>>#>F%&2B%#38DA?#*%8%Dog%=>%8%:#B=K#:%C?8>>%2E%N6=D8?F%M9=>%D#86>%;98;%8%!25%is a%Animal*%86:*
;9#B#E2B#*%=;%=69#B=;>%8%name%86:%8%speak%D#;92:%EB2D%AnimalF%^2@#K#B*%>=6C#%;9#%AnimalY>%speak%:2#>%62;
98K#%86%=DA?#D#6;8;=26*%Dog%must%=DA?#D#6;%;9#%speak%D#;92:F
!%_2"%@=??%26?4%6##:%;2%D2:=E4%;9#%E2??2@=65%E=?#>$
shape.{cpp,h}
circle.{cpp,h}
truck.{cpp,h}
flower.{cpp,h}
drawable.h
^#B#%=>%86%#38DA?#%2E%92@%@#%C2"?:%">#%8%Dog%2'I#C;$
Dog* d = new Dog();
/* Like usual we can access all the public methods and member variables of a
* Dog */
d->breed;
/* But now since a Dog is an Animal we can also do this too */
d->name; // inherited from Animal
d->speak(); // inherited from Animal and since it is a Dog speak() will print
// "Woof Woof"
/* Additionally we can treat our Dog only like an Animal like this */
Animal* a = d;
/* But now we can only do the following */
a->name;
a->speak(); // Still prints "Woof Woof" because speak is a virtual method.
a->breed // ERROR! This will NOT work since we perceive it as an Animal now
/* Additionally, if we try to have our Animal pointer point back to a Dog
* pointer this will cause a problem because an Animal Is NOT A Dog. */
Dog* d2 = a; // ERROR! Animal Is NOT A Dog
/* Furthermore, since Animal is abstract and has a pure virtual method
* we CANNOT construct one! */
Animal a2; // ERROR! Animal is an abstract class
]2@%;98;%@#%C86%"6:#B>;86:%8%>=DA?#%C?8>>%9=#B8BC94*%?#;Y>%?22L%8;%8%D2B#%C2DA?#3%26#F%^#B#%=>%8%:=85B8D
:#A=C;=65%;9#%C?8>>%9=#B8BC94%;98;%=>%">#:%=6%;9=>%?8'F%R]2;#$%M9=>%:=85B8D%=>%D=>>=65%>2D#%=6E2BD8;=26*%#F5F
D#;92:>*%D#D'#B%K8B=8'?#>*%#;CFF*%E2B%:#D26>;B8;=26%A"BA2>#>T
M9=>%D#86>%#K#B4;9=65%=>%8%Drawable%86:%@=??%98K#%8%draw%D#;92:F%G2:#%?=L#%;9#%E2??2@=65%=>%A#BE#C;?4
8CC#A;8'?#$
Drawable* triangle = new Triangle(....);
Drawable* circle = new Circle(...);
Drawable* rectangle = new Rectangle(....);
Drawable* truck = new Truck(...);
Drawable* flower = new Flower(....);
/* Now the only thing we can use on triangle, circle, rectangle, truck, and
* flower is draw but what gets drawn will change depending on what type the
* pointer is actually pointing to. This is called polymorphism, the behavior
* changes depending on the actual type of the object being pointed to. */
PNG canvas;
triangle->draw(&canvas); // draws a Triangle even though triangle is a Drawable*
circle->draw(&canvas); // draws a Circle even though circle is a Drawable*
rectangle->draw(&canvas); // draws a Rectangle even though rectangle is a Drawable*
truck->draw(&canvas); // draws a Truck even though truck is a Drawable*
flower->draw(&canvas); // draws a Flower even though flower is a Drawable*
722L%8;%main.cpp%E2B%8%@2BL=65%#38DA?#%#3#C";8'?#%=6%8C;=26F%main.cpp%5#;>%C2DA=?#:%86:%?=6L#:%=6;2%86
#3#C";8'?#%68D#:%lab_inheritanceF%&2??2@%;9#%=6>;B"C;=26>%'#?2@%;2%'"=?:*%B"6*%86:%K=#@%;9#%2";A";$
M9#%Makefile%AB2K=:#:%E2B%;9=>%10%@=??%CB#8;#%86%#3#C";8'?#%@9#6%42"%B"6%makeF%<;%@=??%5#6#B8;#
lab_inheritanceF
&2B%#38DA?#%@9#6%42"%B"6
X
./lab_inheritance
_2"%C2"?:%8?>2%B"6%Valgrind%26%;9#%62BD8?%#3#C";8'?#$
X
valgrind --leak-check=full ./lab_inheritance
M9=>%?8'%@=??%">#%8??%2E%;9#>#%2'I#C;>%=6%=6;#B#>;=65%@84>%'";%8>%42"%@=??%`"=CL?4%>##%;9#4%8B#%62;%@2BL=65%;9#
@84%;9#%>92"?:F%_2"B%2'I#C;=K#%E2B%;9=>%?8'%=>%;2%52%;9B2"59%;9#%.%;#>;%#3#C";8'?#>%86:%E=3%;9#%C2:#%;2%@2BL
C2BB#C;?4%'4%D2:=E4=65%92@%;9#%C?8>>#>%=6%;9#%9=#B8BC94%:#C?8B#%86:%=DA?#D#6;%;9#=B%D#;92:>F
J6C#%42"%98K#%E=3#:%8??%;9#%a8?5B=6:%#BB2B>*%42"%C86%;#>;%42"B%AB25B8D%2";A";%E2??2@=65%;9#%:=B#C;=26>%'#?2@F
Exercise 1: Fix the Virtual Methods
0?#8>#%'"=?:%86:%B"6%test_virtual$
X
make test_virtual # compile to produce test_virtual executeble
valgrind ./test_virtual # run test_virtual with valgrind
N>%42"%@=??%>##%@9#6%42"%B"6%test_virtual*%;9#%2";A";%@=??%>84$
The Perimeters are NOT the same.
The Areas are NOT the same.
^2@#K#B*%=E%42"%?22L%C?2>#?4%8;%;9#%C2:#%;9#4%>92"?:%'#%;9#%>8D#%'#C8">#%'2;9%2E%;9#%A2=6;#B>%=6
test_virtual.cpp%A2=6;%;2%;9#%>8D#%2'I#C;b
Exercise 2: Fix the Destructor
0?#8>#%'"=?:%86:%B"6%test_destructor$
!%Exercise
<6K#>;=58;#%86:%E=3%;9#%C2:#%>2%;98;%;9#%8B#8>%86:%;9#%A#B=D#;#B>%8B#%;9#%>8D#F
M2%E=3%;9=>%AB2'?#D%42"%>92"?:%26?4%6##:%;2%D2:=E4%shape.cpp%86:c2B%shape.hF
X
make test_destructor # compile to produce test_destructor
executeble
valgrind --leak-check=full ./test_destructor # run test_destructor in valgrind
W9#6%42"%B"6%test_destructor%=6%a8?5B=6:%42"%@=??%>##%;98;%test_destructor%=>%?#8L=65%D#D2B4F%^2@#K#B*%=E
42"%?22L%C?2>#?4*%Triangle%:2#>%98K#%8%K8?=:%:#>;B"C;2B%86:%=;%=>%'#=65%C8??#:%=6%test_destructorb
Exercise 3: Fix the Constructor
0?#8>#%'"=?:%86:%B"6%test_constructor$
X
make test_constructor # make test_constructor
./test_constructor # run test_constructor
W9#6%42"%B"6%test_constructor%42"%@=??%>##%;9#%E2??2@=65%2";A";$
Circle's color is NOT correct!
Circle's center is NOT correct!
<E%42"%?22L%C?2>#?4*%@#%8B#%C26>;B"C;=65%8%Circle%@=;9%8%K8?=:%C#6;#B%86:%C2?2BF%^2@#K#B*%@9#6%=;%=>%'#=65%:B8@6
86:%@9#6%@#%8>L%E2B%;9#%CircleY>%C#6;#B%86:%C2?2B%;9#4%8B#%62;%;9#%>8D#b
Exercise 4: Fix the Pure Virtual Method
0?#8>#%'"=?:%86:%B"6%test_pure_virtualF
X
!%Exercise
<6K#>;=58;#%86:%E=3%;9#%C2:#%>2%;98;%;9#%;9#B#%=>%62%D2B#%D#D2B4%?#8L%=6>=:#%2E%test_destructorF
M2%E=3%;9=>%AB2'?#D%42"%>92"?:%26?4%6##:%;2%D2:=E4%drawable.h%86:%shape.hF
!%Exercise
<6K#>;=58;#%86:%E=3%;9#%C2:#%>2%;98;%;9#%Circle%=>%'#=65%C26>;B"C;#:%@=;9%;9#%AB2A#B%C#6;#B%86:
C2?2BF
M2%E=3%;9=>%AB2'?#D%42"%>92"?:%26?4%6##:%;2%D2:=E4%circle.cpp
M9#%C2BB#C;%test_constructor.png%>92"?:%?22L%?=L#%;9#%E2??2@=65$
make test_pure_virtual # make test_pure_virtual
./test_pure_virtual # run test_pure_virtual
W9#6%42"%;B4%;2%D8L#%test_pure_virtual%42"%@=??%>##%;98;%=;%:2#>%62;%C2DA=?#F
^2@#K#B*%=E%42"%?22L%8;%;9#%truck.{h,cpp}%=;%=>%8%E"??4%E#8;"B#:%C?8>>b%W94%=>%=;%62;%C2DA=?=65d
Exercise 5: Fix the Slicing
0?#8>#%'"=?:%86:%B"6%test_slicing%@=;9$
X
make test_slicing # make test_slicing
./test_slicing # run test_slicing
NE;#B%42"%B"6%test_slicing%2A#6%"A%=;>%2";A";%test_slicing.pngF%_2"%@=??%>##%;98;%8%Flower%98>%]JM%'##6
:B8@6F%&2B%>2D#%B#8>26%I">;%8%'"6C9%2E%eY>%98>%'##6%:B8@6%86:%8%B#:%C=BC?#F
<E%42"%?22L%8;%flower.h%86:%flower.cpp*%@#%98K#%8??%2E%;9#%AB2A#B%D#D'#B%K8B=8'?#>%>#;%"AF%^2@#K#B*%@9#6
@#%;B4%;2%:B8@%;9#D%;9#4%8B#%:B8@6%=6C2BB#C;?4F
Testing Your Code
f"6%;9#%G8;C9%;#>;>%8>%E2??2@>%R;9=>%B#`"=B#>%42"B%C2:#%;2%C2DA=?#%@9#6%B"6%>=DA?4%8>%makeT$
!%Exercise
<6K#>;=58;#%86:%E=3%;9#%C2:#%>2%;98;%test_pure_virtual%C2DA=?#>*%B"6>*%86:%2";A";>%8%TruckF
M2%E=3%;9=>%AB2'?#D%42"%>92"?:%26?4%6##:%;2%D2:=E4%truck.h%86:%truck.cppF
<6%2B:#B%;2%98K#%;9#%Truck%:B8@%AB2A#B?4%42"%@=??%E=B>;%6##:%;2%98K#%V3#BC=>#%,%C2DA?#;#:F
M9#%C2BB#C;%test_pure_virtual.png%>92"?:%?22L%?=L#%;9#%E2??2@=65$
!%Exercise
<6K#>;=58;#%86:%E=3%;9#%C2:#%>2%;98;%test_slicing%2";A";>%8%FlowerF
M2%E=3%;9=>%AB2'?#D%42"%>92"?:%26?4%6##:%;2%D2:=E4%flower.h%86:%flower.cppF
_2"%D">;%">#%A2?4D2BA9=>Db
M9#%C2BB#C;%test_slicing.png%2";A";%>92"?:%?22L%?=L#%;9#%E2??2@=65$
X
make test
./test
Cleaning up files
M2%C?#86%"A%42"B%@2BL=65%B#A2>=;2B4%86:%B#D2K#%;9#%;#>;%=D85#>%AB2:"C#:%'4%42"B%AB25B8D*%42"%C86%;4A#%;9#
E2??2@=65%C2DD86:$
X
make clean
Submitting Your Work
M9#%E2??2@=65%E=?#>%8B#%">#:%;2%5B8:#%;9=>%8>>=56D#6;$
shape.cpp
shape.h
circle.cpp
circle.h
truck.cpp
truck.h
flower.cpp
flower.h
drawable.h
N??%2;9#B%E=?#>%=6C?":=65%864%;#>;=65%E=?#>%42"%98K#%8::#:%@=??%62;%'#%">#:%E2B%5B8:=65F
"%\"=:#$%^2@%;2%>"'D=;%GO%++.%@2BL%">=65%5=;