第十週實驗報告(任務3)

程序頭部註釋開始
* 程序的版權和版本聲明部分
* 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.掌握多重派生的運用和構造函數的定義,裏面有很多的知識點需要注意

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章