2012C++程序設計實驗報告【10.3】

/* (程序頭部註釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生
* All rights reserved.
* 文件名稱:                      
* 作    者:      王琳                      
* 完成日期:    2012     年   4    月   23  日
* 版 本 號:     10-3

* 對任務及求解方法的描述部分
* 問題描述:1)先建立一個Point(點)類,包含數據成員x,y(座標點);(2)以Point爲基類,派生出一個Circle(圓)類,增加數據成員 (半徑);(3)再以Circle類爲直接基類,派生出一個Cylinder(圓柱體)類,再增加數據成員h(高)。要求編寫程序,設計出各類中基本的成員函數(包括構造函數、析構函數、修改數據成員和獲取數據成員的公共接口、用於輸出的重載運算符“<<”函數等),使之能用於處理以上類對象,最後求出圓格柱體的表面積、體積並輸出。(提示:此任務可以分爲三個子任務分成若干步驟進行。先聲明基類,再聲明派生類,逐級進行,分步調試。——這種方法適用於做任何的項目)(1)第1個程序: 基類Point類及用於測試的main()函數(2)第2個程序:聲明Point類的派生類Circle及其測試的main()函數(3)第3個程序:聲明Circle的派生類Cylinder及測試的main()函數
* 程序頭部的註釋結束
*/

源程序:

#include <iostream>

#include<Cmath>   
  
#include<iomanip>   
  
using namespace std;

const double PI = 3.141592653;

class Point//點類聲明
{
public:
	Point(){x = 0;y = 0;}
	Point(double x0,double y0):x(x0),y(y0){ }
	~Point(){ }
    double getx(){return x;}  
    double gety(){return y;}  
    friend ostream &operator << (ostream & input, Point & c);  
protected:  
    double x, y;   //點的橫座標和縱座標   
};

ostream &operator << (ostream & input, Point & c)
{
	input << "點:(" << c.x << "," << c.y << ")";  
  
    return input;  
}

class Circle: public Point   //以點類爲基類的派生類
{  
public:  
    Circle(){radius = 0;}  
    Circle(double x0, double y0, double r):Point(x0, y0),radius(r){ }      //構造函數   
    ~Circle(){} 
    double getr(){return radius;}  
	double area1();
    friend ostream &operator << (ostream & out, Circle & c);  
protected:  
    double radius;  
}; 
 
double Circle::area1() //圓形面積
{
	double c_area = PI * getr() * getr();

	return c_area;
}

ostream &operator << (ostream & out, Circle & c)  
{  
    out << "(" << c.getx() << "," << c.gety() << ")" << "爲圓心" << c.radius << "爲半徑的圓";  
  
    return out;  
}  
  
class Cylinder: public Circle  //以圓類爲基類的派生類
{  
public:  
    Cylinder(){height = 0;}  
    Cylinder(double x1,double y1, double r1, double h):Circle(x1, y1, r1), height(h){ }  
    ~Cylinder(){}  
    double geth(){return height;}  
    double area2();  
    double volume();  
    friend ostream &operator << (ostream & out, Cylinder & c);  
private:  
    double height;  
};  
   
double Cylinder::area2()  
{  
  
    //double c_area = PI * getr() * getr();  
  
    double c_cir = 2 * PI * getr() * height;   //圓柱側面積
  
    return (c_cir + 2 * area1());  
}  
  
double Cylinder::volume()  
{  
    return (PI * getr() * getr() * height);  
}  
  
ostream &operator << (ostream & out, Cylinder & c)  
{  
    out << "(" << c.getx() << "," << c.gety() << ")" << "爲圓心" << c.radius << "爲半徑" << c.height << "爲高的圓柱體";  
  
    return out;  
}  
  
int main()  
{  
    Cylinder cy(6, 5, 4, 3);  
  
    cout << cy << endl;  
  
    cout << setiosflags(ios::fixed) << setprecision(2);  
  
    cout << "表面積是:" << cy.area2() << endl;  
  
    cout << "體積是:" << cy.volume() << endl;  
  
    system("pause");  
  
    return 0;  
}


運行結果:

 (6,5)爲圓心4爲半徑3爲高的圓柱體
表面積是:175.93
體積是:150.80
請按任意鍵繼續. . .

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