Virtual Function¶
Examples¶
C++¶
Student * p = new Student();
p->aboutMe();
Person * p = new Student();
p->aboutMe();
class Person {
...
virtual void aboutMe() {
cout << "I am a person.";
}
};
class Student : public Person {
public:
void aboutMe() {
cout << "I am a student.";
}
};
class Person {
int id;
char name[NAME_SIZE];
public:
virtual void aboutMe() {
cout << "I am a person." << endl;
}
virtual bool addCourse(string s) = 0;
};
class Student : public Person {
public:
void aboutMe() {
cout << "I am a student." << endl;
}
bool addCourse(string s) {
cout << "Added course " << s << " to student." << endl;
return true;
}
};
int main() {
Person * p = new Student();
p->aboutMe();
p->addCourse("history");
delete p;
}