* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:先声明点类,在派生出圆类 圆柱类
* 作 者: 张启立
* 完成日期: 2012年 4 月 16 日
* 版 本 号: 10-3-1
#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(2, 2, 1, 3);
cout << cy << endl;
cout << setiosflags(ios::fixed) << setprecision(2);
cout << "表面积是:" << cy.area2() << endl;
cout << "体积是:" << cy.volume() << endl;
system("pause");
return 0;
}