天天看點

類族的設計

/*
* 程式的版權和版本聲明部分
* Copyright (c)2014, 煙台大學計算機學院學生
* All rightsreserved.
* 檔案名稱: fibnacci.cpp
* 作    者:高古尊
* 完成日期:2014年5月18日
* 版本号: v1.0
*
* 輸入描述:
* 問題描述:
* 程式輸出:
* 問題分析:
*/
#include<iostream>
#include<Cmath>
using namespace std;
class Point //定義坐标點類
{
public:
    Point():x(0),y(0) {};
    Point(double x0, double y0):x(x0), y(y0) {};
    double getx()
    {
        return x;
    }
    double gety()
    {
        return y;
    }
    void PrintPoint(); //輸出點的資訊
protected:
    double x,y;   //點的橫坐标和縱坐标
};
void Point::PrintPoint()
{
    cout<<"Point: ("<<x<<","<<y<<")"<<endl;    //輸出點
}
class Circle: public Point   //利用坐标點類定義直圓類, 其基類的資料成員表示直線的圓點
{
public:
    Circle():r(0) {}
    Circle(Point, double ); //構造函數,用初始化直線的兩個端點及由基類資料成員描述的圓心點
    double area();//計算并傳回圓的面積
    double getr()
    {
        return r;
    }
    void Printarea();   //輸出圓的面積
protected:
    double r;//圓的半徑,從Point類繼承的資料成員表示直線的圓心點
};

Circle::Circle(Point pt, double l):Point(pt.getx(),pt.gety())
{
    r=l;
}

double Circle::area()
{
    return 3.14*r*r;
}
void Circle::Printarea()
{
    cout<<area()<<endl;
}
class Cylinder:public Circle
{
public:
    Cylinder():h(0) {}
    Cylinder(Circle,double );
    double volume();
    void PrintCylinder();
private:
    double h;
};
Cylinder::Cylinder(Circle yuan,double h1):Circle(Point(yuan.getx(),yuan.gety()),yuan.getr())
{
    h=h1;
}
double Cylinder::volume()
{
    return h*area();
}
void Cylinder::PrintCylinder()
{
    cout<<volume()<<endl;
}
int main()
{
    Point p(1,1);
    Circle s(p,1);
    Cylinder a(s,1);
    cout<<"圓心為";
    a.PrintPoint();
    cout<<"圓的面積為";
    a.Printarea();
    cout<<"圓柱體的體積為";
    a.PrintCylinder();
    return 0;
}
           
類族的設計

繼續閱讀