C++实验2

上传人:pu****.1 文档编号:509519028 上传时间:2024-02-02 格式:DOC 页数:13 大小:236.50KB
返回 下载 相关 举报
C++实验2_第1页
第1页 / 共13页
C++实验2_第2页
第2页 / 共13页
C++实验2_第3页
第3页 / 共13页
C++实验2_第4页
第4页 / 共13页
C++实验2_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《C++实验2》由会员分享,可在线阅读,更多相关《C++实验2(13页珍藏版)》请在金锄头文库上搜索。

1、 实验二 类和对象实验课程名:面向对象程序设计(C+)专业班级: 09计算机科学与技术(2)班 学号: 姓名: 实验时间: 实验地点: K4-102 指导教师: 一、实验目的和要求(1) 掌握派生类的声明方法和派生类构造函数的定义方法。(2) 掌握不同继承方式下,基类成员在派生类中的访问属性。(3) 掌握在继承方式下,构造函数与析构函数的执行顺序与构造规则。(4) 学习虚基类在解决二义性问题中的作用。二、实验内容(一)输入下列程序#includeusing namespace std;class Basepublic:void setx(int i)x=i;int getx()return x

2、;public:int x;class Derived:public Basepublic:void sety(int i)y=i;int gety()return y;void show()coutBase:x=xendl;public:int y;int main()Derived bb;bb.setx(16);bb.sety(25);bb.show();coutBase:x=bb.xendl;coutDerived:y=bb.yendl;coutBase:x=bb.getx()endl;coutDerived:y=bb.gety()endl;return 0;(1) 写出程序的运行结果。

3、(2) 按以下要求,对程序进行修改后再调试,指出调试中出错的原因。将基类Base中数据成员x的访问权限改为private时,会出现哪些错误?为什么?将基类Base中数据成员x的访问权限改为protected时,会出现哪些错误?为什么?在源程序的基础上,将派生类Derived的继承方式改为private时,会出现哪些错误?为什么?在源程序的基础上,将派生类Derived的继承方式改为protected时,会出现哪些错误?为什么?解答:(1) 运行结果:(2) 调试中出错的原因:将基类Base中数据成员x的访问权限改为private时,出现错误“error C2248: x : cannot ac

4、cess private member declared in class Base”。在公有继承中,内部访问和对象访问均不能访问基类Base的私有成员x。将基类Base中数据成员x的访问权限改为protected时,出现错误“error C2248: x : cannot access private member declared in class Base”。在公有继承中,对象访问不能访问基类Base的保护成员x。在源程序的基础上,将派生类Derived的继承方式改为private时,出现错误“error C2248: x : cannot access private member d

5、eclared in class Base”。在私有继承中,对象访问不能访问基类Base的公有成员x,setx(int i),getx( )。在源程序的基础上,将派生类Derived的继承方式改为protected时,出现错误“error C2248: x : cannot access private member declared in class Base”。在保护继承中,对象访问不能访问基类Base的公有成员x,setx(int i),getx( )。(二)编写一个学生和教师的数据输入和显示程序。学生数据有编号、姓名、性别、年龄、系别和成绩,教师数据有编号、姓名、性别、年龄、职称和部门

6、。要求将编号、姓名、性别、年龄的输入和显示设计成一个类Person,并作为学生类Student和教师类Teacher的基类。供参考的类结构如下:class Person.;class Student:public Person.;class Teacher:public Person.; (1)程序代码#include #include using namespace std;class personprivate:int no; string name; string ssex; int age;public:void input() coutnoname; coutssexage; voi

7、d display() cout编号:noendl; cout姓名:nameendl; cout姓别:ssexendl; cout年龄:ageendl; ;class student:public personprivate:int bh,score; public:void get()input(); coutbhscore; void show() display(); cout班号:bhendl; cout成绩:scoreendl;class teacher:public personprivate:string zc,bm; public: void get() input(); co

8、utzcbm; void show( ) display(); cout职称:zcendl; cout部门:bmendl;int main( )student s;teacher t; s.get( ); s.show( ); t.get( ); t.show( ); return 0;(2) 程序运行结果:(3) 程序分析这个程序主要是讲公有继承方式的派生类对基类成员的访问规则。在基类person中有四个私有成员no、name、ssex、age,两个公有函数input()、display()。在派生类Student和teacher中分别新增私有数据成员bh、score和zc、bm,公有成员函

9、数get()、show( )。在两个派生类中调用input()、display()就能输出对象的编号、姓名、性别、年龄。(三)按要求阅读、编辑、编译、调试和运行以下程序。 (1) 阅读、编辑、编译、调试和运行以下程序,并写出程序的运行结果。 #include#includeusing namespace std;class MyArraypublic:MyArray(int leng);MyArray() ;void Input();void Display(string);protected:int *alist;int length;MyArray:MyArray(int leng)if(

10、leng=0)couterror length;exit(1);alist=new int leng;length=leng;if(alist=NULL)coutassign failure;exit(1);coutMyArray类对象已创建。endl;MyArray:MyArray()deletealist;coutMyArray类对象被撤销。endl;void MyArray:Display(string str)int i;int *p=alist;coutstrlength个整数:;for(i=0;ilength;i+,p+)cout*p ;coutendl;void MyArray:

11、Input( )cout请键盘输入length个整数:;int i;int *p =alist;for(i=0;i*p;int main()MyArray a(5);a.Input();a.Display(显示已输入的);return 0;程序运行结果: (2) 声明一个类SortArray继承类MyArray,在该类中定义一个函数,具有将输入的整数从小到大进行排序的功能。程序代码 #include #include using namespace std; class MyArray public: MyArray(int leng); MyArray(); void Input(); vo

12、id Display(string); protected: int*alist; int length; ; MyArray:MyArray(int leng) if(leng=0) couterror length; exit(1); alist=new int leng; length=leng; if(alist=NULL) coutassign failure; exit(1); coutMyArray类对象已创建。endl; MyArray:MyArray() delete alist; coutMyArray类对象被撤销。endl; void MyArray:Display(string str) int i; int *p=alist; coutstrlength个整数:; for(i=0;ilen

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 幼儿/小学教育 > 小学课件

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