/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.* 文件名称:点、圆、圆柱
* 作 者: 郭岩岩
* 完成日期:2012 年4月 24日
* 版 本 号: vc.1
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
*程序头部的注释结束
*/
#include<iostream>
using namespace std;
class Point //定义坐标点类
{public:
Point(double a=0,double b=0);
void setpoint(double c,double d);
double getx(){return x;}
double gety(){return y;}
friend ostream &operator<<(ostream &output,Point &p);
protected:
double x,y;
};
Point::Point (double a,double b)
{
x=a;
y=b;
}
void Point::setpoint(double c,double d)
{
x=c;
y=d;
}
ostream& operator<<(ostream &output,Point &p)
{
output<<"("<<p.x <<","<<p.y<<")"<<endl;
return output;
}
class Circle:public Point
{
public:
Circle(double a,double b,double c);
void setr(double);
double getr(){return r;}
double area();
friend ostream & operator<<(ostream & output,Circle & c);
protected:
double r;
};
Circle::Circle(double a,double b,double c):Point(a,b)
{
r=c;
}
void Circle::setr(double c)
{
r=c;
}
double Circle::area()
{
return 3.1415926*getr()*getr();
}
ostream & operator<<(ostream & output,Circle &c)
{
output<<"圆心为:("<<c.x<<","<<c.y <<")"<<"半径为:"<<c.getr()<<"面积为:"<<c.area()<<endl;
return output;
}
class Cylinder:public Circle
{
public:
Cylinder(double a,double b,double c,double d);
double geth(){return h;}
void seth(double d);
double volume();
double area();
friend ostream & operator<<(ostream &output,Cylinder &cy);
protected:
double h;
};
Cylinder::Cylinder(double a,double b,double c,double d):Circle(a,b,c),h(d){}
void Cylinder::seth(double d)
{
h=d;
}
double Cylinder::volume()
{
return Circle::area()*h;
}
double Cylinder::area()
{
return 2*Circle::area()+2*3.1415926*getr()*h;
}
ostream & operator<<(ostream &output,Cylinder &cy)
{
output<<"圆心为:("<<cy.x<<","<<cy.y <<")"<<"半径为:"<<cy.getr()<<"面积为:"<<cy.area()<<"表面积为:"<<cy.volume()<<endl;
return output;
}
int main( )
{
Cylinder cy1(3.5,6.4,5.2,10);
cout<<"\noriginal cylinder:\nx="<<cy1.getx( )<<", y="<<cy1.gety( )<<", r="
<<cy1.getr( )<<", h="<<cy1.geth( )<<"\narea="<<cy1.area()
<<",volume="<<cy1.volume()<<endl;
cy1.seth(15);
cy1.setr(7.5);
cy1.setpoint(5,5);
cout<<"\nnew cylinder:\n"<<cy1;
system("pause");
return 0;
}
original cylinder:
x=3.5, y=6.4, r=5.2, h=10
area=496.623,volume=849.487
new cylinder:
圆心为:(5,5)半径为:7.5面积为:1060.29表面积为:2650.72
请按任意键继续. . .
经验积累:在定义派生类构造函数时,要调用基类的构造函数;
在用到基类和派生类同名的函数时要写明作用域
上机感言:小小字符不可忽视,忘写<<和;导致出现好多bug;