-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx-1_polymorphism.cpp
More file actions
48 lines (43 loc) · 905 Bytes
/
Ex-1_polymorphism.cpp
File metadata and controls
48 lines (43 loc) · 905 Bytes
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
#include <iostream>
using namespace std;
class A // Base class
{
protected:
int i;
public:
void get()
{
cout << "Enter i: ";
cin >> i;
}
void display()
{
cout << "i: " << i << endl;
}
};
class B: public A // Derived class
{
protected:
int j;
public:
void get()
{
A::get(); // Call base class get() to get i
cout << "Enter j: ";
cin >> j;
}
void display()
{
A::display(); // Call base class display() to display i
cout << "j: " << j << endl;
}
};
int main()
{
A *ptr; // Pointer to base class
B obj; // Object of derived class
ptr = &obj; // Pointing to derived class object
ptr->get(); // Call derived class get() using base class pointer
ptr->display(); // Call derived class display() using base class pointer
return 0;
}