-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtual_function.cpp
More file actions
46 lines (41 loc) · 803 Bytes
/
Virtual_function.cpp
File metadata and controls
46 lines (41 loc) · 803 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
#include <iostream>
using namespace std;
class A // Base class
{
protected:
int i;
public:
virtual void get()
{
cout << "Enter i: ";
cin >> i;
}
virtual void display()
{
cout << "i: " << i << endl;
}
};
class B: public A // Derived class
{
protected:
int j;
public:
void get()
{
cout << "Enter j: ";
cin >> j;
}
void display()
{
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;
}