-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance1.cpp
More file actions
79 lines (64 loc) · 1.21 KB
/
Inheritance1.cpp
File metadata and controls
79 lines (64 loc) · 1.21 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
/*
Author: CWN221
Date: 24/10/2024
Description: Demonstrating concept of Inheritance in C++
RegNo: BSE-01-0032/2024
*/
#include <iostream>
using namespace std;
const float PI = 3.142;
class Shape{
protected:
string color;
public:
//Member function
void setColor(string c)
{
color = c;
};
string getColor()
{
return color;
};
};
class Rectangle: public Shape {
private:
float length;
float width;
public:
//Constructor
Rectangle(float l, float w, string c) {
length = l;
width = w;
setColor(c);
}
//Area
double area(){
return length * width;
}
};
class Circle: public Shape {
private:
float radius;
public:
//Constructor
Circle(float r, string c) {
radius = r;
setColor(c);
}
//Area
double area()
{
return PI * radius * radius;
}
};
int main()
{
Rectangle rect(15, 10, "blue");
Circle cir (2, "pink");
//Display rectangle results
cout<<"Rectangle(color): "<<rect.getColor()<<"\nRectangle(area): "<<rect.area()<<endl;
//Display circle results
cout<<"\nCircle(color): "<<cir.getColor()<<"\nCircle(area): "<<cir.area()<<endl;
return 0;
}