码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-06-30 17:26:21      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:protected   public   obj   include   stream   return   circle   int   基类   

#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;
}

 

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

标签:protected   public   obj   include   stream   return   circle   int   基类   

原文地址:https://www.cnblogs.com/duanqibo/p/11110349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!