-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInheritance_Base_Derived_class_1.cpp
50 lines (45 loc) · 1.17 KB
/
Inheritance_Base_Derived_class_1.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
// author: jaydattpatel
#include<iostream>
using namespace std;
class shape // Base class
{
protected: // protected data can be access within class and derived class
int length;
int breadth;
public:
void setlength(int l)
{
length = l;
}
void setbreadth(int b)
{
breadth = b;
}
};
class rectangle: public shape // Derived classes
{
public:
int getarea()
{
return (length * breadth);
}
};
class square: public shape // Derived classes
{
public:
int getarea()
{
return (length*length);
}
};
int main()
{
rectangle rect;
square sq;
rect.setbreadth(5);
rect.setlength(7);
cout << "Area of rectangle is: " << rect.getarea() << endl; // Print the area of the object.
sq.setlength(5);
cout << "Area of square is: " << sq.getarea() << endl; // Print the area of square.
return 0;
}