-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvfunc2.cpp
More file actions
47 lines (46 loc) · 1.2 KB
/
Copy pathvfunc2.cpp
File metadata and controls
47 lines (46 loc) · 1.2 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
44
45
46
47
/*
pure virtual functions are hierarchial
*/
#include<iostream>
using namespace std;
class base/*base class is now an abstrac class because it contains one or more than one
pure virtual function*/
{
public:
virtual void vfunc()=0;//pure virtual function , all derived class must override function
};
class derived1:public base
{
public:
void vfunc()//derived1 function overriding;
{
cout<<"dc1 vfunc\n";
}
};
class derived2:public base
{
void vfunc()//derived2 function overriding;
{
cout<<"dc2 vfunc\n";
}
};
int main()
{
base *p;
//base b; (error) abstract class cannot have a object
derived1 d1;
derived2 d2;
p=&d1;
p->vfunc();
p=&d2;
p->vfunc();
}
/*
If their is no meaningfull defination of a virtual function within the base class
example: A base class may not be able to define an object sufficiently to allow a base class
virtual function to be created or we will want to ensure that all derived classes
override a virtual function
the above two cases can be handled by using pure virtual function.
syntax:
virtual return_type function_name(parameter_list)=0;
*/