-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual_fun.cpp
More file actions
48 lines (43 loc) · 768 Bytes
/
Copy pathvirtual_fun.cpp
File metadata and controls
48 lines (43 loc) · 768 Bytes
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
#include<iostream>
using namespace std;
class parent{
int a;
public:
parent()
{
cout<<"Parent constructor"<<endl;
}
virtual void get_data(){
a=1;
cout<<a<<endl;
}
~parent()
{
cout<<"Parent distructor"<<endl;
}
};
class child : public parent{
int b;
public:
child()
{
cout<<"Child class constructor"<<endl;
}
void get_data()
{
b=20;
cout<<b<<endl;
}
~child()
{
cout<<"child constructor"<<endl;
}
};
int main()
{
parent *p;
p=new child;
p->get_data();//it the get data of parent in vertul then only it will call child's get data else it will alwayas call parent's get data fun
delete p;
return 0;
}