-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPolymorphism_Virtual_function.cpp
62 lines (54 loc) · 1.53 KB
/
Polymorphism_Virtual_function.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
62
// 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;
}
virtual int getarea()
{
cout<<"\ngetarea() acceced from Base class: shape\n";
return 0;
};
};
class rectangle: public shape // Derived classes
{
public:
int getarea()
{
cout<<"\ngetarea() acceced from derived class : rectangle\n";
return (length * breadth);
}
};
class square: public shape // Derived classes
{
public:
int getarea()
{
cout<<"\ngetarea() acceced from derived class : square\n";
return (length*length);
}
};
int main()
{
shape S;
S.getarea();
rectangle rect;
rect.setbreadth(5);
rect.setlength(7);
cout << "\nArea of rectangle is: " << rect.getarea() << endl; // Print the area of the object.
square sq;
sq.setlength(3);
cout << "\nArea of square is: " << sq.getarea() << endl; // Print the area of square.
return 0;
}