zoukankan      html  css  js  c++  java
  • 22.C++- 继承与组合,protected访问级别

    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    class Object
    {
    protected:
           string mName;
           string mInfo;
    public:
            Object()
           {
                  mName="Object";
                  mInfo="";             
           }
           string name()
           {
                  return mName;
           }
           string info()
           {
                  return mInfo;
           }
    };
    
    class Point      : public Object
    {
    protected:
           int x;                     //坐标
           int y;
    
    public:
           Point(int x=0,int y=0)
           {
                  ostringstream s;
                  this->x=x;
                  this->y=y;
                  s<<"P("<<x<<","<<y<<")";  //坐标信息
                  mName="Point";
                  mInfo=s.str();
           } 
    };
    
    class Line : public Object
    {
    private:
           Point mP1;
           Point mP2;
    public:
           Line(Point p1,Point p2)
           {
                  ostringstream s;
                  mP1 =p1;
                  mP2 =p2;
                  s<<"Line from " <<p1.info() <<" to "<<p2.info();      //线的信息
                  mName ="Line";
                  mInfo =s.str();
           }
    };
    
    int main()
    {
           Point p1(2,3); 
           Point p2(6,3);
           Line  L(p1,p2);
    cout<<p1.name()<<":"<<endl; cout<<p1.info()<<endl; cout<<L.name()<<":"<<endl; cout<<L.info()<<endl; return 0; }

    文章来源:https://www.cnblogs.com/lifexy/p/8698104.html

  • 相关阅读:
    UVA10891
    UVA10453
    UVA 10201
    UVA10154
    UVA11137
    UVA10617
    UVA10271
    UVA10739
    UVA10306
    节流防抖
  • 原文地址:https://www.cnblogs.com/xiongjim/p/9673952.html
Copyright © 2011-2022 走看看