C++上机实验5and7.docx
- 文档编号:5987759
- 上传时间:2023-01-02
- 格式:DOCX
- 页数:7
- 大小:16.89KB
C++上机实验5and7.docx
《C++上机实验5and7.docx》由会员分享,可在线阅读,更多相关《C++上机实验5and7.docx(7页珍藏版)》请在冰豆网上搜索。
C++上机实验5and7
C++面向对象程序设计实验五:
派生类与继承
实验目的:
(1)学习类的继承,能够定义和使用类的继承关系。
(2)学习单继承派生类的声明与定义方法。
(3)熟悉派生类在私有继承、公有继承、保护继承这三种方式下对基类成员的访问规则。
(4)知道单继承派生类构造函数和析构函数的执行次序,熟悉单继承派生类构造函数和析构函数的构造方法。
(5)学习多重继承派生类的定义方法。
(6)知道多重继承派生类构造函数和析构函数的执行次序,熟悉多重继承派生类构造函数和析构函数的定义方法,理解虚基类和非虚基类的差别。
实验内容:
1、定义一个Rectangle类,它包含两个数据成员length和width;以及包含用于求长方形面积的area成员函数。
再定义Rectangle的派生类Cuboid,它包含一个新的数据成员height和用于求长方体体积的成员函数Vol。
在main函数中,使用这两个类,求某个长方形的面积和某个长方体的体积。
#include"stdafx.h"
#include
usingnamespacestd;
classRectangle
{doublelength,width;
public:
Rectangle(doublelength1,doublewidth1)
{length=length1;
width=width1;}
doublearea()
{returnlength*width;}
};
classCuboid:
publicRectangle
{doubleheight;
public:
Cuboid(doublelength1,doublewidth1,doubleheight1):
Rectangle(length1,width1)
{height=height1;}
doubleVol()
{returnarea()*height;}//
};
intmain()
{Rectanglerec(12,15);
Cuboidcuboid(25,16,20);
cout<<"therectangle'sareais";
cout< cout< cout<<"thecuboid'svolumeis"; cout< return0; } 2、程序填空: 定义一个Person类,它包含三个数据成员name、age、sex;以及用于显示数据成员值的show成员函数。 再定义Person的派生类Teacher,它包含两个新的数据成员department(部门)、salary(月薪),和用于显示数据成员值的show成员函数。 再定义Person的派生类Student,它包含两个新的数据成员code(学号)、score(分数),和用于显示数据成员值的show成员函数。 再定义Teacher类和Student类的多重继承派生类TStudent(例如: 由正在攻读博士学位的在职教师构成的类)。 在main函数中,使用这四个类,分别输出此四个类的某个对象的全部数据成员的值。 给出部分实现代码如下: //shiyan5.cpp: 定义控制台应用程序的入口点。 // #include"stdafx.h" #include #include usingnamespacestd; classPerson { protected: //若将数据成员设为私有的,在本题中可能比较麻烦 stringname;//姓名 intage;//年龄 charsex;//性别 public: Person(stringname1,intage1,charsex1): name(name1),age(age1),sex(sex1) //构造函数定义 { cout<<"Personconstructing..."< } ~Person(){cout<<"Persondestructing..."< voidshow() { cout<<"name: "< cout<<"age: "< cout<<"sex: "< } }; classTeacher: publicvirtualPerson {protected: stringdepartment; doublesalary; public: Teacher(stringname1,intage1,charsex1,stringdepartment1,doublesalary1): Person(name1,age1,sex1) {department=department1; salary=salary1; cout<<"teacherbase1constructing..."< } ~Teacher() {cout<<"teacherbase1constructing..."< voidshow() {Person: : show(); cout<<"department: "< cout<<"salary: "< } }; /*()此处要定义Teacher类的代码,在此省略了包含两个新的数据成员code(学号)、 score(分数),和用于显示数据成员值的show成员函数*/ classStudent: publicvirtualPerson {protected: stringcode; doublescore; public: Student(stringname1,intage1,charsex1,stringcode1,doublescore1): Person(name1,age1,sex1) {code=code1; score=score1; cout<<"studentbase2constructing..."< } ~Student() {cout<<"studentbase2constructing..."< voidshow() {Person: : show(); cout<<"code: "< cout<<"score: "< } }; /*()此处要定义Student类的代码,在此省略了*/ classTStudent: publicStudent,publicTeacher {public: TStudent(stringname1,intage1,charsex1,stringdepartment1,doublesalary1,stringcode1,doublescore1): Person(name1,age1,sex1), Teacher(name1,age1,sex1,department1,salary1), Student(name1,age1,sex1,code1,score1) {cout<<"thetstudentderivedconstructing..."< } ~TStudent() {cout<<"thetstudentderiveddestructing..."< voidshow() {Person: : show(); cout<<"department: "< cout<<"salary: "< cout<<"code: "< cout<<"score: "< } }; /*()此处要定义TStudent类的代码,在此省略了再定义Teacher类和Student类的多重继承派生类TStudent(例如: 由正在攻读博士学位的在职教师构成的类)。 */ intmain() { Personp1("刘德华",40,'M');//Person类对象 p1.show(); cout< Teachert1("赵薇",28,'F',"导演系",10000);//Teacher类对象 t1.show(); cout< Students1("宁静",35,'F',"200803040520",95);//Student类对象 s1.show(); cout< TStudentts1("黎明",30,'M',"通信工程系",2600,"200803040518",90);//TStudent对象 ts1.show(); cout< system("pause"); return0; } 说明: (1)请大家按学号来坐,便于考勤和管理。 (2)请珍惜宝贵的实验时间! 不要做与实验无关的事情,比如聊QQ、上网或打游戏。 (3)直接把C++代码粘贴到实验题目word文档的正确位置,上交实验报告时只交word文档。 (4)word文档名称: 班级+学号后3位+姓名+实验编号,例如10通1班160张山实验五.doc。 C++面向对象程序设计实验七: 函数模板和类模板 实验目的: (7)理解模版的作用。 (8)掌握函数模版的定义方法和模版函数的生成方法。 (9)掌握类模版的定义方法和模版类的生成方法。 实验内容: 1、已知下列主函数 intmain() {inta[5]={1,2,3,4,5}; floatb[6]={1.1,2.2,3.3,4.4,5.5,6.6} cout< cout< return0; } 请声明对有n个元素的数组求平均值的函数模板average。 #include usingnamespacestd; template int1average(int1*x,inty) {int1temp=0; for(inti=0;i {temp+=x[i]; } returntemp/y; }; intmain() {inta[5]={1,2,3,4,5}; floatb[6]={1.1,2.2,3.3,4.4,5.5,6.6}; cout< cout< return0; } /*2、多文件工程示例程序教师演示: */ 说明: (1)请大家按学号来坐,便于考勤和管理。 (2)请珍惜宝贵的实验时间! 不要做与实验无关的事情,比如聊QQ、上网或打游戏。 (3)直接把C++代码粘贴到实验题目word文档的正确位置,上交实验报告时只交word文档。 (4)word文档名称: 班级+学号后3位+姓名+实验编号,例如10通1班160张山实验七.doc。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 上机 实验 and7
![提示](https://static.bdocx.com/images/bang_tan.gif)