实验三 派生类与继承.doc

上传人:ni****g 文档编号:544862114 上传时间:2023-05-08 格式:DOC 页数:12 大小:126.51KB
返回 下载 相关 举报
实验三 派生类与继承.doc_第1页
第1页 / 共12页
实验三 派生类与继承.doc_第2页
第2页 / 共12页
实验三 派生类与继承.doc_第3页
第3页 / 共12页
实验三 派生类与继承.doc_第4页
第4页 / 共12页
实验三 派生类与继承.doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《实验三 派生类与继承.doc》由会员分享,可在线阅读,更多相关《实验三 派生类与继承.doc(12页珍藏版)》请在金锄头文库上搜索。

1、实验三 派生类与继承一、实验目的1、学习类的继承,能够定义和使用类的继承关系。2、学习派生类的声明与定义方法。3、掌握类的定义和对象的声明。4、熟悉公有派生和私有派生的访问特性。5、掌握派生类构造函数和析构函数的执行顺序。6、掌握利用访问声明调整基类成员在派生类中的访问属性。二、试验内容1、下面的程序可以输出ASCII字符与所对应的数字的对照表。修改下列程序,使其可以输出字母a到z(或任意两个字符间)与所对应的数字的对照表。class table public: table(int p) i=p; void ascii(void); protected: int i;void table:as

2、cii(void)int k=1;for (;i127;i+)coutsetw(4)i (char)i;if(k)%12=0)coutn;k+;coutn;class der_table:public tablepublic:der_table(int p,char *m):table(p)c=m;void print(void);protected:char *c;void der_table:print(void)coutcn;table:ascii();int main()der_table obl(32,ASCII value-char);obl.print();return 0;提示

3、:修改后的主程序为:int main()der_table ob(a,z,ASCII value-char);ob.print();return 0;#includeclass table public: table(int p) i=p; void ascii(void); protected: int i;void table:ascii(void)int k=1;for (;i123;i+)couti(char)i ;if(k)%12=0)coutendl;k+;coutendl;class der_table:public tablepublic:der_table(int p,cha

4、r *m):table(p)c=m;void print(void);protected:char *c;void der_table:print(void)coutcendl;table:ascii();int main()der_table ob(a,ASCII value-char);ob.print();return 0;2、已有类Time和Date,要求设计一个派生类Birthtime,它继承类Time和Date,并且增加一个数据成员Childname用于表示小孩的名字,同事设计主程序显示一个小孩的出生时间和名字。#include#includeclass Time public:T

5、ime(int h,int m, int s)hours=h;minutes=m;seconds=s;void display()cout出生时间:hours时minutes分seconds秒endl;protected:int hours,minutes,seconds;class Date public: Date(int m,int d,int y) month=m; day=d; year=y; void display() cout出生年月:year年month月day日endl; protected: int month,day,year; ;class Birthtime:pub

6、lic Date,public Time public:Birthtime(char *name,int mon,int d,int y,int h,int m,int s):Date(mon,d,y),Time(h,m,s)Childname=new charstrlen(name)+1;strcpy(Childname,name);Birthtime()delete Childname;void print()cout小孩的名字是:Childnameendl;Date:display();Time:display();private:char *Childname;void main()

7、Birthtime a(lihua,12,2,1993,8,34,23); a.print();3、编写一个学生和教师的数据输入和显示程序。学生数据有编号、姓名、性别、年龄、系别和成绩,教师数据有编号、姓名、性别、年龄、职称和部门。要求将编号、姓名、性别、年龄的输入和显示设计成一个类Person,并作为学生类Student和教师类Teacher的基类。#includeclass Personchar *name;char *sex;int age;public:Person(char *n,char *s,int a)name=n;sex=s; age=a;Person()delete nam

8、e;void print()cout姓名:name性别:sex年龄:ageendl;class Student:public Person char *sno;char *xi;double fen;public:Student(char *sn,char *x,double f,char *n,char *s,int a):Person(n,s,a) sno=sn; xi=x; fen=f;void print()cout学生的编号:sno学生的系列:xi学生的成绩:fenendl;Person:print();class Teacher:public Personchar *num;cha

9、r *job; int depart;public:Teacher(char *nu,char *j,int d,char *n,char *s,int a):Person(n,s,a)num=nu;job=j;depart=d;void print()cout老师的编号:num老师的职称:job老师的部门:departendl;Person:print();int main()Student m(122515,ji,98,li,gril,21);m.print();Teacher b(12,math,98,wang,gril,35);b.print();return 0;4、给出下面的基类:

10、class area protected: double height; double width; public: area(double h,double w) height=h; width=w; virtual double getarea()=0;【要求】(1)建立基类area的俩个派生类rectangle与isosceles,让每一个派生类都包含一个函数getarea(),分别用来返回矩形与三角形的面积。用构造函数对height与width进行初始化。(2)写出主程序,用来求height与width分别为10.0与5.0的矩形面积,以及求height与width分别为4.0与6.0的三角形面积。(3)要求通过使用基类指针访问虚函数的方法(即运行时的多态性)分别求出矩形和三角形面积。#includeclass area protected: double height; double width; public: area(double h,double w) height=h; width=w;

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号