-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtualFunctions.cpp
51 lines (40 loc) · 1.46 KB
/
virtualFunctions.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
#include <iostream>
using namespace std;
// pointer_to_derived_class is pre requisite for this
class BaseClass
{
public:
int var_base = 1;
virtual void display()
{
cout << "1- Value of var_base is: " << var_base << endl;
}
};
class DerivedClass : public BaseClass
{
public:
int var_derived = 2;
void display()
{
cout << "2- Value of var_base is: " << var_base << endl;
cout << "2- Value of var_derived is: " << var_derived << endl;
}
};
int main()
{
BaseClass BC_obj;
DerivedClass DC_obj;
//*****************************************************************************************************
/* By doing this, we had seen in the [Pointers_to_Derived_class] that, we were only able to access the
base class functions and data members.
BaseClass *base_class_pointer = &DC_obj; // pointing base class pointer to derived class object
base_class_pointer->display(); */
//******************************************************************************************************
/* Now to cope up with the above behaviour such that; we would be able to access the derived class data and
function members through the base class pointer; there exist VIRTUAL (LATE BINDING IMPLEMENTATION) in C++ */
// Applying virtual keyword in the display of base class
// Now Derived class display is executing
BaseClass *base_class_pointer = &DC_obj;
base_class_pointer->display();
return 0;
}