-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual.cpp
More file actions
31 lines (26 loc) · 804 Bytes
/
virtual.cpp
File metadata and controls
31 lines (26 loc) · 804 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
#include <iostream>
using namespace std;
#define NAME_SIZE 50 //defines a macro
//fragment of ocde using that nname
class Person{
int id;
char name[NAME_SIZE];
public:
virtual void aboutMe(){
cout<<"I am a person";
}
};
//inheritance example
class Student:public Person{
public:
void aboutMe(){
cout<<"I am a student";
}
};
int main(){
Person *p=new Student(); //prints i a m student
p->aboutMe(); //prints i am a student
delete p;
return 0;
}
/*A virtual function a member function which is declared within base class and is re-defined (Overriden) by derived class.When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.*/