/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 苗影
* 完成日期: 2012 年 4 月 24 日
* 版 本 号:
#include <iostream> using namespace std; class Point { public: Point(double x=0,double y=0); void setPoint(double,double); double getX( ) {return x;} double getY( ) {return y;} friend ostream & operator<<(ostream &, Point &); 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 x=0,double y=0,double r=0); void setR(double); double getR( ) ; double area ( ) ; friend ostream &operator<<(ostream &, Circle &); protected: double R; }; Circle::Circle(double a,double b,double r):Point(a,b),R(r){ } void Circle::setR(double r) { R=r; } double Circle::getR( ) { return R; } 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 x=0,double y=0,double r=0,double h=0); void setHeight(double); double getHeight( ) ; double area( ) ; double volume( ) ; friend ostream& operator<<(ostream&, Cylinder&); protected: double height; }; Cylinder::Cylinder(double a,double b,double r,double h) :Circle(a,b,r),height(h){} void Cylinder::setHeight(double h) { height=h; } double Cylinder::getHeight( ) { return height; } double Cylinder::area( ) { return 2*Circle::area( )+2*3.14159*getR()*height; } double Cylinder::volume() { return Circle::area()*height; } 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.getHeight( )<<"\narea="<<cy1.area() <<",volume="<<cy1.volume()<<endl; cy1.setHeight(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 请按任意键继续. . .
经验:
计算面积是老是写成R*R,R是不能直接用的,要通过函数调用