-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathabstract_class.cpp
More file actions
104 lines (91 loc) · 3.41 KB
/
abstract_class.cpp
File metadata and controls
104 lines (91 loc) · 3.41 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*******************************************************************************
*
* Program: Abstract Class With Pure Virtual Function Example
*
* Description: Example of how to use a pure virtual function to create an
* abstract class in C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=wE0_F4LpGVc
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
using namespace std;
// Shape is a base class for Square and Triangle, and we want to be able to
// use polymorphism to work with arrays of pointers to Square, Triangle and
// other object instances so that we can execute member functions like
// area() to calculate the area of shapes. The problem is that there is NO
// "generic shape area", the area calculation is really only defined for
// specific types of shapes. So instead we create a pure virtual function
// area() in our base class shape, which makes the Shape class an abstract
// class (having 1 or more pure virtual member function makes any class a
// an abstrac class). We then leave it to Square, Triangle and any other
// derived class to override the area member function (if they do not, they
// will also become abstract classes). Notably, we cannot create an instance
// of an abstract class like Shape, but we CAN have pointers and references
// of the type of an abstract class.
//
class Shape
{
public:
// The virtual keyword will make the area() function a virtual function that
// allows for dynamic binding (i.e. runtime polymorphism). The = 0 assignment
// will make it a pure virtual function, and makes Shape an abstract class.
virtual double area() = 0;
};
// Square inherits from Shape, and overrides the area pure virtual function.
// Because Square overrides all of the pure virtual functions of its base
// class, we can create Square object instances, and we would call Square
// a concrete class.
class Square : public Shape
{
public:
double side;
Square(double side) : side(side) {}
double area()
{
return side * side;
}
};
// Triangle also inherits from Shape, and also overrides the area pure virtual
// function with a calculation that works for triangles.
class Triangle : public Shape
{
public:
double base;
double height;
Triangle(double base, double height) :
base(base), height(height) {}
double area()
{
return 0.5 * base * height;
}
};
int main()
{
// We *cannot* make a Shape object instance because it is an abstract class.
// So the below code will cause a compiler error if we uncomment it.
//
// Shape shape;
// We CAN create pointers and references of the type of an abstract class,
// allowing us to create an array of pointers to object instances of
// derived classes of Shape.
Shape *shapes[] =
{
new Square(5),
new Triangle(8,10),
new Square(7),
new Triangle(3,4)
};
// Because we have an array of pointers to Shapes, which support dynamic
// binding of the area() member function, we can use runtime polymorphism
// to loop through our array of pointers to Shapes and call the area
// member function for each object! This allows us to utilize polymorphism,
// BUT without implementing a "generic Shape" area member function that
// would not make any sense.
//
for (int i = 0; i < 4; i++)
cout << "Shape " << i << ": " << shapes[i]->area() << endl;
return 0;
}