Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360911
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: C/C++

2011-08-17 16:49:38

  1. #include<stdio.h>
  2. #include<stdlib.h>//for malloc
  3. #include<string.h>//for memset
  4. #include<unistd.h>
  5. #include<sys/types.h>
  6. #include<stdint.h>
  7. #include<iostream>

  8. using namespace std;

  9. class base
  10. {
  11.     private:
  12.         int a;
  13.         int b;
  14.     public:
  15.         void show(int c,int d);
  16.         virtual void show1(int c,int d);
  17.         void show2(void);
  18.         base(int a,int b);
  19.         base(){a=1;b=2;cout<<"hello"<<endl;};//无参数构造方法
  20.         virtual ~base();//定义为虚函数,析构时先掉子类的析构,再调父类的析构
  21.     static void show3(int a,int b);
  22.     static int c;//这里只是声明并无空间
  23. };

  24. void base::show3(int a,int b)
  25. {
  26.     cout<<"this is show 3 a is "<<a<<"b is"<<b<<endl;    
  27. }

  28. void base::show2(void)
  29. {
  30.     cout<<"this is show2 in base!"<<endl;
  31.     base::c=123;
  32. }

  33. base::base(int a,int b)
  34. {
  35.     cout<<"base int a int b"<<endl;
  36.     this->a=a;
  37.     this->b=b;
  38. }
  39. /*
  40. base::base()
  41. {
  42.     cout<<"base no base"<<endl;
  43. }
  44. */
  45. void base::show(int c,int d)
  46. {
  47.     cout <<"c is"<<c<<"d is"<<d << endl;
  48. }

  49. void base::show1(int c,int d)
  50. {
  51.     cout <<"virtual method c is"<<a<<"d is"<<b<<endl;
  52. }

  53. base::~base()
  54. {
  55.     cout<<"break base"<<endl;
  56. }

  57. class child :public base
  58. {
  59.     private:
  60.         int a,b;
  61.     public:
  62.         void show(int c,int d);
  63.         virtual void show1(int c,int d);
  64.         child(int a,int b);
  65.         ~child();
  66. };


  67. child::child(int a,int b)
  68. {
  69.     this->a=111;
  70.     this->b=222;
  71. }



  72. void child::show(int c,int d)
  73. {
  74.     cout <<"c is"<<c<<"d is"<<d << endl;
  75. }

  76. void child::show1(int c,int d)
  77. {
  78.     cout<<"this is child virtual method c is"<<this->a<<"d is "<<this->b<<endl;
  79. }

  80. child::~child()
  81. {
  82.     cout<<"break child!"<<endl;
  83. }
  84.     

  85. int base::c;
  86. int main()
  87. {
  88.     //base *mybase=new base(4,5);
  89.     base *myNew=new child(1,2);
  90.     myNew->show(3,4);
  91.     myNew->show1(3,4);
  92.     myNew->show2();
  93.     base::show3(123,456);
  94. //    base::c=4;
  95.     cout<<"base c is "<<base::c<<endl;
  96.     delete(myNew);
  97.     //delete(mybase);
  98. }
阅读(1016) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~