c++派生类与继承实验报告

上传人:第*** 文档编号:31321001 上传时间:2018-02-06 格式:DOC 页数:28 大小:182KB
返回 下载 相关 举报
c++派生类与继承实验报告_第1页
第1页 / 共28页
c++派生类与继承实验报告_第2页
第2页 / 共28页
c++派生类与继承实验报告_第3页
第3页 / 共28页
c++派生类与继承实验报告_第4页
第4页 / 共28页
c++派生类与继承实验报告_第5页
第5页 / 共28页
点击查看更多>>
资源描述

《c++派生类与继承实验报告》由会员分享,可在线阅读,更多相关《c++派生类与继承实验报告(28页珍藏版)》请在金锄头文库上搜索。

1、 实验 2 派生类与继承实验课程名:面向对象程序设计(C+)专业班级: 学号: 姓名: 实验时间: 实验地点: 指导教师: 2.1 实验目的和要求(1) 掌握派生类的声明方法和派生类构造函数的定义方法。(2) 掌握不同继承方式下,基类成员在派生类中的访问属性。(3) 掌握在继承方式下,构造函数与析构函数的执行顺序与构造规则。(4) 学习虚基类在解决二义性问题中的作用。二、实验内容一、构造一个类Geometry及其派生类,该类主要实现关于几何图形的基本操作。对于基类“几何图形” ,有求面积、求体积的函数(纯虚函数) ,其派生类圆和矩形主要有初始化(构造函数) ,求面积,求周长操作,类圆的派生类圆

2、球和圆柱有求表面积、体积操作。 试在主函数中分别定义圆、圆球、圆柱以及矩形的对象,并调用其成员函数实现其相应操作。CircleradiumsCircle()Circle()BallBall()Ball()GeometryGeometry()Geometry()GetArea()GetPerimeter()Getcolume()show()ColumnColumn()Column()RectangleRectangle()Rectangle()实验代码如下:#includeusing namespace std;class Geometrypublic:Geometry()Geometry()d

3、ouble GetArea()/求面积函数double GetPerimeter()/求体积函数double Getcolume()/求周长函数virtual show();class Circle:public Geometrypublic:Circle(double i)radiums=i;Circle()double GetArea();double Getcolume();double R()return radiums;show();private:double radiums;double Circle:GetArea()double S;S=3.14*radiums*radium

4、s;return S;double Circle:Getcolume()double L;L=2*3.14*radiums;return L;Circle:show()cout#includeusing namespace std;class Point /定义一个点的类,坐标为(x,y)public:Point(double i,double j)x=i;y=j;double x,y;class Linepublic:Line(double x1,double y1,double x2,double y2):p1(x1,y1),p2(x2,y2);double length();privat

5、e:Point p1,p2;double Line:length()return(sqrt(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);class Trianglepublic:Triangle(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5,double x6,double y6):L1(x1,y1,x2,y2),L2(x3,y3,x4,y4),L3(x5,y5,x6,y6);int pand

6、uan();double Area();void show();private:Line L1,L2,L3;int Triangle:panduan()double a=L1.length();double b=L2.length();double c=L3.length();if(a+bc&b+ca&a+cb)return 1;elsereturn 0;double Triangle:Area()double a=L1.length();double b=L2.length();double c=L3.length();double s=(a+b+c)/2;return(sqrt(s*(s-

7、a)*(s-b)*(s-c);void Triangle:show()if(panduan()cout#includeusing namespace std;class Myarrayprotected:int *alist;int length;public:Myarray(int len)alist=new intlen;length=len;Myarray(); void input()int i;for(i=0;ialisti;int getlen()return length;void display()for(int i=0;ialisti+1)t=alisti;alisti=al

8、isti+1;alisti+1=t;cout0 : 1 : 2 : 3其中,0、1、2、3 为队列中的元素,0 是队头。在栈类中实现一个输出栈中内容的函数 printStack,输出格式为:Stack member:| 3 | 2 | 1 | 0 |-其中,3、2、1、0 是栈中元素,3 为栈顶元素。(3)用多文件结构实现程序。三个类的定义放在一个头文件中,类的实现放在另一个源文件中。主程序用于测试你所设计的三个类的正确性。测试内容包括: 在队列中加入几个元素,用 printQueue()打印队列内容,然后再从队列中取出这些元素,看是否正确 在栈中加入几个元素,用 printStack()打印

9、栈的内容,然后再从栈中取出这些元素,看是否正确 测试取队列长度的函数 getQueueLength()的正确性 测试判断栈是否为空的函数 empty()的正确性 实验代码:#includeusing namespace std;struct Nodeint data;Node *next;class LinkListpublic:LinkList(int a,int n);/构造函数LinkList()head=new Node;tail=new Node;head-next=NULL;tail-next=NULL;LinkList();/析构函数 int Length();/求链表长度的函数

10、int puthead();/在头部插入元素的函数int puttail();/在尾部插入元素的函数void emoty();/检查链表是否为空的函数int gethead (); /从表头取出一个元素Node *head,*tail;int length; LinkList:LinkList(int a,int n)int i;Node *p,*q;head=new Node;head-data=a0;p=new Node;p-data=a1;head-next=p;for(i=2;idata=ai;p-next=q;p=q;tail=p;tail-next=NULL;LinkList:Li

11、nkList()Node *p,*q;p=head;while(p)q=p;p=p-next;delete q;int LinkList:Length()Node *p;int i=0;p=head;while(p)p=p-next;i+;return i;int LinkList:puthead()Node *p;p=new Node;coutp-data;p-next=head;head=p;return 0;int LinkList:puttail()Node *p;p=new Node;coutp-data;tail-next=p;tail=p;tail-next=NULL;retur

12、n 0;void LinkList:emoty()if(!head-next)coutnext;return p-data;delete p;class Queue:public LinkListpublic:Queue(int a,int n):LinkList(a,n);void inqueue();/入队函数void outqueue();/出队函数;void Queue:inqueue()LinkList:puttail();void Queue:outqueue()cout;while(head!=NULL)cout;while(head-next)coutLinkList:geth

13、ead():;coutendl;int main()int a10=0,1,2,3,4,5,6,7,8,9;Queue queue(a,10);queue.inqueue();queue.outqueue();Stack stack;stack.outstack();return 0;运行结果:程序分析:定义基类链表其中包含功能能够在链表的头尾增加节点以及在链表尾增加节点 能够记录链表的个数(用静态成员) 能返回链表中的节点个数 能查看链表头节点的元素值 能告知链表是否为空 在链表类的构造函数中初始化链表 在链表类的析构函数中释放链表所有元素的空间 定义派生类队与栈,队列初始化用链表的初始化函数,用 gettail 实现入队操作用gethead 实现从队列的头部出对。栈中初始化用函数 puthead 实现从栈顶入栈,用函数gethead()实现从栈顶出栈。三、结论1)继承可以从基类中获得派生类中不曾定义过的成员,提高了编程效率;2)继承与派生分为共有、私有、保护三种继承方式,其中共有使用最广泛,它使得派生类与基类中的成员具有相同的属性。3)多重继承存在有二义性,用虚基类能有效解决这一问题。4)除了继承还有组合关系,及在一个类中定义另一个类的对象,此时初始化时要用对象名来调用初始化函数。调用对应的函数时,要用对象名调用它的函数。

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

当前位置:首页 > 办公文档 > 其它办公文档

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