-
Notifications
You must be signed in to change notification settings - Fork 7
/
pointer_to_derived_class.cpp
39 lines (34 loc) · 1.12 KB
/
pointer_to_derived_class.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
#include<iostream>
using namespace std;
class BaseClass{
public:
int var_base;
void display(){
cout<<"Dispalying Base class variable var_base "<<var_base<<endl;
}
};
class DerivedClass : public BaseClass{
public:
int var_derived;
void display(){
cout<<"Dispalying Base class variable var_base "<<var_base<<endl;
cout<<"Dispalying 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; // Pointing base class pointer to derived class
base_class_pointer->var_base = 34;
// base_class_pointer->var_derived= 134; // Will throw an error
base_class_pointer->display();
base_class_pointer->var_base = 3400;
base_class_pointer->display();
DerivedClass * derived_class_pointer;
derived_class_pointer = &obj_derived;
derived_class_pointer->var_base = 9448;
derived_class_pointer->var_derived = 98;
derived_class_pointer->display();
return 0;
}