zoukankan      html  css  js  c++  java
  • c++之继承二

    下面的实例是使用继承完成点、圆、圆柱体的层次结构

      1 #include <iostream>
      2 using namespace std;
      3 #define PI 3.1415926
      4 
      5 class Point
      6 {
      7     friend ostream& operator << (ostream& output,Point&);
      8 public:
      9     Point(int x =0,int y =0);    //带默认参数的构造函数
     10     void setPoint(int x,int y);    //设置点坐标
     11     int getX();    //得到点的x轴坐标
     12     int getY();    //得到点的y轴坐标
     13 protected:    //成员函数,x,y轴的坐标
     14     int x,y;
     15 };
     16 
     17 class Circle: public Point        //类Circle公有继承Point
     18 {
     19     friend ostream& operator << (ostream& output,Circle&);
     20 public:
     21     Circle(double r = 0.0,int x =0,int y =0);    //带默认参数的构造函数
     22     void setRadius(double r);    //设置圆的半径
     23     double getRadius();        //得到圆的半径
     24     double getArea();        //得到圆的面积
     25 protected:
     26     double radius;        //成员函数,半径
     27 };
     28 
     29 class Cylinder:public Circle    //类Cylinder继承Circle
     30 {
     31     friend ostream& operator <<(ostream& output,Cylinder&);
     32 public:
     33     Cylinder(double height=0.0,double radius =0.0,int x=0,int y=0);        //带默认参数的构造函数
     34     void setHeight(double height);    //设置圆柱体高度值
     35     double getHeight();    //返回圆柱体的高度值
     36     double getArea();    //返回圆柱体的面积
     37     double getVolume();    //返回圆柱体的体积
     38 protected:
     39     double height;    //成员函数,圆柱体的高度
     40 };
     41 
     42 //Point类的实现
     43 Point::Point(int x,int y)
     44 {
     45     this->x = x;
     46     this->y = y;
     47 }
     48 void Point::setPoint(int x,int y)
     49 {
     50     this->x = x;
     51     this->y = y;
     52 }
     53 int Point::getX()
     54 {
     55     return this->x;
     56 }
     57 int Point::getY()
     58 {
     59     return this->y;
     60 }
     61 ostream& operator <<(ostream& output,Point& point)
     62 {
     63     cout << "[point: x="<<point.getX()<<" ,"<<"y="<<point.getY() << "]" <<endl;
     64     return output;
     65 }
     66 
     67 //Circle类的实现
     68 Circle::Circle(double radius,int x,int y):Point(x,y)
     69 {
     70     this->radius = radius;
     71 }
     72 void Circle::setRadius(double radius)
     73 {
     74     this->radius = radius;
     75 }
     76 double Circle::getRadius()
     77 {
     78     return this->radius;
     79 }
     80 double Circle::getArea()
     81 {
     82     return PI * radius * radius;
     83 }
     84 ostream& operator <<(ostream& output,Circle& circle)
     85 {
     86     cout << "[circle: x=" << circle.getX() << " ,y=" <<circle.getY() <<" ,radius=" << circle.getRadius() << " ,area=" <<circle.getArea() << "]"<<endl;
     87     return output;
     88 }
     89 
     90 //类Cylinder的实现
     91 Cylinder::Cylinder(double height,double radius,int x,int y):Circle(radius,x,y)
     92 {
     93     this->height = height;
     94 }
     95 
     96 void Cylinder::setHeight(double height)
     97 {
     98     this->height = height;
     99 }
    100 double Cylinder::getHeight()
    101 {
    102     return this->height;
    103 }
    104 double Cylinder::getArea()
    105 {
    106     return 2*Circle::getArea()+2 * PI * radius * height;
    107 }
    108 double Cylinder::getVolume()
    109 {
    110     return Circle::getArea()*height;
    111 }
    112 ostream& operator <<(ostream& output,Cylinder& cylinder)
    113 {
    114     cout << "[cylinder: x=" << cylinder.getX() << " ,y=" <<cylinder.getY() << " ,height=" << cylinder.getHeight() <<" ,radius=" << cylinder.getRadius() << " ,area=" <<cylinder.getArea() <<" ,volume=" << cylinder.getVolume()<< "]"<<endl;
    115     return output;
    116 }
    117 
    118 void main()
    119 {
    120     Point p ( 72, 115 ) ;        //定义点对象并初始化
    121    cout << "The initial location of p is " << p << endl ;
    122    p.setPoint ( 10, 10 ) ;        //置点的新数据值
    123    cout << "
    The new location of p is " << p << endl ;    //输出数据
    124    Circle c ( 2.5, 37, 43 ) ;    //定义圆对象并初始化
    125    cout<<"
    The initial location and radius of c are
    "<<c<<"
    Area = "<<c.getArea()<<"
    " ;
    126    //置圆的新数据值
    127    c.setRadius ( 4.25 ) ;
    128    c.setPoint ( 2, 2 ) ;
    129    //输出圆心坐标和圆面积
    130    cout<<"
    The new location and radius of c are
    "<<c<<"
    Area = "<<c.getArea()<< "
    " ;
    131    Cylinder cyl ( 5.7, 2.5, 12, 23 ) ;    //定义圆柱体对象并初始化
    132    //输出圆柱体各数据和表面积,体积
    133    cout << "
    The initial location, radius ang height of cyl are
    " << cyl
    134        << "Area = " << cyl.getArea() << "
    Volume = " << cyl.getVolume() << '
    ';
    135    //置圆柱体的新数据值
    136    cyl.setHeight ( 10 ) ;
    137    cyl.setRadius ( 4.25 ) ;
    138    cyl.setPoint ( 2, 2 ) ;
    139    cout << "
    The new location, radius ang height of cyl are
    " << cyl
    140        << "Area = " << cyl.getArea() << "
    Volume = "<<cyl.getVolume()<< "
    " ;
    141 } 
  • 相关阅读:
    JavaScript 开发进阶:理解 JavaScript 作用域和作用域链
    jquery插件 源码
    webkit浏览器渲染影响因素分析
    JS操作JSON总结
    转:ie6与firefox操作iframe中DOM节点的一点不同
    input:focus
    JS设计模式一:单例模式
    JavaScript 变量、函数与原型链
    让你提前认识软件开发(49):自己主动測试
    Docker 开源管理工具集锦
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4333991.html
Copyright © 2011-2022 走看看