首页 / 软件开发 / C++ / C++的虚函数与抽象类
C++的虚函数与抽象类2011-04-281.虚函数1.1虚函数的作用虚函数的作用是允许在派生类中重新定义与基 类同名的函数,并且可以通过基类指针或引用来访问基类和派生类中的同名函数。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<<hour<<":"<<min<<":"<<sec& lt;<endl;
}
LocalTime::LocalTime(int h,int m,int s,string z):Time (h,m,s),zone(z){}
void LocalTime::show(){
cout<<hour<<":"<<min<<":"<<sec& lt;<"@"<<zone<<endl;
}
int main(){
Time t;
LocalTime lt;
Time *pt=&t;
pt->show();
pt=<
pt->show();
system("PAUSE");
return EXIT_SUCCESS;
}
结果:0:0:0
0:0:0