-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtual_functions.cpp
More file actions
29 lines (25 loc) · 823 Bytes
/
Copy pathVirtual_functions.cpp
File metadata and controls
29 lines (25 loc) · 823 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
#include<iostream>
using namespace std;
class BaseClass {
public:
int var_base = 43;
virtual void display(){
cout << "Displaying base class variable var_base "<<var_base << endl;
}
};
class DerivedClass : public BaseClass {
public:
int var_derived = 23;
void display(){
cout << "Displaying base class variable var_base "<<var_base << endl;
cout<<"Displaying derived class variable var_derived "<<var_derived << endl;
}
};
int main() {
BaseClass * base_class_pointer;
BaseClass obj_base;
DerivedClass obj_derived;
base_class_pointer = &obj_derived; // base_class_pointer is pointing to DerivedClass object.
base_class_pointer->display(); // calling display() function of DerivedClass object.
return 0;
}