1. Virtual function
1.1 Function of virtual function
The function of a virtual function is to allow a function with the same name as the base class to be redefined in a derived class, and a function of the same name in a base class and a derived class can be accessed through a base class pointer or reference.
class Time{
public:
Time(int=0,int=0,int=0);
void show();
protected:
int hour;
int min;
int sec;
};
class LocalTime:public Time{
public:
LocalTime(int=0,int=0,int=0,string="+8");
void show();
protected:
string zone;
};
Time::Time(int h,int m,int s):hour (h),min(m),sec(s){}
void Time::show(){
cout< }
LocalTime::LocalTime(int h,int m,int s,string z):Time (h,m,s),zone(z){}
void LocalTime::show(){
cout< }
int main(){
Time t;
LocalTime lt;
Time *pt=&t;
pt->show();
pt=<
pt->show();
system("PAUSE");
return EXIT_SUCCESS;
}
Results:
0:0:0
0:0:0