面向对象程序设计课后习题答案

发布时间 : 星期四 文章面向对象程序设计课后习题答案更新完毕开始阅读

date::display(); cout<

time::display(); } protected:

char childname[20]; };

void main()

{ birthtime yx(\ yx.display(); }

[5_17]程序如下

#include #include class animal { public: animal()

{ name=NULL; } animal(char *n); ~animal()

{ delete name; } virtual void whoami(); protected:

char *name; };

class cat:public animal { public:

cat():animal() { }

cat(char *n):animal(n) { }

void whoami(); };

class tiger:public cat { public:

tiger():cat() { }

tiger(char *n):cat(n) { }

void whoami(); };

animal::animal(char *n)

{ name=new char [strlen(n)+1]; strcpy(name,n);

}

void animal::whoami()

{ cout<<\ } void cat::whoami()

{ cout<<\ } void tiger::whoami()

{ cout<<\void main() //添加的主函数 { cat cat(\ cat.whoami();

tiger tiger(\ tiger.whoami(); }

[5_18]实现本题功能的程序如下: #include class building { public:

building(int f,int r,double ft) { floors=f; rooms=r;

footage=ft; }

protected:

int floors;//层数

int rooms; //房间数 double footage;//平方数 };

class house:public building { public:

house(int f,int r,double ft,int br,int bth) nbsp; :building(f,r,ft)

{ bedrooms=br; bathrooms=bth; } void show()

{ cout<<\ cout<<\ \

cout<<\ cout<<\ cout<<\ } private:

int bedrooms;//卧室数 int bathrooms;//浴室数 };

class office:public building

{ public:

office(int f,int r,double ft,int p,int ext) :building(f,r,ft)

{ phones=p; extinguishers=ext; } void show()

{ cout<<\ cout<<\ \

cout<<\ cout<<\ cout<<\ cout<

int phones;//电话数

int extinguishers;//灭火器数 }; main()

{ house h_ob(2,12,5000,6,4); office o_ob(4,25,12000,30,8); cout<<\ h_ob.show();

cout<<\ o_ob.show(); return 0; }

[5_19]实现本题功能的程序如下: #include #include const int l=80; class person { public:

void input()

{ cout<<\ cin>>name;

cout<<\ cin>>id; }

void print()

{ cout<<\ } void printname()

{ cout<

char name[l],id[l]; };

class stud { public:

void input()

{ cout<<\ cin>>addr;

cout<<\ cin>>tel; }

void print()

{ cout<<\ cout<<\ } private:

char addr[l]; char tel[l]; };

class student:private person { public:

void input()

{ person::input();

cout<<\ cin>>old;

cout<<\ cin>>sno; }

void print()

{ person::print();

cout<<\ cout<<\ }

void printname()

{ person::printname(); } private:

int old;

unsigned long sno; };

class score:private student,private stud { public:

void input()

{ student::input(); stud::input();

cout<<\ cin>>math;

cout<<\

联系合同范文客服:xxxxx#qq.com(#替换为@)