南京邮电大学通达学院C++上机实验参考答案

  • 实验目的和要求

(1)进一步熟悉累的设计,运用继承与派生机制设计派生类,合理设置数据成员和成员函数。

(2)掌握双目运算符、单目运算符的重载方法,对常用算数运算符能在自定义类中通过友元函数、成员函数进行重载,以实现静态多态性。

(3)掌握通过继承、虚函数、基类的指针或引用实现动态多态性。

(4)理解并掌握有纯虚函数的抽象类的作用,在各派生类中重新定义纯虚函数的方法,并实现动态多态性。

实验原理及内容

实验题目1【见实验教材实验五的题目1】 定义Point类,在该类中包含如下内容:

1)私有数据成员:

   double x; //x轴坐标

   double y; //y轴坐标

2)共有成员函数:

   构造函数  //默认值为原点(0.0,0.0)

    Point operator++();  //成员函数重载前置“++”

    Point operator-(const Point &a);  //成员函数重载运算符“-”

    friend Point operator+(const Point &a,const Point &b); //友元函数重载“+”

    friend Point operator+(const Point &a,double b); //友元函数重载“+”

    friend ostream & operator <<(ostream & out,const Point & obj); //友元函数重载“<<”

给出主函数如下:

int main()

{

    Point p1(10,30),p2(-18,0),p3;

    cout<<"初始坐标点p1,p2,p3为:";

    cout<<p1<<" "<<p2<<" "<<p3<<endl;

    p3=p1+p2;

    cout<<"执行p3=p1+p2后,坐标点p1,p2,p3为:";

    cout<<p1<<" "<<p2<<" "<<p3<<endl;

    p3=p3+20;

    cout<<"执行p3=p3+20后,坐标点p1,p2,p3为:";

    cout<<p1<<" "<<p2<<" "<<p3<<endl;

    p2=p3-p1;

    cout<<"执行p2=p3-p1后,坐标点p1,p2,p3为:";

    cout<<p1<<" "<<p2<<" "<<p3<<endl;

    ++p2;

    cout<<"执行++p2后,坐标点p1,p2,p3为:";

    cout<<p1<<" "<<p2<<" "<<p3<<endl;

    return 0;

}

#include<iostream>
using namespace std;
class Point {
private:
	double x;
	double y;
public :
	Point(double m = 0.0,double n=0.0){
		x = m;
		y = n;
	}
	Point operator++();
	Point operator-(const Point& a);
	friend Point operator +(const Point& a, const Point& b);
	friend Point operator+(const Point& a, double b);
	friend ostream& operator <<(ostream& out, const Point& obj);
};
Point Point::operator++() {
	++x;
	++y;
	return *this;
}
Point Point::operator-(const Point& a) {
	Point reduce;
	reduce.x =x-a.x;
	reduce.y =y-a.y;
	return reduce;
}
Point operator+(const Point& a, const Point& b){
	Point puls;
	puls.x =(a.x+b.x);
	puls.y =(a.y+b.y);
	return puls;
}
Point operator+(const Point& a, double b) {
	Point puls1;
	puls1.x += a.x+b;
	puls1.y += a.y+b;
	return puls1;
}

ostream& operator <<(ostream& out, const Point& obj) {
	out << "(" << obj.x << "," << obj.y << ")";
	return out;
 }
int main(){
    Point p1(10,30),p2(-18,0),p3;
    cout<<"初始坐标点p1,p2,p3为:";
    cout<<p1<<" "<<p2<<" "<<p3<<endl;
    p3=p1+p2;
    cout<<"执行p3=p1+p2后,坐标点p1,p2,p3为:";
    cout<<p1<<" "<<p2<<" "<<p3<<endl;
    p3=p3+20;
    cout<<"执行p3=p3+20后,坐标点p1,p2,p3为:";
    cout<<p1<<" "<<p2<<" "<<p3<<endl;
    p2=p3-p1;
    cout<<"执行p2=p3-p1后,坐标点p1,p2,p3为:";
    cout<<p1<<" "<<p2<<" "<<p3<<endl;
    ++p2;
    cout<<"执行++p2后,坐标点p1,p2,p3为:";
    cout<<p1<<" "<<p2<<" "<<p3<<endl;
    return 0;
}

运行结果如下:

 实验题目(2)【见实验教材实验五的题目2】定义一个抽象类容器,其中定义若干纯虚函数,实现求表面积、体积、输出等功能。

参考代码如下:

#include<iostream>
using namespace std;
class Container{
protected:
	double radius;
public:
	Container(double r)
	{
		radius = r;
	}
	virtual double area() = 0;
	virtual double volume() = 0;
	virtual void print() = 0;
};

class Cube : public Container                           //立方体类,从Container公有继承
{                        //定义Cube类的成员函数:表面积和体积

public:
	//构造函数
	Cube(double r) : Container(r) {}
    double area()		//重新定义求面积函数
	{
		return 6 * radius * radius;
	}
	double volume()   //重新定义求体积函数
	{
		return radius * radius * radius;
	}
	void print()   //重新定义求打印函数
	{
		cout <<"Cube area=" <<area() << "  Cube volum="<<volume() << endl;
	}
};

class Sphere : public    Container                           //球类,从Container公有继承
{
	//定义Sphere类的成员函数:表面积和体积
public:
	//构造函数
	Sphere(double r) : Container(r) {}
	double area()		//重新定义求面积函数
	{
		return 4 * 3.14 * radius * radius;
	}
	double volume()   //重新定义求体积函数
	{
		return (4 * 3.14 * radius * radius) / 3;
	}
	void print()   //重新定义求打印函数
	{
		cout <<"Sphere  area="<< area() << "   Sphere volume="<<volume() << endl;
	}
};
class Cylinder : public    Container                           //圆柱体类,从Container公有继承
{
	//定义Cylinder类的成员函数:表面积和体积
public:
	//构造函数
	Cylinder(double r) : Container(r) {}

	double area()		//重新定义求面积函数
	{
		return 2*3.14*radius*radius+2*3.14*radius;
	}
	double volume()   //重新定义求体积函数
	{
		return 3.14 * radius * radius*radius;
	}
	void print()   //重新定义求打印函数
	{
		cout << "Cylinder area="<<area() << "   Cylinder volum="<<volume() << endl;
	}
};
int main(){
	Container* p;
	Cube c1(1.0);
	Sphere s1(1.0);
	Cylinder c2(1.0);
	p = &c1;
	cout << p->area() << endl;
	cout << p->volume() << endl;
	p->print();
	p = &s1;
	cout << p->area() << endl;
	cout << p->volume() << endl;
	p->print();
	p = &c2;
	cout << p->area() << endl;
	cout << p->volume() << endl;
	p->print();
	return 0;
}

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大狗晋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值