Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7651345
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-03-29 19:33:16

  1. /****************************************************
  2. * 文件名:例八
  3. * 功能:对象的指针和对象的引用
  4. * 说明:使用对象指针作为函数的参数可以实现
  5.         参数值的双向传递
  6. * 时间:2011-3-29      --Lzy
  7. /****************************************************/
  8. #include <iostream.h>        //对象指针的使用


  9. class A
  10. {
  11. public:
  12.     A(int = 0, int = 0);
  13.     void display();
  14. private:
  15.     int x, y;
  16. };

  17. A::A(int xx, int yy)
  18. {
  19.      x = xx; y = yy;
  20. }

  21. void A::display()
  22. {
  23.     cout<<"x="<<x<<" y="<<y<<endl;
  24. }

  25. void main()
  26. {
  27.     A a(3, 5), *p;
  28.     p = &a;
  29.     p->display();
  30. }


  31. //对象指针作为函数的参数

  32. #include <iostream.h>
  33. #include <string.h>

  34. class student
  35. {
  36. private:
  37.     char name[10];
  38.     int rollnumber;
  39.     double score[3];

  40. public:
  41.     student(char *strl= "", int a= 01, double s1=0, double s2=0, double s3=0)
  42.     {
  43.         strcpy(name, strl);
  44.         rollnumber = a;
  45.         score[0] = s1;
  46.         score[1] = s2;
  47.         score[2] = s3;    
  48.     }
  49.     void dipsplay();
  50.     double GetScore1(){return score[0];}
  51.     double GetScore2(){return score[1];}
  52.     double GetScore3(){return score[2];}
  53. };

  54. void student::dipsplay()
  55. {
  56.     cout<<"Name:"<<name<<endl;
  57.     cout<<"Rollnumber:"<<rollnumber<<endl;
  58.     cout<<"Score:"<<score[0]<<'\t'<<score[1]<<'\t'<<score[2]<<'\t'<<endl;
  59. }

  60. double f (student *p)
  61. {
  62.     double ave;
  63.     ave = (p->GetScore1()+p->GetScore2()+p->GetScore3())/3;
  64.     return ave;
  65. }

  66. void main()
  67. {
  68.     student s1("Lzy", 18, 98, 97, 99);
  69.     student *p = &s1;
  70.     p->dipsplay();
  71.     cout<<"平均成绩:"<<f(p)<<endl;
  72. }

  73. //对象数组

  74. #include <iostream.h>

  75. class MyClass
  76. {
  77. private:
  78.     int x, y;

  79. public:
  80.     MyClass(int = 1, int = 1);
  81.     ~MyClass();
  82.     void set(int = 1, int = 1);
  83.     void display();
  84. };

  85. MyClass::MyClass(int xx, int yy)
  86. {
  87.     x = xx;
  88.     cout<<"构造函数被调用"<<endl;
  89. }

  90. MyClass::~MyClass()
  91. {
  92.     cout<<"析构函数被调用"<<x<<" "<<y<<endl;    
  93. }

  94. void MyClass::set(int xx, int yy)
  95. {
  96.     x = xx; y = yy;
  97. }

  98. void MyClass::display()
  99. {
  100.     cout<<x<<" "<<y<<endl;
  101. }

  102. void main()
  103. {
  104.     MyClass a[3];
  105.     for (int i = 0; i < 3; i++)
  106.         a[i].set(i*10, i+10);
  107.     for ( i = 0; i < 3; i++)
  108.         a[i].display();

  109. }
阅读(1541) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~