《C++程序设计实验四-类的继承与派生(共12页).docx》由会员分享,可在线阅读,更多相关《C++程序设计实验四-类的继承与派生(共12页).docx(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上成绩批阅日期台州学院电子与信息工程学院实验报告班级 学号 姓名 吴前斌 同组 实验课程: C+程序设计 实验项目: 类的继承与派生 实验日期: 2019 年 5 月 5 日专心-专注-专业主要内容(参考)预习简要(可选) 四、实验内容、方法、步骤一、实验目的和任务 五、实验数据记录与处理二、实验原理 六、实验结果分析、思考、心得三、实验器材 七、原始数据一、 实验目的1. 掌握继承与派生的关系及方法;2. 在派生类构造函数中初始化基类成员;3. 多重继承的方法。二、 实验内容1. 定义1个CPerson类,其属性有:姓名、性别和年龄。2. 从CPerson类派生出CS
2、tudent类,增加属性:学号、入学时间和入学成绩;3. 从CPerson类派生出CTeacher类,添加属性:职务、部门和工作时间;4. 由CStudent类派生出CGraduate类,添加属性:研究方向和导师;5. 由CGraduate和CTeacher共同派生出在职研究生类CGradonWork6. 对上述类分别定义其中的构造函数和输出函数,并在主函数中定义对象进行测试,测试的代码大致如下:CGradonWork gradonWork(tzcer,男,33,15,2003,335,computer,tangmin,jsj,sxxy,2002-8);gradonWork.Print();其
3、中CGradonWork 的构造函数参数分别对应的属性是一个在职的研究生对象,其姓名为tzcer,性别为男,33岁,学号15,2003年读在职,入学成绩335,就读计算机方向,其导师tangmin,同时tzcer也是sxxy部门,jsj专业的老师,2002-8月入职。 要求记录输出信息并说明实验步骤和解决方法!7、 问题:何为继承二义性,在本实验中哪个地方存在二义性,你是如何解决的?8、 定义形状类,其中基类为点类,圆类继承点类,圆柱体类继承圆类,计算面积、体积。9、 建立一个建筑物类的层次体系。其中,基类building包括保护数据成员name(建筑物名称)、floors(层数)和areas
4、(总面积),由building类派生住宅类house和办公楼类office;住宅类house包括私有数据成员rooms(房间数)和balcony(阳台数);办公楼类office包括私有数据成员offices(办公室数)和meetingrooms(会议室数)。住宅类house和办公楼类office都含有构造函数和print函数,以分别用于对数据成员的初始化和输出。在主函数中,实例化house类和office类对象并且将其数据输出。10、 某校每位学生都要学习英语、语文、数学三门公共课程以及不同的专业课程。会计学专业要学习会计学和经济学两门课程,化学专业要学习有机化学和化学分析两门课程。编程序管理
5、学生成绩,计算公共课的总分和平均分,以及所有课程的总成绩。数据记录:1、#include#include using namespace std;class CDate public: int x,n,e; CDate(); CDate(int x,int n,int e):x(x),n(n),e(e);class CPerson public: string name,sex; int age; CPerson(); CPerson(string name,int age,string sex):name(name),age(age),sex(sex) void Print() coutna
6、me age sexendl; ;class CStudent:virtual public CPersonpublic: string id; CDate school; int grade; CStudent(); CStudent(string name,int age,string sex,string id,CDate school,int grade): CPerson(name,age,sex),id(id),school(school),grade(grade) void Print() coutname age sex id school.x-school.n-school.
7、e gradeendl; ;class CTeacher:virtual public CPerson public: string duty,depart; CDate work; CTeacher(); CTeacher(string name,int age,string sex,CDate work,string duty,string depart): CPerson(name,age,sex),duty(duty),depart(depart),work(work)void Print() coutname age sex duty depart work.x-work.n-wor
8、k.eendl; ;class CGraduate:public CStudent public: string study,tutor; CGraduate(); CGraduate(string name,int age,string sex,string id,CDate school,int grade,string study,string tutor):CStudent(name,age,sex,id,school,grade),CPerson(name,age,sex),study(study),tutor(tutor)void Print() coutname age sex
9、id school.x-school.n-school.e grade study tutorendl; ;class CGradOnWork:public CGraduate,public CTeacher public: string name,sex; int age; CGradOnWork(); CGradOnWork(string name,int age,string sex,string duty,string depart,CDate work,string id,CDate school,int grade,string study,string tutor): CTeac
10、her(name,age,sex,work,duty,depart),CGraduate(name,age,sex,id,school,grade,study,tutor),CPerson(name,age,sex),name(name),age(age),sex(sex)void Print() coutname age sex duty depart work.x-work.n-work.e id school.x-school.n-school.e grade study tutorendl; ;int main() CGradonWork gradonWork(tzcer,男,33,1
11、5,2003,335,computer,tangmin,jsj,sxxy,2002-8); s1.Print(); s1.CTeacher:Print(); s1.CGraduate:Print(); s1.CStudent:Print(); s1.CPerson:Print();8、#include#includeusing namespace std;class Point protected:double x, y; public: Point()x = 0;y = 0;Point(double x0,double y0) x = x0; y = y0; double get_x()re
12、turn x;double get_y()return y;friend ostream & operator (ostream &out,Point &d); ostream & operator(ostream &out,Point &d)return out Point:( d.x , d.y ) endl;class Circle : public Pointprotected:double r;public:Circle()r = 0;Circle (double r0):r(r0)void set_r(double r1)r = r1;double get_r()return r;
13、double area()return 3.14159 * r * r;friend ostream & operator (ostream &out, Circle &r);ostream & operator(ostream &out,Circle &r)return out r: r.get_r() endl area: r.area() endl;class Cylinder : public Circleprivate:double h;public:Cylinder ()h = 0;Cylinder (double r0, double h0):Circle(r0), h(h0)v
14、oid set_h(double h1)h = h1;double get_h()return h;double area()return 2 * 3.14159 * r * h + 2 * 3.14159 * r * r;double volume()return Circle:area() * h;friend ostream & operator (ostream &out, Cylinder &h);ostream & operator(ostream &out,Cylinder &h)return out h: h.get_h() endl area: h.area() endl v
15、olume: h.volume() endl;int main()Point ps(-2,5);cout ps; Circle o(2);cout o;Cylinder h(2,4);cout h;system(pause);return 0;运行的效果如下:9、#include#includeusing namespace std;class buildingprotected:string name;int floors;float areas;public:building(string,int,float);void print();building:building(string q
16、,int m,float s)name=q;floors=m;areas=s;void building:print()coutname:namefloors:floorsarea:areasendl;class house:public buildingpublic:house(string q,int m,float s,int i,int j):building(q,m,s)rooms=i;balcony=j;void print();private:int rooms;int balcony;void house:print()coutname:namefloors:floorsare
17、as:areasrooms:roomsbalcony:balconyendl;class office:public buildingpublic:office(string q,int m,float s,int a,int b):building(q,m,s)offices=a;meetingrooms=b;void print();private:int offices;int meetingrooms;void office:print()coutname:namefloors:floorsareas:areasoffices:officesmeetingrooms:meetingro
18、omsendl;int main()house q1(住宅,8,520,20,17);office q2(办公室,26,530,5,6);q1.print();q2.print();return 0;运行的效果如下:10、#include#includeusing namespace std;class comFinalprotected:char name20;int English,Chinese,Math,Total;float average;public:comFinal(char *n,int Eng,int Chi,int Mat);comFinal();comFinal();c
19、har *getName()return name;int getEng()return English;int getChi()return Chinese;int getMat()return Math;void setEng(int x)English=x;void setChi(int x)Chinese=x;void setMat(int x)Math=x;int getTotal()return English+Chinese+Math;float getAverage()return (float)(English+Chinese+Math)/3);void show();com
20、Final:comFinal(char *n,int Eng,int Chi,int Mat)English=Eng;Chinese=Chi;Math=Mat;strcpy(name,n);void comFinal:show()cout学生姓名:getName()endl;cout英语成绩:getEng()endl;cout语文成绩:getChi()endl;cout数学成绩:getMat()endl;cout基础分总评:getTotal()endl;cout基础课平均成绩:getAverage()endl;class account:public comFinalprotected:int
21、 accoun;int econ;int majtotal;float majave;public:account(char *n,int Eng,int Chi,int Mat,int acc,int eco);account();int getmajtotal()return econ+accoun;float getmajave()return float(accoun+econ)/2);int getaccount()return accoun;int getecon()return accoun;void setaccount(int x)accoun=x;void setecon(
22、int x)econ=x;void show();account:account(char *n,int Eng,int Chi,int Mat,int acc,int eco):comFinal(n,Eng,Chi,Mat)econ=eco;accoun=acc;void account:show()comFinal:show();cout会计学成绩:accounendl;cout经济学成绩:econendl;cout总分:getTotal()+accoun+econendl; class chemistry:public comFinalprotected:int chemistr;int
23、 analy;int majtatal;float majave;public:chemistry(char *n,int Eng,int Chi,int Mat,int chem,int anal);chemistry();int getmajtotal()return analy+chemistr;float getmajave()return float(chemistr+analy)/2);int getche()return chemistr;int getanl()return analy;void setche(int x)chemistr=x;void setanl(int x
24、)analy=x;void show();chemistry:chemistry(char *n,int Eng,int Chi,int Mat,int chem,int anal):comFinal(n,Eng,Chi,Mat)chemistr=chem;analy=anal;void chemistry:show()comFinal:show();cout有机化学成绩:chemistrendl;cout化学分析成绩:analyendl;cout总分:getTotal()+chemistr+analyendl;int main()account a1(cz,90,80,90,80,90);chemistry c1(zc,80,90,80,90,80);a1.show();cout-endl;c1.setanl(100);c1.show();运行的效果如下图所示:实验心得:本次实验用了C+函数定义函数和函数重载,以及相关类的知识点,同时得掌握使用debug进行对函数的调试。相比之前学的C语言来说这个和之前还是有些区别的,接下来应该多花些时间学好该门课程。 教师签名: