* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 第十周 任务三
* 作 者: 杨森
* 完成日期: 2012 年 4 月 25 日
运行结果:
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 第十周 任务三
* 作 者: 杨森
* 完成日期: 2012 年 4 月 25 日
* 版 本 号: V1.0
源程序:
#include<iostream>
#define pi 3.1415
using namespace std;
class Point
{
public:
int x;
int y;
public:
Point (int xx = 0, int yy = 0):x(xx), y(yy){};
~Point();
void setPoint(int a, int b);
friend ostream & operator << (ostream & out, Point &a);
};
Point::~Point ()
{
}
ostream & operator << (ostream & out, Point & a)
{
out << "(" << a.x << "," << a.y << ")" << endl;
return out;
}
void Point::setPoint (int a, int b)
{
x = a;
y = b;
}
class Circle: public Point
{
public:
int r;
public:
Circle (int xx, int yy, int r1):Point (xx, yy){r = r1;}
~Circle();
void setCircle(int a, int b, int c);
friend ostream & operator << (ostream & out, Circle & a);
};
Circle::~Circle()
{
}
void Circle::setCircle(int a, int b, int c)
{
x = a;
y = b;
r = c;
}
ostream & operator << (ostream & out, Circle & a)
{
out << "圆心:"<< "(" << a.x << "," << a.y << ")" << endl;
out << "半径:" << a.r;
return out;
}
class Cylinder: public Circle
{
private:
int h;
public:
Cylinder(int xx, int yy, int r1, int h1):Circle(xx, yy, r1){h = h1;}
~Cylinder();
void setCylinder(int a, int b, int c, int d);
friend ostream & operator << (ostream &out, Cylinder &a);
double Carea();
double Cvolume();
};
Cylinder::~Cylinder()
{
}
void Cylinder::setCylinder(int a, int b, int c, int d)
{
x = a;
y = b;
r = c;
h = d;
}
ostream & operator << (ostream &out, Cylinder &a)
{
out <<"圆心:" << "("<< a.x <<"," <<a.y <<")" << endl;
out<<"半径:" << a.r << endl;
out<< "高: " << a.h << endl;
return out;
}
double Cylinder::Carea()
{
double m;
m = 2 * r * pi;
return (m * h);
}
double Cylinder::Cvolume()
{
return (r * r * pi * h);
}
int main()
{
Cylinder c(2, 2, 2 ,2);
cout << c;
cout<< "表面积:" << c.Carea () << endl;
cout << "体积:" << c.Cvolume () << endl;
system("pause");
return 0;
}
运行结果:
小感:层层叠加,多层派生