第十周实验报告(任务3)

时间:2022-03-11 08:04:57

程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: Point类的相关处理 
* 作    者:     郭广建                       
* 完成日期:  2012年    04  月  23   日
* 版 本 号:  1.0

源程序:

/*()先建立一个Point(点)类,包含数据成员x,y(坐标点);
()以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径);
()再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。
要求编写程序,设计出各类中基本的成员函数(包括构造函数、析构函数、修改数据成员和获取数据成员的公共接口、用于输出的重载运算符“<<”函数等),使之能用于处理以上类对象,最后求出圆格柱体的表面积、体积并输出。
(提示:此任务可以分为三个子任务分成若干步骤进行。先声明基类,再声明派生类,逐级进行,分步调试。——这种方法适用于做任何的项目)
()第个程序:基类Point类及用于测试的main()函数

()第个程序:声明Point类的派生类Circle及其测试的main()函数

()第个程序:声明Circle的派生类Cylinder及测试的main()函数*/
#include<iostream>

#define pi 3.14

using namespace std;

class Point 
{
protected:
	double x;

	double y;
public:

	Point(double x0 = 0,double y0 = 0):x(x0),y(y0){}

	friend ostream& operator <<(ostream& output, Point& cp);

    Point getPoint(double,double);

	double getX(double);

	double getY(double);


	~Point();
};
class Circle : public Point
{
protected:
	double r;

public:

	Circle(double x0, double y0 , double r0):Point(x0,y0)
	{
		r = r0;
	}
    friend ostream& operator <<(ostream& output, Circle& cri);

	Point getCri_cen(double,double);

	double getRadii(double);

	~Circle();
};
class Cylinder : public Circle
{
private:
	double h;
public:
	Cylinder(double x0, double y0, double r0, double h0):Circle(x0,y0,r0)
	{
		h = h0;
	}
    friend ostream& operator <<(ostream& output, Cylinder& cyl);

	Circle getCircle(double,double,double);

	double getHigh(double);

	void Voluem();

	void Area();

	~Cylinder();
};
//Point类的有关定义
ostream& operator <<(ostream& output, Point& cp)
{
	output << '(' << cp.x << ',' << cp.y << ')' ;

	return output;
}
Point Point::getPoint(double x1,double y1)
{
	cout <<"请输入要修改为的点的坐标:" <<endl;

	cin >> x1 >> y1;

	x = x1;

	y = y1;

	return Point(x, y);
}
double Point::getX(double x1)
{
	cout << "请输入要改的横坐标:" <<endl;

	cin >> x1;

	return (x = x1);
}
double Point::getY(double y1)
{
	cout << "请输入要改的纵坐标:" <<endl;

	cin >> y1;

	return(y = y1);
}
Point::~Point(){}

//Circle类的有关定义
ostream& operator <<(ostream& output, Circle& cir)
{
	output << "圆心是:" << '(' << cir.x << ',' << cir.y << ')';

	output << "半径是:" << cir.r;

	return output;
}
Point Circle::getCri_cen(double x1,double y1)
{
	cin >> x1 >> y1;

	x = x1;

	y = y1;

	return Point(x, y);
}

double Circle::getRadii(double r1)
{
	cin >> r1;

	return (r = r1);
}

Circle::~Circle(){}

//Cylinder类的有关定义
ostream& operator <<(ostream& output, Cylinder& cyl)
{
	output << "底面圆心是:" << '(' << cyl.x << ','<< cyl.y << ')';

	output << "  底面半径是:" << cyl.r;

	output << "  高是:" << cyl.h;
	
	return output;
}

Circle Cylinder::getCircle(double x1,double y1,double r1)
{
	cin >> x1 >> y1 >> r1;

	return Circle(x = x1, y = y1, r = r1);
}

double Cylinder::getHigh(double h1)
{
	cin >> h1;

	return (h = h1);
}

void Cylinder::Voluem()
{
	cout << (pi* r * r * h);
}

void Cylinder::Area()
{
	cout << ( 2 * pi * r * r + 2 * pi * r * h);
}

Cylinder::~Cylinder(){};
int main()
{
	Point cp1(-2,5), cp2(3,7);

	double x1 = 0, y1 = 0, r1 = 0, h1 = 0;

	cout << "对Point类进行测试:" <<endl;

	cout << "该点坐标是:" << cp1 <<endl;

	cp1.getPoint(x1,y1);

	cout << "改正后的点为:" << cp1 <<endl;

	cp1.getX(x1);

	cout << "改正后的点为:" << cp1 <<endl;

	cp1.getY(y1);

	cout << "改正后的点为:" << cp1 <<endl;




	Circle cir1(2,2,5);

	cout << "对Circle类进行测试:" <<endl;


	cout <<"圆的信息为:" <<endl;

	cout << cir1 <<endl;

	cout << "请输入想要设定的圆心:" <<endl;

	cir1.getCri_cen(x1,y1);

	cout << cir1;

	cout <<endl;

	cout << "请输入想要设定的半径:"<<endl;

	cir1.getRadii(r1);

	cout << cir1;

    cout <<endl;



	Cylinder cyl1(2,2,4,3);

	cout << "以下是对Cylinder类的测试:"<<endl;

	cout << "圆柱体信息为:" ;

	cout << cyl1;

	cout <<endl;

	cout << "圆柱体的体积是:";

	cyl1.Voluem();

	cout <<endl;

	cout << "圆柱体的表面积是:" ;

	cyl1.Area();

	cout <<endl;

	cout << "请输入想要设定的底面:" ;

	cyl1.getCircle(x1,y1,r1);

	cout <<endl;

	cout << "请输入想要设定的高:" ;

	cyl1.getHigh(h1);

	cout <<endl;

	cout << "修改后圆柱体的体积是:" ;

	cyl1.Voluem();

	cout <<endl;

	cout << "修改后圆柱体的表面积是:" ;

	cyl1.Area();

	cout <<endl;

    system("pause");

    return 0;
}



运行结果:

对Point类进行测试:
该点坐标是:(-2,5)
请输入要修改为的点的坐标:
2 3
改正后的点为:(2,3)
请输入要改的横坐标:
5
改正后的点为:(5,3)
请输入要改的纵坐标:
7
改正后的点为:(5,7)
对Circle类进行测试:
圆的信息为:
圆心是:(2,2)半径是:5
请输入想要设定的圆心:
3 4
圆心是:(3,4)半径是:5
请输入想要设定的半径:
6
圆心是:(3,4)半径是:6
以下是对Cylinder类的测试:
圆柱体信息为:底面圆心是:(2,2)  底面半径是:4  高是:3
圆柱体的体积是:150.72
圆柱体的表面积是:175.84
请输入想要设定的底面:3 5 7

请输入想要设定的高:2

修改后圆柱体的体积是:307.72
修改后圆柱体的表面积是:395.64
请按任意键继续. . .

经验积累:

1.掌握多重派生的运用和构造函数的定义,里面有很多的知识点需要注意