收藏 分销(赏)

第三次上机报告.doc

上传人:天**** 文档编号:4543143 上传时间:2024-09-27 格式:DOC 页数:8 大小:121KB 下载积分:6 金币
下载 相关 举报
第三次上机报告.doc_第1页
第1页 / 共8页
第三次上机报告.doc_第2页
第2页 / 共8页


点击查看更多>>
资源描述
实验三 学号: 姓名: 班级:软件工程141 继承与派生(4学时) 一、实验目得 1.学习定义与使用类得继承关系,定义派生类。 2.熟悉不同继承方式下对基类成员得访问控制。 3.学习利用虚基类解决二义性问题。 二、实验任务 1.定义一个基类Animal,有私有整型成员变量age,构造其派生类dog,在其成员函 数SetAge(int n)中直接给age赋值,瞧瞧会有什么问题,把age改为公有成员变量,还会 有问题吗?编程试试瞧。 2.定义一个基类BaseClass,有整型成员变量Number,构造其派生类DerivedClass, 观察构造函数与析构函数得执行情况。 3、 分析并调试以下程序运行结果,并与实际运行结果相对照。 #include <iostream> using namespace std; class Base { int x; public: Base(int i) { x=i; cout<<"Constuctor of Base"<<endl; } ~Base{ cout<<"Destuctor of Base"<<endl; } void show { cout<<"x="<<x<<endl; } }; class Derived:public Base { Base d; public: Derived(int i):Base(i),d(i) { cout<<"Constuctor of Derived"<<endl; } ~Derived { cout<<"Destuctor of Derived"<<endl; } }; int main( ) { Derived obj(5); obj、show; return 0; } 4.定义一个车(vehicle)基类,具有MaxSpeed、Weight等成员变量,Run、Stop等成 员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。自行车(bicycle)类有高度 (Height)等属性,汽车(mot orcar)类有座位数(SeatNum)等属性。从bicycle与motorcar 派生出摩托车(motorcycle)类。 在继承过程中,注意把vehicle设置为虚基类。如果不把vehicle设置为虚基类,会有什么问题?编程试试瞧。 5.设计people(人员)类,考虑到通用性,抽象出所有类型人员都具有得属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。派生出student(学生)类,添加属性:班号char classNO[7];从people类派生出teacher(教师)类,添加属性:职务char principalship[11]、部门char departmentE21]。从student类中派生出graduate(研究生)类,添加属性:专业char subject[21]、导师teacher adviser;从graduate类与teacher类派生出TA(助教生)类,注意虚基类得使用。重载相应得成员函数,测试这些类。 三、实验步骤 1、若将age设置为私有成员,在其派生类中直接给其赋值,会出现错误提示: 将age设置为公有成员时,能正常运行 代码: #include<iostream> using namespace std; class Animal { public: int age; }; class dog:public Animal { public: int setAge(int n) {age=n; return age;} }; int main { dog d; cout<<d、setAge(3)<<endl; return 0; } 运行结果为: 2、执行情况为:(1)基类BaseClass得构造函数; (2)派生类DerivedClass得构造函数; (3)派生类DerivedClass得析取函数; (4)基类BaseClass得析取函数; 代码: #include<iostream> using namespace std; class BaseClass { public: BaseClass { Number=90; cout<<"constructor of BaseClass!"<<endl; } ~BaseClass {cout<<"destructor of BaseClass!"<<endl;} private: int Number; }; class DerivedClass:public BaseClass { public: DerivedClass { cout<<"constrctor of DerivedClass!"<<endl; } ~DerivedClass { cout<<"destructor of DerivedClass!"<<endl; } }; int main { DerivedClass der; return 0; } 执行结果: 3、分析:(1)执行Base得构造函数; (2)执行Derived得对象obj得Base类构造函数,并将obj(5)中参数5赋值给X及Derived类构造函数; (3)执行Derived类得对象obj得Derived得析取函数、在执行Derived类得obj得Base析取函数,最后执行与第一步相对应得Base得析取函数; 执行结果: 4、虚基类:使得在继承间接共同基类时只保留一份成员。 若不使用虚基类,在最终得派生类中会保留该间接共同基类数据成员得多份同名成员。 代码: #include<iostream> using namespace std; class vehicle { public: vehicle(float m,float w) { MaxSpeed=m;Weight=w; } ~vehicle{} void run { cout<<"running of vehicle!"<<endl; } void stop { cout<<"stopping of vehicle!"<<endl; } float MaxSpeed; float Weight; }; class bicycle:virtual public vehicle { public: bicycle(float m,float w,float h):vehicle(m,w) { Height=h; } ~bicycle{} float Height; }; class motorcar:virtual public vehicle { public: motorcar(float m,float w,int s):vehicle(m,w) { SeatNum=s; } ~motorcar{} int SeatNum; }; class motorcycle:public bicycle,public motorcar { public: motorcycle(float m,float w,float h,int s):vehicle(m,w),bicycle(m,w,h),motorcar(m,w,s){} ~motorcycle{} void display { cout<<"MaxSpeed="<<MaxSpeed<<endl; cout<<"Weight="<<Weight<<endl; cout<<"Height="<<Height<<endl; cout<<"SeatNum="<<SeatNum<<endl; } }; void main { float m,w,h; int s; cout<<"please input \nMaxSpeed="; cin>>m; cout<<"Weight="; cin>>w; cout<<"Height="; cin>>h; cout<<"SeatNum="; cin>>s; motorcycle moc(m,w,h,s); moc、display; } 运行结果: 5、代码: #include<iostream> #include<string> using namespace std; class People { public: People(string n,string s,string b,string i) { number=n; sex=s; birthday=b; id=i; }; ~People{} protected: string number; string sex; string birthday; string id; }; class Student:virtual public People { public: Student(string n,string s,string b,string i,string c):People(n,s,b,i) { classNo=c; } ~Student{} protected: string classNo; }; class Teacher:virtual public People { public: Teacher(string n,string s,string b,string i,string p,string d):People(n,s,b,i) { principalship=p; department=d; } ~Teacher{} protected: string principalship;//职务 string department;//部门 }; class Graduate:public Student { public: Graduate(string n,string s,string b,string i,string c,string su,string t):People(n,s,b,i),Student(n,s,b,i,c) { subject=su; teacher_adviser=t; } ~Graduate{} protected: string subject;//专业 string teacher_adviser;//导师 }; class TA:public Graduate,public Teacher { public: TA(string n,string s,string b,string i,string c,string su,string t,string p,string d):People(n,s,b,i),Graduate(n,s,b,i,c,su,t),Teacher(n,s,b,i,p,d) {} ~TA{} void display { cout<<"编号: "<<number<<endl; cout<<"性别: "<<sex<<endl; cout<<"生日: "<<birthday<<endl; cout<<"身份证号: "<<id<<endl; cout<<"班级: "<<classNo<<endl; cout<<"专业: "<<subject<<endl; cout<<"导师: "<<teacher_adviser<<endl; cout<<"导师职务: "<<principalship<<endl; cout<<"导师部门: "<<department<<endl; } }; void main { string n,s,b,i,c,su,t,p,d; cout<<"请输入编号:"<<endl;cin>>n; cout<<"请输入性别:"<<endl;cin>>s; cout<<"请输入生日:"<<endl;cin>>b; cout<<"请输入身份证号:"<<endl;cin>>i; cout<<"请输入班级:"<<endl;cin>>c; cout<<"请输入专业:"<<endl;cin>>su; cout<<"请输入导师:"<<endl;cin>>t; cout<<"请输入导师职务:"<<endl;cin>>p; cout<<"请输入导师部门:"<<endl;cin>>d; TA ta(n,s,b,i,c,su,t,p,d); ta、display; } 运行结果: 四、实验总结: 在本次操作中,前四个题操作较顺利,在做第五题时,调试改正了好久,(1)在多次派生后得初始化时,不懂得对基类进行初始化,导致编译错误;(2)在定义性别,身份证号等时,将其定义为字符型,导致输出时,全乱码,后将其改成字符串形式,输出正确;虽然在后面将其改成字符串形式输出正确,但还就是不太清楚,若用字符型,怎样才能保证不乱码,字符串与字符数组,在什么情况下,如何选择更恰当,这就是后面急需解决得问题。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服