点类派生出圆类,圆类派生出圆柱类

#include <iostream>
#include <stdlib.h>
using namespace std;
const double PI=3.14159;

//基类的定义
class Point
{
private:
int x,y;
public:
Point(int xx,int yy)
{
x=xx;
y=yy;
}
int Getx()
{
return x;
}
int Gety()
{
return y;
}
/*
void show()
{
cout<<x<<","<<y<<endl;
}
*/
friend ostream &operator<<(ostream &,const Point &);
};
ostream &operator<<(ostream &show,const Point &p)
{
show<<"("<<p.x<<","<<p.y<<")"<<endl;
return show;
}

//定义子类“圆”,公共的继承
class Circle:public Point
{
protected:
int r;
public:
Circle(int xx,int yy,int rr):Point(xx,yy)
{
r=rr;
}
int Getr()
{
return r;
}
double circumferen() const
{
return 2*PI*r;
}
double area()
{
return PI*r*r;
}
friend ostream &operator<<(ostream &,const &);
};

ostream &operator<<(ostream &show.const Point &p)
{
show<<"("<<c.x<<","<<c.y<<")",半径"<<c.r<<",周长:"<<
c.circmferen()<<"面积:"<<c.area()<<endl;
return show;
}
class Cylinder:public Circle
{
private:
int h;
public:
Cylinder(int xx=0,int yy=0,int rr=0,int hh=0):Circle(xx,yy,rr)
{
h=hh;
}
int Geth()
{
return h;
}
double Cy_area() const
{
return 2*Circle::area()+2*PI*r*h;
}
double volume()
{
return Circle::area()*h;
}
friend ostream &operator<<(ostream &,const Point &);
};
ostream &operator<<(ostream &show.const Point &cy)
{
show<<"("<<cy.x<<","<<cy.y<<")",半径"<<cy.r<<",表面积:"<<cy.Cy_area()
<<"体积:"<<cy.volume()<<endl;
return show;
}

int main()
{
Point objP(3,5);
// cout<<"点类的对象:"<<objP;
cout<<"横坐标的值:"<<objP.Getx()<<" ";
cout<<"纵坐标的值:"<<objP.Gety()<<endl;

Circle objC(3,5,9);
// cout<<"圆类的对象:"<<objC;
cout<<"圆的半径:"<<objC.Getr()<<endl;
cout<<"圆的周长:"<<objC.circumferen()<<end;;
cout<<"圆的面积:"<<objC.area()<<endl;

Cylinder objCy(3,5,9,12);
// cout<<"圆柱类的对象:"<<objCy;
cout<<"圆柱的表面积:"<<objCy.Cy_area()<<end;;
cout<<"圆柱的体积:"<<objC.volume()<<endl;

return 1;
}

 

posted @ 2019-06-30 16:52  bobo哥  阅读(808)  评论(0编辑  收藏  举报