-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inheritance trial
60 lines (53 loc) · 1.61 KB
/
Inheritance trial
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
#include <iostream>
using namespace std;
// this is branch 2
class Base{
public:
Base(){cout<<"Base constructor"<<endl;}
~Base(){cout<<"Base destructor"<<endl;}
void f1(){cout<<"Base f1"<<endl;}
virtual void f2(){cout<<"Base f2"<<endl;}
virtual void f3(int i){cout<<"Base f3: "<<i<<endl;}
virtual void f4(int i){cout<<"Base f4: "<<i<<endl;}
virtual void f5(){cout<<"Base f5"<<endl;}
};
class Derived: public Base{
private:
void f5(){cout<<"Derived f5 showing that at runtime we can call private function of derived class as well"<<endl;}
public:
Derived(){cout<<"Derived constructor"<<endl;}
~Derived(){cout<<"Derived destructor"<<endl;}
void f1(){cout<<"Derived f1"<<endl;}
void f2(){cout<<"Derived f2"<<endl;}
void f3(){cout<<"Derived f3"<<endl;}
};
int main() {
Base b1;
Derived d1;
Base *bptr=&b1;
cout << "Base ptr pointing to base object"<<endl;
bptr->f1();
bptr->f2();
// bptr->f3(); gives error
bptr->f3(5);
bptr=&d1;
cout << "Base ptr pointing to derived object"<<endl;
bptr->f1();
bptr->f2();
// bptr->f3(); gives error
bptr->f3(5);
bptr->f5();
Derived d2;
Derived *dptr=&d2;
cout << "Derived ptr pointing to derived object"<<endl;
dptr->f1();
dptr->f2();
dptr->f3();
// dptr->f3(5); gives error
dptr->f4(5);
// dptr->f5(); this gives error as it is resolved at compile-time
Base *bptr_dynamic=new Derived;
delete bptr_dynamic;
cout << "above shows menory leak--- we can put virtual ketword to base destructor in Base class";
return 0;
}