南京邮电大学C++实验报告3(仅供参考)

实验三: 多态性实验

一、 实验目的和要求
(1)熟悉类的设计、运用继承与派生机制设计派生类,合理设置数据成员和成员函数。
(2)掌握双目运算符、单目运算符的重载方法,对常用算术运算符能在自定义类中通过友元函数、成员函数进行重载,以实现静态多态性。
(3)掌握通过继承、虚函数、基类的指针或引用实现动态多态性的方法。
(4)理解并掌握有纯虚函数的抽象类的作用,在各派生类中重新定义各纯虚函数的方法,以及此时实现的动态多态性。

二、实验环境(实验设备)
硬件: 微型计算机
软件: Windows 操作系统、Microsoft Visual Studio 2010

三、实验原理及内容
实验题目1:定义点类Point,有两个double 类型的数据成员x 和y,分别表示横坐标和纵
坐标,要求完成如下内容。
(1)定义坐标默认值为原点(0.0,0.0)的构造函数。
(2)以成员函数形式重载:前置“++”运算符和双目运算符“−”。
(3)用友元函数形式重载:双目运算符“+”(两种版本,详见实验指导部分)、插
入运算符。
(4)先根据main()主函数代码和运行结果,补充类的定义和相关函数的定义,写出完
整程序。
(5)程序正确后,删除main()函数体,根据运行结果,自己重新完成main()函数。
main()主函数代码如下。(中文五号宋体,英文五号Consolas字体,单倍行距)
int main()
{
Point pt1(10.5,20.8),pt2(-5.3,18.4),pt3;
cout<<“original pt1,pt2,pt3 are:\n”;
cout<<pt1<<pt2<<pt3;
pt3=pt1+100.8;
cout<<“after pt3=pt1+100.8, pt3 is:”<<pt3;
pt3=pt1+pt2;
cout<<“after pt3=pt1+pt2, pt3 is:”<<pt3;
pt3=++pt1;
++pt2;
cout<<“after ++ pt1,pt2,pt3 are:\n”;
cout<<pt1<<pt2<<pt3;
pt3=pt1-pt2;
cout<<“after pt3=pt1-pt2, pt3 is:”<<pt3;
return 0 ;
}
程序运行结果如下。
original pt1,pt2,pt3 are:
(10.5,20.8)
(-5.3,18.4)
(0,0)
after pt3=pt1+100.8, pt3 is:(111.3,121.6)
after pt3=pt1+pt2, pt3 is:(5.2,39.2)
after ++ pt1,pt2,pt3 are:
(11.5,21.8)
(-4.3,19.4)
(11.5,21.8)
after pt3=pt1-pt2, pt3 is:(15.8,2.4)
实验解答:
(1) 类Point的构造函数:

Point(double a=0.0,double b=0.0):x(a),y(b)
	{}

(2) 用成员函数重载:前置“++”运算符和双目运算符“−”:

Point operator++()
{
	++x;
	++y;
	return *this;
}
Point operator-(Point &a)
{
	Point temp;
	temp.x=x-a.x;
	temp.y=y-a.y;
	return temp;
}

(3) 用友元函数形式重载:双目运算符“+”(两种版本,详见实验指导部分)

friend Point operator+(Point &a,double b)
{
	Point temp;
	temp.x=a.x+b;
	temp.y=a.y+b;
	return temp;
}
friend Point operator+(Point &a,Point &b)
{
	Point temp;
	temp.x=a.x+b.x;
	temp.y=a.y+b.y;
	return temp;
}

(4) 友元函数重载插入运算符:

friend ostream & operator<<(ostream &out,const Point &com)
{
	out<<"("<<com.x<<","<<com.y<<")"<<endl;
	return out;
}

(5) 程序正确后,根据运行结果,重新完成的main()函数:

int main()
{
	Point pt1(10.5,20.8),pt2(-5.3,18.4),pt3;
    cout<<"original pt1,pt2,pt3 are:\n";
    cout<<pt1<<pt2<<pt3;
    pt3=pt1+100.8;
    cout<<"after pt3=pt1+100.8, pt3 is:"<<pt3;
    pt3=pt1+pt2;
    cout<<"after pt3=pt1+pt2, pt3 is:"<<pt3;
    pt3=++pt1;
    ++pt2;
    cout<<"after ++ pt1,pt2,pt3 are:\n";
    cout<<pt1<<pt2<<pt3;
    pt3=pt1-pt2;
    cout<<"after pt3=pt1-pt2, pt3 is:"<<pt3;
    return 0 ;
}

实验题目2: 定义一个抽象类容器类,其中定义了若干纯虚函数,实现求表面积、体积、输出等功能。由此抽象类派生出正方体、球体和圆柱体等多个派生类,根据需要定义自己的成员变量,在各个派生类中重新定义各纯虚函数,实现各自类中相应功能,各个类成员的初始化均由本类构造函数实现。
① 在主函数中,定义容器类的指针和各个派生类的对象,使指针指向不同对象处调用相同的函数能执行不同的函数代码,从而实现动态多态性。
② 定义一个顶层函数void TopPrint(Container &r);使得主函数中调用该函数时,根据实在参数所有的类自动调用对应类的输出函数。
③ 主函数中定义一个Container类对象,观察编译时的错误信息,从而得出什么结论?
实验解答:
(1)基类Container的定义见实验教材。
(2)各个派生类的定义,根据提示进行填写完整代码:
① //正方体类,从Container类公有继承,定义构造函数,重新定义基类的3个纯虚函数

class Cube:public Container
{
public:
	Cube(double a):Container(a)
	{}
	double area()
	{
		return 6*radius*radius;
	}
	double volume()
	{
		return radius*radius*radius;
	}
	void print()
	{
		cout<<"the sidelength of cube is:"<<radius<<endl<<"the area of cube is:"<<6*radius*radius<<endl<<"the volume of cube is:"<<radius*radius*radius<<endl;
	}
};

② //球类,从Container类公有继承,定义构造函数,重新定义基类的3个纯虚函数

class Sphere:public Container
{
public:
	Sphere(double a):Container(a)
	{}
	double area()
	{
		return 4*PI*radius*radius;
	}
	double volume()
	{
		return 4.0/3*PI*radius*radius*radius;
	}
	void print()
	{
		cout<<"the radius of sphere is:"<<radius<<endl<<"the area of sphere is:"<<4*PI*radius*radius<<endl<<"the volume of sphere is:"<<4.0/3*PI*radius*radius*radius<<endl;
	}
};

③ //圆柱体类,从Container类公有继承,需要增加的成员变量,定义构造函数,重新定义基类的三个纯虚函数

class Cylinder:public Container
{
	double h;
public:
	Cylinder(double a,double H):Container(a),h(H)
	{}
	double area()
	{
		return 2*PI*radius*radius+2*PI*radius*h;
	}
	double volume()
	{
		return PI*radius*radius*h;
	}
	void print()
	{
		cout<<"the sidelengths of sphere are:"<<radius<<","<<h<<endl<<"the area of cylinder is:"<<2*PI*radius*radius+2*PI*radius*h<<endl<<"the volume of cylinder is:"<<PI*radius*radius*h<<endl;
	}
};

(3)正确定义各派生类对象,记录程序的运行结果是:
正方体对象:_______6
1
the sidelength of cube is:1
the area of cube is:6
the volume of cube is:1


球体对象:___________50.264
33.5093
the radius of sphere is:2
the area of sphere is:50.264
the volume of sphere is:33.5093


圆柱体对象:________50.264
25.132
the sidelengths of sphere are:2,2
the area of cylinder is:50.264
the volume of cylinder is:25.132


运行结果:
6
1
the sidelength of cube is:1
the area of cube is:6
the volume of cube is:1
50.264
33.5093
the radius of sphere is:2
the area of sphere is:50.264
the volume of sphere is:33.5093
50.264
25.132
the sidelengths of sphere are:2,2
the area of cylinder is:50.264
the volume of cylinder is:25.132
(4)主函数中定义一个Container类对象,编译器的报错信息:
报错就不放辣
试说明原因:

纯虚函数仅仅为多态机制提供一个界面而没有任何实体定义。

四、实验小结(包括问题和解决方法、心得体会、意见与建议等)
(中文五号宋体,英文五号Consolas字体,单倍行距)
(一)实验中遇到的主要问题及解决方法
1.在题目(2)中在主函数中定义Container类的对象,会产生报错信息,解释原因。

纯虚函数没有任何实体定义。

  1. 通过题目(2),你觉得纯虚函数与抽象类在编程中有什么价值和意义?

纯虚函数为多态机制提供一个界面。

3.在题目(1)中通过代码验证,请总结友元函数与成员函数在实现运算符重载时的区别。

1、 形参个数不同。
2、 显示方式的调用不同。
3、 第一运算对象不同。
4、 第二运算对象有区别。
5、 类外定义时声明部分有区别。

4.其它问题及解决方法:

无。

(二)实验心得

学了知识就要练习,才能更好理解和掌握。

(三)意见与建议(没有可省略)

五、支撑毕业要求指标点
1.2-M掌握计算机软硬件相关工程基础知识,能将其用于分析信息安全领域的相关工程问题。
3.1-H掌握信息安全领域所涉及的软硬件系统,从数字电路、计算机系统、到各类系统软件的基本理论与设计结构。

  • 12
    点赞
  • 78
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值