关于构造函数案例.pptVIP

  • 7
  • 0
  • 约3.3千字
  • 约 17页
  • 2016-09-21 发布于湖北
  • 举报
3.拷贝构造函数 注意一种特殊情形,重载运算符“=”与拷贝构造的关系 #include iostream.h class Point { public: Point(); Point(Point p); Point operator =(Point p) { this-X=p.X; this-Y=p.Y; coutcall the = operator function !endl; return *this; } private: int X,Y; }; Point::Point () { cout构造函数被调用!endl; } Point::Point (Point p) { X=p.X; Y=p.Y; cout拷贝构造函数被调用!endl; } void main() { Point p1,p2; //Point p2 = p1; 等价于Point p1(p2); p2=p1; } 运行结果 举例2:讨论构造函数的调用次序问题 class B1 //基类B1,构造函数有参数 {public: B1(int i) {coutconstructing B1 iendl;} }; class B2 //基类B2,构造函数有参数 {public: B2(int j) {coutconstructing B2 jendl;} }; clas

文档评论(0)

1亿VIP精品文档

相关文档