[cpp]
/*
* Copyright (c) 2013, 煙台大學計算機學院
* All rights reserved.
* 文件名稱:test.cpp
* 作者:邱學偉
* 完成日期:2013 年 5 月 19 日
* 版本號:v1.0
* 輸入描述:無
* 問題描述:
* 程序輸出:
* 問題分析:
* 算法設計:略
*/
/*(1)先建立一個Point(點)類,包含數據成員x,y(坐標點);
(2)以Point為基類,派生出一個Circle(圓)類,增加數據成員 (半徑);
(3)再以Circle類為直接基類,派生出一個Cylinder(圓柱體)類,再增加數據成員h(高)。
要求編寫程序,設計出各類中基本的成員函數(包括構造函數、析構函數、修改數據成員和獲取數據成員的公共接口、用於輸出的重載運算符“<<”函數等),使之能用於處理以上類對象,最後求出圓格柱體的表面積、體積並輸出。
*/
#include <iostream>
using namespace std;
class Point
{
public:
Point(double a=0,double b=0);
void SetPoint(double,double);
double getX() const {return x;}
double getY() const {return y;}
void ShowPoint();
friend ostream & operator<<(ostream &output,const Point &c);
protected:
double x,y;
};
Point::Point(double a,double b)
{
x=a;
y=b;
}
void Point::SetPoint(double x1,double y1)
{
x=x1;
y=y1;
}
void Point::ShowPoint()
{
cout<<"Point: ("<<x<<","<<y<<")"<<endl;
}
ostream & operator<<(ostream &output,const Point &c)
{
output<<"point: ("<<c.x<<","<<c.y<<")"<<endl;
return output;
}
class Circle:public Point
{
public:
Circle(double a,double b,double c):Point(a,b),r(c){}
void SetCircle(double r2);
double Getr(){return r;}
double area();
void ShowCircle();
friend ostream & operator<<(ostream &,const Circle &C);
protected:
double r;
};
void Circle::SetCircle(double r2)
{
r=r2;
}
double Circle::area()
{
return 3.1415*r*r;
}
void Circle::ShowCircle()
{
cout<<"Circle: ("<<x<<","<<y<<") r="<<r<<endl;
}
ostream & operator<<(ostream &output,const Circle &C)
{
output<<"Circle: ("<<C.x<<","<<C.y<<") r="<<C.r<<endl;
return output;
}
class Cylinder:public Circle
{
public:
Cylinder(double x3,double y3,double r3,double h3):Circle(x3,y3,r3),h(h3){}
void setheight(double);
double GetCylinder();
double area();
double volume();
friend ostream &operator<<(ostream &, Cylinder &);
protected:
double h;
};
void Cylinder::setheight(double h1)
{
h=h1;
}
double Cylinder::GetCylinder()
{
return h;
}
double Cylinder::area()
{
return 2*3.1415*r*r+3.1415*2*r*h;
}
double Cylinder::volume()
{
return 3.1415*r*r*h;
}
ostream &operator<<(ostream &output, Cylinder &cy)
{
output<<"cylinder: ("<<cy.x<<","<<cy.y<<") r="<<cy.r<<" height="<<cy.h<<" area="<<cy.area()<<" volume="<<cy.volume()<<endl;
return output;
}
int main()
{
Cylinder cy(3.5,6.4,5.2,10);
cout<<"cylinder: POINT:("<<cy.getX()<<","<<cy.getY()<<") r="<<cy.Getr()<<" height="<<cy.GetCylinder()<<" are="<<cy.area()<<" volume="<<cy.volume()<<endl;
cy.setheight(15);
cy.SetCircle(7.5);
cy.SetPoint(5,5);
cout<<"cylinder: "<<cy<<endl;
return 0;
}