forked from tridibsamanta/CPP_Beginner_to_Expert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPP039_AbstractClass_PureVirtualFunction.cpp
More file actions
50 lines (47 loc) · 1.08 KB
/
Copy pathCPP039_AbstractClass_PureVirtualFunction.cpp
File metadata and controls
50 lines (47 loc) · 1.08 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
/**
* Author: Tridib Samanta
* Created: 09.02.2020
**/
/*
One important thing to note is that, you should override the pure virtual function of the base class in the derived class.
If you fail the override it, the derived class will become an abstract class as well.
*/
#include <iostream>
using namespace std;
// Abstract class
class Shape
{
protected:
float l;
public:
void getData()
{
cin >> l;
}
// virtual Function
virtual float calculateArea() = 0;
};
class Square : public Shape
{
public:
float calculateArea()
{ return l*l; }
};
class Circle : public Shape
{
public:
float calculateArea()
{ return 3.14*l*l; }
};
int main()
{
Square s;
Circle c;
cout << "Enter length to calculate the area of a square: ";
s.getData();
cout<<"Area of square: " << s.calculateArea();
cout<<"\nEnter radius to calculate the area of a circle: ";
c.getData();
cout << "Area of circle: " << c.calculateArea();
return 0;
}