/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:先建立一个Point(点)类,包含数据成员x,y(坐标点);以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径);再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员(高)。要求编写程序,设计出各类中基本的成员函数(包括构造函数、析构函数、修改数据成员和获取数据成员的公共接口、用于输出的重载运算符“<<”函数等),使之能用于处理以上类对象,最后求出圆格柱体的表面积、体积并输出。
* 作 者:王琦
* 完成日期:2012年 04月 25日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 程序头部的注释结束
* 程序输出:
#include <iostream>
using namespace std;
class Point
{
protected:
double x,y;//分别代表横坐标和纵坐标
public:
Point(double m=0,double n=0);
void setpoint(double d,double e){x=d;y=e;}
friend ostream& operator << (ostream& output,Point&c);
double getpointx(){return x;}
double getpointy(){return y;}
};
Point::Point(double m,double n)
{
x=m;
y=n;
}
ostream& operator << (ostream& output,Point&c)
{
output<<"点为:"<<"("<<c.x<<","<<c.y<<")"<<endl;
return output;
}
class Circle:public Point
{
protected:
double r;//圆的半径
public:
Circle(double c1,double c2,double c3):Point(c1,c2){r=c3;}
friend ostream& operator << (ostream& output,Circle&c);
void setRadius(double);
double area( );
double getRadius(){return r;}
};
void Circle::setRadius(double m)
{
r=m;
}
double Circle::area()
{
return 3.1415926*r*r;
}
ostream& operator << (ostream& output,Circle&c)
{
output<<"圆心:"<<"("<<c.x<<","<<c.y<<")"<<"面积为:"<<c.area()<<endl;
return output;
}
class Cylinder:public Circle
{
private:
double h;//圆柱的高
public:
Cylinder(double t1,double t2,double t3,double t4):Circle(t1,t2,t3){h=t4;}
friend ostream& operator << (ostream& output,Cylinder&c);
double area( ) ;
double volume();
void setHeight(double m){h=m;}
double getHeight(){return h;}
};
double Cylinder::area( )
{
return 2*Circle::area( )+2*3.14159*r*h;
}
double Cylinder::volume()
{
return area()*h;
}
ostream& operator << (ostream& output,Cylinder&c)
{
output<<"Center=["<<c.x<<","<<c.y<<"], r="<<c.r<<", h="<<c.h
<<"\narea="<<c.area( )<<", volume="<<c.volume( )<<endl;
return output;
}
int main( )
{
Point t(5,6);
cout<<t;
Circle t1(7,3,1);
cout<<t1;
Cylinder cy1(3.5,6.4,5.2,10);
cout<<"\noriginal cylinder:\nx="<<cy1.getpointx( )<<", y="<<cy1.getpointy( )<<", r="
<<cy1.getRadius( )<<", h="<<cy1.getHeight( )<<"\narea="<<cy1.area()
<<",volume="<<cy1.volume()<<endl;
cy1.setHeight(15);
cy1.setRadius(7.5);
cy1.setpoint(5,5);
cout<<"\nnew cylinder:\n"<<cy1;
system("pause");
return 0;
}
*感想:这两周的任务每次做的都会出现很多的错误,写过的运行不出来,出现的错误改的少了,但还是会出现,我是很不愿意贴错误的代码。