实验3副本.docx
- 文档编号:14343855
- 上传时间:2023-06-22
- 格式:DOCX
- 页数:31
- 大小:20.06KB
实验3副本.docx
《实验3副本.docx》由会员分享,可在线阅读,更多相关《实验3副本.docx(31页珍藏版)》请在冰点文库上搜索。
实验3副本
实验3---副本
《C++面向对象程序设计》实验报告
实验序号:
3 实验项目名称:
派生类与继承
学 号
姓 名
专业
实验地点
指导教师
实验时间
一、实验目的及要求
(1)掌握派生类的声明方法和派生类构造函数的定义方法;
(2)掌握不同继承方式下,基类成员在派生类中的访问属性;
(3)掌握在继承方式下,构造函数与析构函数的执行顺序与构造规则;
(4)学习虚函数在解决二义性问题中的作用。
二、实验设备(环境)及要求
MicorsoftVisualC++6.0
三、实验内容与步骤(题目、算法和结果描述)
1、输入下列程序。
#include
usingnamespacestd;
classBase{
public:
voidsetx(inti)
{x=i;}
intgetx()
{returnx;}
public:
intx;
};
classDerived:
publicBase{
public:
voidsety(inti)
{y=i;}
intgety()
{returny;}
voidshow()
{cout<<"Base:
:
x="< public: inty; }; intmain() { Derivedbb;//语句2 bb.setx(16);//语句3 bb.sety(25);//语句4 bb.show();//语句5 cout<<"Base: : x="< cout<<"Derived: : y="< cout<<"Base: : x="< cout<<"Derived: : y="< return0; } (1)写出程序的运行结果。 【运行结果截图】: (2)按以下要求,对程序进行修改后再调试,指出调试中出错的原因。 1)将基类Base中的数据成员x的访问权限改为private时,会出现哪些错误,为什么? 【运行结果分析】: 派生类函数不能直接访问基类中的私有成员。 2)将基类Base中的数据成员x的访问权限改为protected时,会出现哪些错误,为什么? 【运行结果分析】: 基类的protected成员在派生类内部的其他成员可以直接访问它们,但是在类外部通过派生类的对象无法访问。 3)在原程序的基础上,将派生类Derived的继承方式改为private时,会出现哪些错误? 为什么? 【运行结果分析】: 基类中的成员通过private方式继承,在派生类中都为私有成员,派生类的其他成员可以直接访问它们,类外的对象都不能直接访问。 4)在原程序的基础上,将派生类Derived的继承方式改为protected时,会出现哪些错误? 为什么? 【运行结果分析】: 基类的public成员和protected成员通过protected继承到派生类中都作为派生类的protected成员,派生类的其他成员可以直接访问它们,但是类的外部使用者不能通过派生类的对象来访问它们。 2、编写一个学生和教师的数据输入和显示程序。 学生数据有编号、姓名、性别、年龄、系别和成绩,教师数据有编号、姓名、性别、年龄、职称和部门。 要求将编号、姓名、性别、年龄的输入和显示设计成一个类Persona,并作为学生类Student和教师类Teacher的基类。 【person类结构】: classPerson{ public: voidinput() {cout<<"Num: "; cin>>number; cout<<"Name: "; cin>>name; cout<<"Sex: "; cin>>sex; cout<<"Age: "; cin>>age; } voidprint() {cout<<"Num: "< cout<<"Name: "< cout<<"Sex: "< cout<<"Age: "< } protected: intnumber,age; stringname,sex; }; 【Student类结构】: classStudent: publicPerson{ public: voidinput1() {cout<<"Pleaseenterthestudent'sinformation: "< Person: : input(); cout<<"Institute: "; cin>>department; cout<<"Score: "; cin>>score; } voidprint1() {cout<<"Thestudent's: "< Person: : print(); cout<<"Institute: "< cout<<"Score: "< } private: stringdepartment; floatscore; }; 【Teacher类结构】: classTeacher: publicPerson{ public: voidinput2() {cout<<"Pleaseentertheteacher'sinformation: "< Person: : input(); cout<<"Title: "; cin>>title; cout<<"Department: "; cin>>section; } voidprint2() {cout<<"Theteacher's: "< Person: : print(); cout<<"Title: "< cout<<"Department: "< } private: stringtitle,section; }; 【运行结果截图】: 3、按要求阅读、编辑、编译、调试和运行以下程序。 (1)阅读、编辑、编译、调试和运行以下程序,并写出程序运行结果。 #include #include usingnamespacestd; classMyArray{ public: MyArray(intleng); ~MyArray(); voidInput(); voidDisplay(string); protected: int*alist; intlength; }; MyArray: : MyArray(intleng) {if(leng<=0) { cout<<"errorlength"; exit (1);} alist=newint[leng]; length=leng; if(alist==NULL) { cout<<"assignfailure"; exit (1);} cout<<"MyArray类对象已创建。 "< } MyArray: : ~MyArray() {delete[]alist; cout<<"MyArray类对象被撤销。 "< } voidMyArray: : Display(stringstr) {inti; int*p=alist; cout< "; for(i=0;i cout<<*p<<""; cout< } voidMyArray: : Input() {cout<<"请从键盘输入"< "; inti; int*p=alist; for(i=0;i cin>>*p; } intmain() { MyArraya(5); a.Input(); a.Display("显示已输入的"); return0; } (2)声明一个类SortArray继承类MyArray,在该类中定义一个函数,具有将输入的整数从小到大进行排序的功能。 【提示】: 请注意编写继承类的构造函数、析构函数,以及修改主函数。 【SortArray类框架】: classSortArray: publicMyArray{ public: voidSort(); SortArray(intleng): MyArray(leng) {len=leng; cout<<"SortArray类对象已创建。 "< } virtual~SortArray(); private: intlen; int*sp; }; SortArray: : ~SortArray() {cout<<"SortArray类对象被撤销。 "< } voidSortArray: : Sort() { intq; sp=alist; for(inti=0;i { for(intj=0;j { if(*(sp+j)>*(sp+j+1)) { q=*(sp+j); *(sp+j)=*(sp+j+1); *(sp+j+1)=q; } } } } 【运行结果截图】: (3)声明一个类ReArray继承类MyArray,在该类中定义一个函数,即有将输入的整数进行倒置的功能。 【提示】: 请注意编写继承类的构造函数、析构函数,以及修改主函数。 【ReArray类框架】: classReArray: publicMyArray{ public: voidReverse(); ReArray(intleng): MyArray(leng) {len=leng; voidReverse(); cout<<"ReArray类对象已创建。 "< virtual~ReArray(); private: intlen; int*bp; }; ReArray: : ~ReArray() {cout<<"ReArray类对象被撤销。 "< } voidReArray: : Reverse() { intq; bp=alist; for(inti=0;i {q=*(bp+i); *(bp+i)=*(bp+len-i-1); *(bp+len-i-1)=q;} } 【运行结果截图】: (4)声明一个类Average继承类MyArray,在该类中定义一个函数,即有将输入的整数进行平均的功能。 【提示】: 请注意编写基继承类的构造函数、析构函数,以及修改主函数。 【Average类框架】: classAverage: publicMyArray{ public: voidAver(); Average(intleng): MyArray(leng) {len=leng; cout<<"Average类对象已创建。 "< virtual~Average(); private: intlen; int*cp; }; Average: : ~Average() {cout<<"Average类对象被撤销。 "< } voidAverage: : Aver() { doubleq=0; cp=alist; for(inti=0;i {q=q+*cp; cp++;} q=q/len; cout<<"Theaverageofthe"< } 【运行结果截图】: (5)声明一个NewArray类,同时继承了类SortArray,ReArray和Average,使得类NewArray的对象同时具有排序、倒置和求平均值的功能。 在继承的过程中声明MyArray为虚基类,体会虚基类在解决二义性问题中的作用。 【NewArray类框架】: classNewArray: publicReArray,publicSortArray,publicAverage {public: NewArray(intleng); ~NewArray(); }; NewArray: : NewArray(intleng): MyArray(leng),SortArray(leng),ReArray(leng),Average(leng) {cout<<"\n新数组正在创建。 \n";} NewArray: : ~NewArray() {cout<<"\n新数组已被清空。 \n";} 【运行结果截图】: 四、分析与讨论(记录实验过程中出现的主要问题和心得体会) 1、查阅课本 2、参考例题 3、看PPT,回忆老师讲课内容 4、不懂的还可以问同学、老师 5、上课认真听 五、教师评语 签名: 日期: 成绩 附: 程序源代码 1、#include usingnamespacestd; classBase{ public: voidsetx(inti) {x=i;} intgetx() {returnx;} public: intx; }; classDerived: publicBase{ public: voidsety(inti) {y=i;} intgety() {returny;} voidshow() {cout<<"Base: : x="< public: inty; }; intmain() { Derivedbb;//语句2 bb.setx(16);//语句3 bb.sety(25);//语句4 bb.show();//语句5 cout<<"Base: : x="< cout<<"Derived: : y="< cout<<"Base: : x="< cout<<"Derived: : y="< return0; } 2、#include #include usingnamespacestd; classPerson{ public: voidinput() {cout<<"Num: "; cin>>number; cout<<"Name: "; cin>>name; cout<<"Sex: "; cin>>sex; cout<<"Age: "; cin>>age; } voidprint() {cout<<"Num: "< cout<<"Name: "< cout<<"Sex: "< cout<<"Age: "< } protected: intnumber,age; stringname,sex; }; classStudent: publicPerson{ public: voidinput1() {cout<<"Pleaseenterthestudent'sinformation: "< Person: : input(); cout<<"Institute: "; cin>>department; cout<<"Score: "; cin>>score; } voidprint1() {cout<<"Thestudent's: "< Person: : print(); cout<<"Institute: "< cout<<"Score: "< } private: stringdepartment; floatscore; }; classTeacher: publicPerson{ public: voidinput2() {cout<<"Pleaseentertheteacher'sinformation: "< Person: : input(); cout<<"Title: "; cin>>title; cout<<"Department: "; cin>>section; } voidprint2() {cout<<"Theteacher's: "< Person: : print(); cout<<"Title: "< cout<<"Department: "< } private: stringtitle,section; }; intmain() {Studentstu; stu.input1(); cout< Teachertea; tea.input2(); stu.print1(); cout< tea.print2(); return0; } 3、 (2)#include #include usingnamespacestd; classMyArray{ public: MyArray(intleng); ~MyArray(); voidInput(); voidDisplay(string); protected: int*alist; intlength; }; MyArray: : MyArray(intleng) {if(leng<=0) { cout<<"errorlength"; exit (1);} alist=newint[leng]; length=leng; if(alist==NULL) { cout<<"assignfailure"; exit (1);} cout<<"MyArray类对象已创建。 "< } MyArray: : ~MyArray() {delete[]alist; cout<<"MyArray类对象被撤销。 "< } voidMyArray: : Display(stringstr) {inti; int*p=alist; cout< "; for(i=0;i cout<<*p<<""; cout< } voidMyArray: : Input() {cout<<"Pleaseinput"< "< inti; int*p=alist; for(i=0;i cin>>*p; } classSortArray: publicMyArray{ public: voidSort(); SortArray(intleng): MyArray(leng) {len=leng; cout<<"SortArray类对象已创建。 "< } virtual~SortArray(); private: intlen; int*sp; }; SortArray: : ~SortArray() {cout<<"SortArray类对象被撤销。 "< } voidSortArray: : Sort() { intq; sp=alist; for(inti=0;i { for(intj=0;j { if(*(sp+j)>*(sp+j+1)) { q=*(sp+j); *(sp+j)=*(sp+j+1); *(sp+j+1)=q; } } } } intmain() { SortArraya(5); a.Input(); a.Display("These"); a.Sort(); a.Display("Aftersorted"); return0; } 3、(3)#include #include usingnamespacestd; classMyArray{ public: MyArray(intleng); ~MyArray(); voidInput(); voidDisplay(string); protected: int*alist; intlength; }; MyArray: : MyArray(intleng) {if(leng<=0) { cout<<"errorlength"; exit (1);} alist=newint[leng]; length=leng; if(alist==NULL) { cout<<"assignfailure"; exit (1);} cout<<"MyArray类对象已创建。 "< } MyArray: : ~MyArray() {delete[]alist; cout<<"MyArray类对象被撤销。 "< } voidMyArray: : Display(stringstr) {inti; int*p=alist; cout< "; for(i=0;i cout<<*p<<""; cout< } voidMyArray: : Input() {cout<<"Pleaseinput"< "< inti; in
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 副本