-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path49Pointers_to_DerivedClass.cpp
More file actions
43 lines (34 loc) · 1.38 KB
/
Copy path49Pointers_to_DerivedClass.cpp
File metadata and controls
43 lines (34 loc) · 1.38 KB
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
#include <iostream>
using namespace std;
class BaseClass{
public:
int var_base;
void display(){
cout<<"Displaying Base class variable var_base "<<var_base<<endl;
}
};
class DerivedClass : public BaseClass{
public:
int var_derived;
void display(){
cout<<"Displaying Base class variable var_base "<<var_base<<endl;
cout<<"Displaying Derived class variable var_display "<<var_derived<<endl;
}
};
int main(){
BaseClass *base_class_pointer;
BaseClass obj_base;
DerivedClass obj_derived;
base_class_pointer = &obj_derived; // Base class pointer can point toward the Derived Class object
base_class_pointer->var_base = 34;
// base_class_pointer->var_derived = 99; //This will throw an error, as it can only change or access data of base clas
(*base_class_pointer).display(); // Even though the pointer is pointing towards the object of Derived class,
// but while calling the function display which is same in both class, the function which is
// called will be of Base class.
DerivedClass *derived_class_pointer;
derived_class_pointer = &obj_derived;
derived_class_pointer->var_base = 77;
derived_class_pointer->var_derived = 78;
derived_class_pointer->display();
return 0;
}