《28,刘小虎—C++实验二》由会员分享,可在线阅读,更多相关《28,刘小虎—C++实验二(36页珍藏版)》请在金锄头文库上搜索。
1、实验实验 2 派生类与继承派生类与继承实验课程名:实验课程名:高级语言程序设计高级语言程序设计专业班级:专业班级: 1212 网工网工 2 2 班班 学号:学号: 201240420228201240420228 姓名:姓名: 刘小虎刘小虎 实验时间:实验时间: 4.20-5.54.20-5.5 实验地点:实验地点: K4-201K4-201 指导教师:指导教师: 祁文青祁文青 一一实验目的和要求实验目的和要求(1) 掌握派生类的声明方法和派生类构造函数的定义方法。(2) 掌握不同继承方式下,基类成员在派生类中的访问属性。(3) 掌握在继承方式下,构造函数与析构函数的执行顺序与构造规则。(4)
2、 学习虚基类在解决二义性问题中的作用。2.2 实验内容与步骤1.输入下列程序。/test4_1.cpp#includeusing namespace std;class Basepublic:void setx(int i)x=i;Int getx()return x;public:int x;class Derived:public Basepublic:void sety(int i)y=i;int gety()return y;void show()cout#includeusing namespace std;class Personpublic:void get_value()cin
3、numnamesexage;void display()coutxibiescore;void display_1()couttitlebumen;void display_2()coutmn;Student *stud=new Studentn;for(i=0;i#includeusing namespace std;class MyArraypublic:MyArray(int leng);MyArray;void Input();void Display(string);protected:int*alist;int length;MyArray:MyArray(int leng)if(
4、leng*p;int main()MyArray a(5);a.Input();a.Display(“显示已输入的”);return 0;(2) 声明一个类 SortArray 继承类 MyArray,在该类中定义一个函数,具有将输入的整数从小到大进行排序的功能。【提示】在第(1)步的基础上可增加下面的参考框架:class SortArray : public MyArray public:void Sort();SortArray(int leng):MyArray(leng)cout#includeusing namespace std;class MyArraypublic:MyArra
5、y(int leng);MyArray();void Input();void Display(string);protected:int*alist;int length;MyArray:MyArray(int leng)if(leng*p;class SortArray : public MyArray public:void Sort();SortArray(int leng):MyArray(leng)cout*(a+j+1) temp=*(a+j);*(a+j)=*(a+j+1);*(a+j+1)=temp;int main()SortArray a(5);a.Input();a.D
6、isplay(“显示排序以前的“);a.Sort();a.Display(“显示排序以后的“);cout#includeusing namespace std;class MyArraypublic:MyArray(int leng);MyArray();void Input();void Display(string);protected:int*alist;int length;MyArray:MyArray(int leng)if(leng*p;class ReArray : public MyArray public:void reverse();ReArray(int leng):M
7、yArray(leng)cout#includeusing namespace std;class MyArraypublic:MyArray(int leng);MyArray();void Input();void Display(string);protected:int*alist;int length;MyArray:MyArray(int leng)if(leng*p; class AverArray : public MyArraypublic:double Aver();AverArray(int leng):MyArray(leng)cout#includeusing nam
8、espace std;class MyArraypublic:MyArray(int leng);MyArray();void Input();void Display(string);protected:int*alist;int length;MyArray:MyArray(int leng)if(leng*p; class SortArray :virtual public MyArray public:void Sort();SortArray(int leng):MyArray(leng)cout*(a+j+1) temp=*(a+j);*(a+j)=*(a+j+1);*(a+j+1
9、)=temp;class ReArray :virtual public MyArray public:void reverse();ReArray(int leng):MyArray(leng)cout“ReArray 类对象已创建。“endl;virtual ReArray();ReArray:ReArray()cout“ReArray 类对象被撤销。“endl;void ReArray: reverse ()int i,j,m,temp;m=(length-1)/2;int *p=alist;for(i=0;i=m;i+)j=length-i-1;temp=*(p+i);*(p+i)=*
10、(p+j);*(p+j)=temp;class AverArray :virtual public MyArraypublic:double Aver();AverArray(int leng):MyArray(leng)cout“AverArray 类对象已创建。“endl;virtual AverArray();AverArray:AverArray()cout“AverArray 类对象被撤销。“endl;double AverArray:Aver()double m=0;for(int i=0;ilength;i+)m=alisti+m;cout“显示“length“个整数的平均值:“
11、;return m/length;class NewArray:public SortArray,public ReArray,public AverArraypublic:void fun();NewArray(int leng):SortArray(leng),ReArray(leng),AverArray(leng),MyArray(leng)cout“NewArray 类对象已创建。“endl;virtual NewArray();NewArray:NewArray()cout“ReArray 类对象被撤销。“endl;void NewArray: fun ()Sort();Displ
12、ay(“显示排序以后的“);reverse ();Display(“显示排序以后的“);Aver();int main()NewArray s(5);s.Input();s.Display(“显示“);couts.fun()endl;cout“201240420228,刘小虎“endl;return 0;三、结论(写本次实验的收获)(写本次实验的收获)通过本次试验我掌握派生类的声明方法和派生类构造函数的定义方法。派生类有三种继承方式,私有继承、公有继承、保护继承。在不同继承方式下,基类成员在派生类中的访问规则不同。在程序中要能准确分辨内部访问和对象访问,判断其对基类成员的访问权限。特别要注意对于基类中的私有成员,这两种访问方式均不可访问。