}
int main( int argc, char** argv ) { Car *a = new Bus(\黄海\北京客运\ Car *b = new Truck(\东风\北京货运\ cout<<\ a->show(); cout<<\ b->show(); system(\ return 0; } 5.【程序参考代码】 #include
class Table{ public: Table(double h){height=h;} double get_height(){return height;} private: double height; };
class RoundTable:public Table,public Circle { char *color; public: RoundTable(double h,double r,char c[]):Table(h),Circle(r) { color=new char[strlen(c) +1]; strcpy(color,c); } char* get_color(){return color;} };
int main() { RoundTable rt(0.8,1.0,\白色\ cout<<\圆桌的高:\ cout<<\圆桌面积:\ cout<<\圆桌颜色:\ return 0; } 6.【程序参考代码】 #include
Car( string model, int color, unsigned int motopower, unsigned int speed,unsigned int weight, unsigned int id) { _model = model; _color = color; _motopower = motopower;
第 29 页
_speed = speed; _weight = weight; _id = id; } void show(); private:
string _model; int _color;
unsigned int _motopower; unsigned int _speed; unsigned int _weight; unsigned int _id; };
class Bus: virtual public Car {public: Bus( string model, int color, unsigned int motopower, unsigned int speed,unsigned int weight, unsigned int id, unsigned int seatnum, string corp ):Car(model,color, motopower,speed,weight,id) { _seatnum = seatnum; _corp = corp; } void display(); protected:
unsigned int _seatnum; string _corp; };
class Wagon : virtual public Car {public: Wagon( string model, int color, unsigned int motopower, unsigned int speed,unsigned int weight, unsigned int id, unsigned int load, string corp ): Car(model,color, motopower,speed,weight,id) { _load = load; _corp = corp; } void display(); protected:
unsigned int _load; string _corp; };
class StationWagon : public Bus,public Wagon {public: StationWagon( string model, int color, unsigned int motopower, unsigned int speed,unsigned int weight, unsigned int id, unsigned int seatnum,unsigned int load, string corp ):
Car(model,color, motopower,speed,weight,id),
Bus(model,color, motopower,speed,weight,id,seatnum,corp), Wagon(model,color, motopower,speed,weight,id,load,corp)
{ } void display(); };
void Car::show() { cout<<\型号:\ cout<<\颜色:\ cout<<\发动机功率:\ cout<<\车速:\ cout<<\重量:\ cout<<\车牌号码:\ }
void Bus::display() { show();
cout<<\客车座位数:\ cout<<\客运公司:\}
void Wagon::display(){ show();
30
cout<<\载货重量:\ cout<<\货运公司:\}
void StationWagon::display(){ show(); cout<<\客车座位数:\ cout<<\载货重量:\ cout<<\客运及货运公司:\}
int main( ) { StationWagon sw(\黄海\北京客货运\ sw.display(); system(\ return 0; } 7.【程序参考代码】 #include
//设置成员变量,mm:月份;dd:天数;yy:年份; void Date::setDate(const int mm, const int dd, const int yy) { year=yy; month=mm; day=dd; }
void Date::display() {
cout< int Date::getYear(){ return year; } int Date::getMonth(){ return month; } int Date::getDay(){ return day; } class Employee {public: Employee(char *ID, char *newName,int y,int m,int d); Employee(const Employee &); void SetData(char *ID, char *newName, int y,int m,int d); void Display(); bool IsBirthday(Date day); private: char id[10]; char name[20]; Date birthday; }; 第 31 页 Employee::Employee(char *ID, char *newName, int y,int m,int d):birthday(y,m,d) { strcpy(id, ID); strcpy(name, newName); } Employee::Employee(const Employee &other) { strcpy(id, other.id); strcpy(name, other.name); birthday=other.birthday; } void Employee::SetData(char *ID, char *newName,int y,int m,int d) { strcpy(id, ID); strcpy(name, newName); birthday.setDate(y,m,d); } void Employee::Display() { cout<<\ cout<<\ birthday.display(); cout< bool Employee::IsBirthday(Date day) { if(birthday.getYear()==day.getYear() && birthday.getMonth()==day.getMonth() && birthday.getDay()==day.getDay()) return true; else return false; } int main() { Date today(1980,11,20); Employee employee(\ if( employee.IsBirthday(today)) cout<<\ else cout<<\ system(\ return 0; } 32 第5章 多态性与虚函数 一、简答题 1.【答案要点】 在面向对象方法中一般是这样表述多态性:向不同的对象发送同一个消息,不同的对象在接收时会有不同的反应,产生不同的动作。也就是说,每个对象可以用自己的方式去响应共同的消息。在C++程序设计中,多态性是指用一个名字定义不同的函数,这些函数执行不同但又类似的操作,从而可以使用相同的调用方式来调用这些具有不同功能的同名函数。 多态从实现的角度来讲可以划分为两类:编译时的多态和运行时的多态。编译时的多态是在编译的过程中确定了同名操作的具体操作对象,而运行时的多态则是在程序运行过程中才动态地确定操作所针对的具体对象。在C++中,编译时的多态性主要是通过函数重载和运算符重载实现的。运行时的多态性主要是通过虚函数来实现的。 2.【答案要点】 如果一个类至少有一个纯虚函数,那么就称该类为抽象类。 抽象类只能作为其他类的基类来使用,为其派生类提供一个接口规范,其纯虚函数的实现由派生类给出。 3.【答案要点】 构造函数不能声明为虚构造函数。因为虚函数作为运行过程中多态的基础,主要是针对对象的,而构造函数是在对象产生之前运行的,因此虚构造函数是没有意义的。 析构函数可以是虚函数,并且最好把基类的析构函数声明为虚函数,这将使其所有派生类的析构函数自动成为虚函数。这样,如果程序中显式地用了delete运算符准备删除一个对象,而delete运算符的操作对象用了指向派生类对象的基类指针,则系统会调用相应类的析构函数,对该派生类对象进行清理工作。 4.【答案要点】 C++中的多态性可以分为4类:参数多态、包含多态、重载多态和强制多态。参数多态如函数模板和类模板,包含多态通过虚函数来实现,重载多态如函数重载和运算符重载,强制多态如类型强制转换。前面两种统称为通用多态,而后面两种统称为专用多态。 二、编程题 1.【程序参考代码】 #include cout<<\书名:\ cin>>title; } void printtitle(){ cout<<\书名:\ \ } virtual bool isgood()=0; protected: char title[80]; }; class Book: public base {public: void setsold() { cout<<\每月销售书量:\ cin>>numsold; } bool isgood(){ return(numsold>500)?true:false; } private: int numsold; //月销售书量 }; class Journal: public base 第 33 页
相关推荐: