写个简单的类继承看你能学到什么

class Point

{

public:

      Point():_x(0),_y(0){}

      Point(int x, int y):_x(x),_y(y){}

      Point(const Point& rhs)

      {

            *this = rhs;

      }    

      virtual ~Point(){}

      Point& operator=(const Point&);

      int X() const {return _x;}

      int Y() const {return _y;}

      virtual int Z() const =0;    

protected:

      int _x;

      int _y;

};

 

Point& Point::operator =(const Point &rhs)

{

      _x = rhs._x;

      _y = rhs._y;

      return *this;

}

 

class Point3d:public virtual Point

{

public:

      Point3d():_z(0){}

      Point3d(int x, int y, int z):Point(x,y),_z(z){}

      Point3d(const Point3d& rhs)//:Point(rhs), _z(rhs._z)

      {

            //如果如果给Point class定义了构造函数,但是没有明确定义default constructor

            //'Point::Point' : no appropriate default constructor available

            //因为除非构造在初始化列表里边直接完成,否则肯定是先缺省构造,然后再赋值

            *this = rhs;

      }

      Point3d& operator=(const Point3d&);

      virtual int Z() const {return _z;} 

protected:

      int _z;

};

 

Point3d& Point3d::operator =(const Point3d &rhs)

{

      Point::operator =(rhs);

      _z = rhs._z;

      return *this;

}

 

class Vertex:public virtual Point

{

public:

      Vertex():next(NULL){}

      Vertex(int x, int y):Point(x,y),next(NULL){}

      Vertex(const Vertex& rhs):Point(rhs),next(rhs.next){}

      ~Vertex()

      {

            if(next)

            {

                  delete next;

                  next = NULL;

            }

      }

      Vertex& operator=(const Vertex&);

      virtual int Z() const {return 0;}

      Vertex* Next() const {return next;}

protected:

      Vertex *next;

};

 

Vertex& Vertex::operator=(const Vertex &rhs)

{

      Point::operator =(rhs);

      next = rhs.next;

      return *this;

}

 

class Vertex3d:public Vertex, public Point3d

{

public:

      Vertex3d(){};

      Vertex3d(const Vertex& vtx, const Point3d& p3d)

      {

            //这样根本赋不进去值

            //(Vertex)(*this) = vtx;

            //(Point3d)(*this) = p3d;

           

            next = vtx.Next();

            //虚基类的data有且只有一套

            _x = p3d.X();

            _y = p3d.Y();

            _z = p3d.Z();

 

      }

      Vertex3d& operator=(const Vertex3d&);

     

      //如果不做明确定义

      // 'Vertex3d' : ambiguous inheritance of 'int Point::Z(void)'

      virtual int Z() const

      {

            return Point3d::Z();

      }

private:

      int mumble;

};

 

Vertex3d& Vertex3d::operator=(const Vertex3d& rhs)

{

      Vertex::operator =(rhs);

      Point3d::operator =(rhs);

      mumble = rhs.mumble;

      return *this;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值