程序设计教程(机械工业出版社)课后习题答案 第8章 继承――派生(2)

2019-02-16 00:06

bool pop(int &x); LinearList::length; };

bool Stack::push(int x) { return insert(x,0); }

bool Stack::pop(int &x) { return remove(x,1); }

8、 利用习题6.8的第14、15题中的时间类Time和日期类Date,定义一个带日期的时间

类TimeWithDate。对该类对象能进行比较、增加(增加值为秒数)、相减(结果为秒数)等操作。 答:

#include using namespace std; class Time {

public: Time()

{ hour=minute=second=0; }

Time(int h,int m,int s)

{ hour=h;minute=m;second=s; }

void set(int h, int m, int s) { hour=h; minute=m; second=s; }

void increment() { if (second==59) { if (minute==59) { if (hour==23) { hour=0; minute=0; second=0; } else

{ hour++; minute=0; second=0; } }

else

{ minute++; second=0; } }

else

second++; }

void display() const

{ cout<

bool equal(const Time &time2) const { if (hour == time2.hour && minute == time2.minute && second == time2.second) return true; else

return false; }

bool less_than(const Time &time2) const { if (hour < time2.hour ||

hour == time2.hour && minute < time2.minute || hour == time2.hour && minute == time2.minute && second < time2.second) return true; else

return false; }

protected: int hour; int minute; int second; };

class Date { public:

Date(int y, int m, int d)

{ year = y; month = m; day = d; }

void increment() { int d;

switch (month)

{ case 1: case 3: case 5: case 7: case 8: case 10: case 12: d = 31; break;

case 4: case 6: case 9: case 11: d = 30; break; case 2:

if (year@0 == 0 || year%4 == 0 && year0 != 0) d = 29; else

d = 28; }

if (day < d) day++;

else if (month != 12) { day = 1; month++; } else

{ day = 1; month = 1; year++; } }

bool equal(const Date &date2) const

{ return year == date2.year && month == date2.month && day == date2.day; }

bool less_than(const Date &date2) const { if (year < date2.year ||

year == date2.year && month < date2.month ||

year == date2.year && month == date2.month && day < date2.day)

return true; else

return false; } void display() const

{ cout << year << '-' << month << '-' << day; }

protected:

int year,month,day; };

class TimeWithDate: public Time, public Date {

public:

TimeWithDate(int y,int mo,int d,int h,int mi,int s):Date(y,mo,d),Time(h,mi,s) {}

bool equal(const TimeWithDate &td2) const;

bool less_than(const TimeWithDate &td2) const; void increment();

int difference(const TimeWithDate & td2) const; void display() const; };

bool TimeWithDate::equal(const TimeWithDate &td2) const { return Time::equal(td2) && Date::equal(td2); }

bool TimeWithDate::less_than(const TimeWithDate &td2) const { if (Date::less_than(td2)) return true;

else if (Date::equal(td2) && Time::less_than(td2)) return true; else

return false; }

void TimeWithDate::increment() { Time::increment();

if (((Time *)this)->equal(Time(0,0,0)))

//或 if (hour == 0 && minute == 0 && second == 0) Date::increment(); }

int TimeWithDate::difference(const TimeWithDate &td2) const { int days=0;

if (less_than(td2))

for (TimeWithDate td=*this; td.less_than(td2); td.increment()) days--; else

for (TimeWithDate td=td2; td.less_than(*this); td.increment()) days++; return days; }

void TimeWithDate::display() const { Date::display(); cout << ' ';

Time::display(); }

9、 如何定义两个类A和B(B是A的派生类),使得在程序中能够创建一个与指针变量p

(类型为A *)所指向的对象是同类的对象?

答:

class A { ... public:

virtual A *create() { return new A; } };

class B:public A { ... public:

A *create()

{ return new B; } };

int main() { A *p;

if (...)

p = new A; else

p = new B; ... A *q;

q = p->create(); //创建一个与p所指向的对象同类的对象。 ... }

10、下面的设计有什么问题?如何解决?

class Rectangle //矩形类 { public:

Rectangle(double w, double h): width(w), height(h) {} void set_width(double w) { width = w; } void set_height(double h) { height = h; } double get_width() const { return width; } double get_height() const { return height; } double area() const { return width*height; }

void print() const { cout << width << \ private:

double width; //宽

double height; //高 };

class Square: public Rectangle //正方形类 { public:

Square(double s): Rectangle(s,s) {}

void set_side(double s) //设置边长。 { set_width(s); set_height(s); }

double get_side() const //获取边长。 { return get_width(); } };

答:Square不能以public继承方式继承Rectangle类,否则,Rectangle的所有public成员函

数就能被Square类的对象访问,特别地,当用set_width和set_height分别对Square类的对象进行操作时,就可能破坏Square类对象的长、宽相等的特性。

解决办法是: 把Square定义成以protected或private方式从Rectangle继承。为了能对Square类的对象访问Rectangle中定义的area和print,可在Square类中加上对Rectangle类成员的访问控制调整声明:

class Square: Rectangle //正方形类

{ public: ...

Rectangle::area; Rectangle::print; };


程序设计教程(机械工业出版社)课后习题答案 第8章 继承――派生(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2017-2018学年高中地理 太阳对地球的影响双基限时练 新人教版必

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: