第一范文网 - 专业文章范例文档资料分享平台

《C++面向对象程序设计》习题答案

来源:用户分享 时间:2025/8/8 15:53:51 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

}

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 using namespace std; #include class Circle {public: Circle(double r){radius=r;} double get_area() {return 3.1416*radius*radius;} private: double radius; };

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 #include using namespace std; class Car {public:

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 #include using namespace std; class Date{ public: Date(){ year=0; month=0; day=0; } //无参构造函数 Date(int mm, int dd, int yy){ year=yy; month=mm; day=dd; } //带参数的构造函数 Date(const Date &d){ year=d.year; month=d.month; day=d.day; } //复制构造函数 void setDate(const int, const int, const int); //修改日期的函数 void display(); //输出日期的函数 int getYear(); //获取年份的函数 int getMonth(); //获取月份的函数 int getDay(); //获取日信息的函数 private: int year, month, day; };

//设置成员变量,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 #include using namespace std; class base {public: void settitle() {

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 页

搜索更多关于: 《C++面向对象程序设计》习题答案 的文档
《C++面向对象程序设计》习题答案.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c1o5nt7wnwx2nsfs0jg6i_7.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top