c++ 复制构造函数示例程序

来源:互联网 发布:淘宝新品上架怎么弄 编辑:程序博客网 时间:2024/05/16 14:09

/*******************************************************
 *author:彭晓林
 *copyright: 版权所有,翻版不究
 *function: 复制构造函数测试程序
 ******************************************************/

#include <iostream>
#include <string>

using namespace std;

class DEMO

 public:
  DEMO(int a, int b)
  {
   x = a;
   y = b;
  }
  DEMO(DEMO &demo)
  {
   x = demo.x;
   y = demo.y;

   cout<<"复制构造函数被调用"<<endl;
  }
 void print();

 private:
  int x;
  int y;
};

void DEMO::print()
{
 cout<<x<<endl;
 cout<<y<<endl;
}

int main()
{
 DEMO TestA(1,2); 
 TestA.print();
 cout<<endl;
 
 DEMO TestB(TestA);
 TestB.print();
 
 while(1);
}s

原创粉丝点击