Chinaunix首页 | 论坛 | 博客
  • 博客访问: 630970
  • 博文数量: 1008
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-31 09:44
文章分类
文章存档

2012年(1008)

我的朋友

分类:

2012-08-01 11:26:23

  1. /***********************************************
  2. * 文件名:例七
  3. * 功能:复制构造函数
  4. * 说明:用一个已知的对象初始化另一个对象
  5.         该函数只有一个参数
  6. * 时间:2011-3-29 --Lzy
  7. /***********************************************/
  8. #include <iostream.h>

  9. class Complex
  10. {

  11. public:
  12.     Complex(double x, double y);
  13.     Complex(Complex & c);        //复制初始化构造函数

  14.     double GetRel(){return rel;}
  15.     double GetImg(){return img;}

  16. private:
  17.     double rel, img;
  18. };

  19. Complex::Complex(double x, double y)
  20. {
  21.     rel = x; img = y;
  22.     cout<<"构造函数被调用"<<endl;
  23. }

  24. Complex::Complex(Complex & c)
  25. {
  26.     rel = c.rel;
  27.     img = c.img;
  28.     cout<<"复制初始化构造函数被调用"<<endl;
  29. }

  30. void main()
  31. {
  32.     Complex c1(6.8, 7.23);
  33.     Complex c2(c1);        
  34.     cout<<"("<<c2.GetRel()<<" ,"<<c2.GetImg()<<")"<<endl;
  35. }


  36. //复制初始化构造函数其他应用

  37. #include <iostream.h>

  38. class Complex
  39. {

  40. public:
  41.     Complex();
  42.     Complex(double x, double y);
  43.     Complex(Complex & c);        //复制初始化构造函数

  44.     double GetRel(){return rel;}
  45.     double GetImg(){return img;}

  46. private:
  47.     double rel, img;
  48. };

  49. Complex::Complex()
  50. {
  51.     rel = 0; img = 0;
  52.     cout<<"缺省构造函数被调用"<<endl;
  53. }

  54. Complex::Complex(double x, double y)
  55. {
  56.     rel = x; img = y;
  57.     cout<<"构造函数被调用"<<endl;
  58. }

  59. Complex::Complex(Complex & c)
  60. {
  61.     rel = c.rel;
  62.     img = c.img;
  63.     cout<<"复制初始化构造函数被调用"<<endl;
  64. }

  65. Complex fun(Complex c)
  66. {
  67.     cout<<"在函数fun中"<<endl;
  68.     double x,y;
  69.     x = c.GetRel() * 10;
  70.     y = c.GetImg() + 100;
  71.     Complex temp(x, y);
  72.     return temp;
  73. }

  74. void main()
  75. {
  76.     Complex c1(6.8, 7.23), c2;
  77.     c2 = fun(c1);
  78.     cout<<"("<<c2.GetRel()<<" ,"<<c2.GetImg()<<")"<<endl;
  79. }
阅读(163) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~