-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual2.cpp
More file actions
38 lines (32 loc) · 751 Bytes
/
virtual2.cpp
File metadata and controls
38 lines (32 loc) · 751 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
/*virtual function helps implement a member fuction ofr the derived class instead of the base class*/
#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";
}
virtual bool addCourse(string s)=0;
};
//inheritance example
class Student:public Person{
public:
void aboutMe(){
cout<<"I am a student";
}
bool addCourse (string s){
cout<<"Added course "<<s<< " to student "<<endl;
return true;
}
};
int main(){
Person *p=new Student(); //prints i a m student
p->aboutMe(); //prints i am a student
p->addCourse("history");
delete p;
return 0;
}