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<<endl;
}
LocalTime::LocalTime(int h,int m,int s,string z):Time(h,m,s),zone(z){}
void LocalTime::show(){
cout<<hour<<":"<<min<<":"<<sec<<"@"<<zone<<endl;
}
int main(){
Time t;
LocalTime lt;
Time *pt=&t;
pt->show();
pt=<
pt->show();
system("PAUSE");
return EXIT_SUCCESS;
}
|
class Time{
public:
Time(int=0,int=0,int=0);
virtual void show();
…
};
|
class Base{
public:
virtual Base *fun(){
cout<<"Base's fun()."<<endl;
return this;
}
};
class Derived:public Base{
public:
virtual Derived *fun(){
cout<<"Derived's fun()."<<endl;
return this;
}
};
void test(Base &x){
Base *b;
b=x.fun();
}
int main(){
Base b;
Derived d;
test(b);
test(d);
system("PAUSE");
return EXIT_SUCCESS;
}
|
class Time{
public:
Time(int=0,int=0,int=0);
~Time(){
cout<<"Time destructor"<<endl;
}
protected:
int hour;
int min;
int sec;
};
class LocalTime:public Time{
public:
LocalTime(int=0,int=0,int=0,string="+8");
~LocalTime(){
cout<<"LocalTime destructor"<<endl;
}
protected:
string zone;
};
Time::Time(int h,int m,int s):hour(h),min(m),sec(s){}
LocalTime::LocalTime(int h,int m,int s,string z):Time(h,m,s),zone(z){}
int main(){
Time *p=new LocalTime;//指向派生类
delete p;
system("PAUSE");
return EXIT_SUCCESS;
}
|
virtual ~Time(){
cout<<"Time destructor"<<endl;
}
|
virtual void show()=0;//纯虚函数
|
virtual 函数类型 函数名(参数列表)=0;
|