-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstractClass.cpp
More file actions
90 lines (70 loc) · 1.69 KB
/
Copy pathabstractClass.cpp
File metadata and controls
90 lines (70 loc) · 1.69 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
//Example of pure virtual function -- Abstract CLass -- Runtime Polymorphism
//Prithwiraj Shome
#include <iostream>
using namespace std;
typedef class Shape {
protected:
double height = 0;
double width = 0;
double radius = 0;
const double pi = 3.14;
public:
void set_height_width(double h, double w)
{
height = h;
width = w;
}
void set_radius(double r)
{
radius = r;
}
virtual void show() = 0; //Pure virtual function -- will be defined in derived classes
virtual double area() = 0; //Pure virtual function -- will be defined in derived classes
}SHAPE;
typedef class Rectangle : public SHAPE {
public:
Rectangle(double h, double w)
{
height = h;
width = w;
}
double area()
{
return (height * width);
}
void show()
{
cout << "Height = " << height << endl;
cout << "Width = " << width << endl;
}
}RECTANGLE;
typedef class Circle : public SHAPE {
public:
Circle(double r)
{
radius = r;
}
double area()
{
return (pi * radius * radius);
}
void show()
{
cout << "Radius = " << radius << endl;
}
}CIRCLE;
int main(void)
{
SHAPE * sPtr = new RECTANGLE(3,4); // Notice the pointer is of base class type but instance is of derived class type
sPtr->show();
cout << "Rectangle area is = " << sPtr->area() << " unit square" << endl;
sPtr->set_height_width(2, 2);
sPtr->show();
cout << "Rectangle area is = " << sPtr->area() << " unit square" << endl;
SHAPE * cPtr = new CIRCLE(1); // Notice the pointer is of base class type but instance is of derived class type
cPtr->show();
cout << "Circle Area is = " << cPtr->area() << " unit square" << endl;
cPtr->set_radius(5);
cPtr->show();
cout << "Circle Area is = " << cPtr->area() << " unit square" << endl;
}