-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path55_Pointers_to_derived_classes.cpp
62 lines (40 loc) · 1.64 KB
/
55_Pointers_to_derived_classes.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
// Pointers to derived Classes...!!
#include<iostream>
using namespace std;
class BaseClass{
public:
int var_base;
void display(){
cout << "Displaying Base class variable var_base: " << var_base << endl;
}
};
class DerviedClass : public BaseClass{
public:
int var_derived;
void display(){
cout << "Displaying Base class variable var_base: " << var_base << endl;
cout << "Displaying Derived class variable var_derived: " << var_derived << endl;
}
};
int main()
{
// Pointer to a Base Class object
BaseClass * base_class_pointer;
BaseClass obj_base;
DerviedClass obj_derived;
// stored the address of derived class object to the pointer
base_class_pointer = &obj_derived; // Pointing base class pointer to derived class
base_class_pointer -> var_base = 34;
// base_class_pointer -> derived_base = 34; --> Will show an error
base_class_pointer -> display(); // Only runs the display function of base Class because object to derived class ka hai magar pointer Base class ka hai.
base_class_pointer -> var_base = 3400;
base_class_pointer -> display();
// ***************************************************************************************************************************************************
// Pointer of derived class pointing to derived class object
DerviedClass * derived_class_pointer;
derived_class_pointer = &obj_derived;
derived_class_pointer -> var_base = 5390498;
derived_class_pointer -> var_derived = 98;
derived_class_pointer -> display();
return 0;
}