Chinaunix首页 | 论坛 | 博客
  • 博客访问: 243650
  • 博文数量: 108
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 314
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-29 10:58
文章分类

全部博文(108)

文章存档

2015年(20)

2014年(88)

我的朋友

分类: 嵌入式

2014-04-01 21:45:50

  1. /***********************************************
  2. * 文件名:例一
  3. * 功能:复制构造函数
  4. * 说明:用一个已知的对象初始化另一个对象
  5.         该函数只有一个参数
  6. * 时间:2014-4-1 --xwg
  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. }
阅读(793) | 评论(0) | 转发(0) |
0

上一篇:tableWidget用法

下一篇:线程创建

给主人留下些什么吧!~~