您好,欢迎来到爱站旅游。
搜索
您的当前位置:首页c++类的继承编程练习

c++类的继承编程练习

来源:爱站旅游


实验十六 继承

1、实验目的

1)掌握继承的实现方法;

2)继承中常见问题的处理方法。

2、实验内容

1.分析下面的程序,指出程序运行的结果

#include

class CBase

{public:

void fn1();

};

void CBase::fn1()

{cout<<\"调用基类类的函数fn1()\\n\";

}

class CDerived:public CBase

{

public:

void fn1();

};

void CDerived::fn1()

{cout<<\"调用派生类的函数fn1()\\n\";

}

void main()

{

CDerived d1;

CBase *pb=&d1;

CBase &pd=d1;

d1.fn1();

pb->fn1();

pd.fn1();

}

运行结果:

调用派生类的函数fn1()

调用基类类的函数fn1()

调用基类类的函数fn1()

2.2编写并调试程序:

1)分析下面程序,写出该程序的功能和运行结果

#include

using namespace std;

class A{

private:

int a;

public:

A()

{a=0;}

A(int i)

{a=i;}

void Print()

{cout<};

class B:public A{

private:

int b1,b2;

public:

B()

{b1=0;b2=0;}

B(int i)

{b1=1;b2=0;}

B(int i,int j,int k):A(i),b1(j),b2(k)

{}

void Print()

{

A::Print();

cout<}

};

int main()

{

B ob1,ob2(1),ob3(3,6,9);

ob1.Print();

ob2.Print();

ob3.Print();

return 0;

}

运行结果:

0,0,0

0,1,0

3,6,9

程序功能:

输出空间一点的坐标。

2)定义一个图形类,其中有保护类型的成员数据:高度和宽度,一个公有的构造函数。由该图形类建立两个派生类:矩形类和等腰三角形类。在每个派生类中都包含一个函数area(),分别用来计算矩形和等腰三角形的面积。

#include

using namespace std;

class figure

{

protected:

double height,width;

public:

figure(double=0,double=0);

};

figure::figure(double h,double w)

{

height=h;

width=w;

}

class triangle:public figure

{

public:

double area();

triangle(double=0,double=0);

};

triangle::triangle(double h,double w):figure(h,w)

{

height=h;

width=w;

}

double triangle::area()

{

return 0.5*height*width;

}

class rectangle:public figure

{

public:

double area();

rectangle(double=0,double=0);

};

rectangle::rectangle(double h,double w):figure(h,w)

{

height=h;

width=w;

}

double rectangle::area()

{

return height*width;

}

int main()

{

triangle tri(2,3);

rectangle rec(2,3);

cout<<\"The area of triangle is:\"<cout<<\"The area of rectangle is:\"<return 0;

}

3)编写一个程序计算出圆和圆柱体的表面积和体积。

要求:

(1) 定义一个点(point)类,包含数据成员x,y(坐标点),以它为基类,派生出一个circle类(圆类),增加数据成员(半径)r,再以circle作为直接基类,派生出一个cylinder

(圆柱体)类,再增加数据成员h(高)。设计类中数据成员的访问属性。

(2) 定义基类的派生类圆、圆柱都含有求表面积和体积的成员函数和输出函数。

(3) 定义主函数,求圆、圆柱的面积和体积。

#include

using namespace std;

const double PI=3.141592653;

class point

{

protected:

double X,Y;

public:

point(double x=0,double y=0)

{X=x;Y=y;}

void getXY()

{

cout<<\"Please input X:\";

cin>>X;

cout<<\"Please input Y:\";

cin>>Y;

}

void show()

{

cout<<\"The coordinate of the point is:(\"<}

};

class circle:protected point

{

protected:

double radius;

public:

circle(double x=0,double y=0,double r=0):point(x,y)

{X=x;Y=y;radius=r;}

void getXYradius()

{

circle::getXY();

cout<<\"Please input radius:\";

cin>>radius;

}

double area()

{return PI*radius*radius;}

};

class cylinder:protected circle

{

protected:

double height;

public:

cylinder(double x=0,double y=0,double r=0,double h=0):circle(x,y,r)

{X=x;Y=y;radius=r;height=h;}

void getdata()

{

circle::getXYradius();

cout<<\"Please input the height:\";

cin>>height;

}

double area()

{

return PI*radius*radius*2+2*PI*radius*height;

}

double V()

{

return PI*radius*radius*height;

}

};

int main()

{

circle ci;

cylinder cy;

ci.getXYradius();

cy.getdata();

cout<<\"The area of the circle is:\"<cout<<\"The area of the cylinder is:\"<cout<<\"The volume of the cylinder is:\"<}

实验总结:

1.个人感觉面向对象比面向过程更加直观。

2.就是因为面向对象的思路是用程序语言直接描述客观世界,它程序的长度会更长,但是一个大程序会被分割成多个小部分,每一部分分开编程,然后再调试各个部分的连接情况,当程序比较大的时候,工作量其实相对更小。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- azee.cn 版权所有 赣ICP备2024042794号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务