Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4247884
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-04-20 11:02:02


派生类对象的初始化 实验



  1. #include <iostream>
  2. #include <cstring>

  3. using namespace std;
  4. class A
  5. {
  6.  public:
  7.          A(int xx);
  8.  protected:
  9.         int x;
  10. };

  11. A::A(int xx)
  12. {
  13.     x=xx;
  14. }

  15. class B
  16. {
  17.  public:
  18.        B(int yy);
  19.  protected:
  20.        int y;
  21. };

  22. B::B(int yy)
  23. {    
  24.        y=yy;
  25. }

  26. class C :public A,public B
  27. {
  28.  public:
  29.        C(int xx,int yy,int zz);
  30.        void output();
  31.        int z;
  32. };

  33. C::C(int xx,int yy,int zz):A(xx),B(yy)
  34. {
  35.     z=zz;
  36. }

  37. void C::output()
  38. {
  39.       cout<<x<<" "<<y<<" "<<z<<endl;
  40. }

  41. int main()
  42. {
  43.     C c(1,1,1); //有参构造函数,所以一定要传递初始化 参数
  44.     c.output();    
  45.     return 0;
  46. }



  1. ywx@yuweixian:~/yu/c++/pais$ ./pshi
  2. 1 1 1
  3. ywx@yuweixian:~/yu/c++/pais$



阅读(971) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~