-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path34.polymorphismExercise.cpp
61 lines (57 loc) · 1.33 KB
/
34.polymorphismExercise.cpp
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
#include <iostream>
using namespace std;
class Shape
{
public:
virtual void area() = 0;
virtual void perimeter() = 0;
};
class Rectangle : public Shape
{ private:
int length;
int breadth;
public:
Rectangle(int length, int breadth){
this->length = length;
this->breadth = breadth;
}
void area(){
int area;
area = length * breadth;
cout<<"Area of a rectangle: "<<area<<endl;
}
void perimeter(){
int perimeter;
perimeter = 2 * (length + breadth);
cout<<"Perimeter of a rectangle: "<<perimeter<<endl;
}
};
class Circle: public Shape
{
private:
int radius;
public:
Circle(int radius){
this->radius = radius;
}
void area(){
int area;
area = 3.14 * radius * radius;
cout<<"Area of a circle: "<< area<<endl;
}
void perimeter(){
int perimeter;
perimeter = 2 * 3.14 * radius;
cout<<"Perimeter of a circle: "<<perimeter<<endl;
}
};
int main(){
Shape *myShape;
myShape = new Circle(4);
myShape->area();
myShape->perimeter();
myShape = new Rectangle(23,34);
myShape->area();
myShape->perimeter();
return 1;
}