/*
*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:田成琳
*完成日期:2014 年 5 月 10 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*输入描述:无
*问题描述:定义点类Point,并以点类为基类,派生出圆类Cricle,
求圆的面积,并派生出圆柱体类。
*程序输出:圆和圆柱体的信息
*问题分析:
*算法设计:
*/
#include<iostream>
#include<Cmath>
using namespace std;
const double pi=3.14;
class Point //定义坐标点类
{
public:
Point():x(0),y(0) {};
Point(double x0, double y0):x(x0), y(y0) {};
double getX()
{
return x;
}
double getY()
{
return y;
}
void PrintPoint(); //输出点的信息
protected:
double x,y; //点的横坐标和纵坐标
};
void Point::PrintPoint()
{
cout<<"Point: ("<<x<<","<<y<<")"<<endl; //输出点
}
class Cricle:public Point
{
public:
Cricle(Point pts,Point pte);
Cricle(double a,double b,double c,double d)
{
r=sqrt(pow(a-c,2)+pow(b-d,2));
}
double area();
double getR()
{
return r;
}
protected:
double r;
};
Cricle::Cricle(Point pts,Point pte)
{
r=sqrt(pow((pts.getX()-pte.getX()),2)+pow((pts.getY()-pte.getY()),2));
}
double Cricle::area()
{
return pi*r*r;
}
class Cylinder:public Cricle
{
public:
Cylinder(double a,double b,double c,double d,double hh):
h(hh),Cricle(a,b,c,d){}
double area1();
double volume();
private:
double h;
};
double Cylinder::area1()
{
return 2*pi*getR()*getR()+2*pi*getR()*h;
}
double Cylinder::volume()
{
return pi*getR()*getR()*h;
}
int main()
{
double h;
Point ps(-2,5),pe(7,9);
cout<<"两点为:"<<endl;
cout<<"1st ";
ps.PrintPoint();
cout<<"2nd ";
pe.PrintPoint();
cout<<"以两点距离为半径构成的圆:"<<endl;
Cricle c(ps,pe);
cout<<"圆半径为:"<<c.getR()<<endl;
cout<<"圆的面积为:"<<c.area()<<endl;
cout<<"以圆半径为半径构成的圆柱体:"<<endl;
cout<<"请输入圆柱体的高:"<<endl;
cin>>h;
Cylinder Cy(-2,5,7,9,h);
cout<<"圆柱体的表面积为:"<<Cy.area1()<<endl;
cout<<"圆柱体的体积为:"<<Cy.volume()<<endl;
return 0;
}
运行结果:
心得体会:派生出的类构造函数必须使用到父类的成员