C++基础篇

来源:互联网 发布:淘宝卖lol龙虾啥是抽 编辑:程序博客网 时间:2024/06/05 05:31
==================          第一部分    ==============================
[cpp] view plaincopy
  1. #include <iostream.h>  
  2.   
  3. //////////////////////////////////////////////////////////////////////////  
  4. // 基类(父类)  
  5. class Animal  
  6. {  
  7. public:  
  8.     Animal()  
  9.  {  
  10.   cout << "animal 结构体" << endl ;  // 第一  
  11.  }  
  12.  ~Animal()  
  13.  {  
  14.   cout << "animal 析构函数" << endl;  // 第四  
  15.  }  
  16.   
  17. protected:  
  18. private:   
  19. };  
  20.   
  21. //////////////////////////////////////////////////////////////////////////  
  22. // 派生类(子类)  
  23. class Fish : public Animal  
  24. {  
  25. public:  
  26.  Fish()  
  27.  {  
  28.   cout << "Fish 结构体" << endl; // 第二  
  29.  }  
  30.     
  31.  ~Fish()  
  32.  {  
  33.   cout << "Fish 析构函数" << endl; // 第三  
  34.  }  
  35.   
  36.   
  37. protected:  
  38. private:  
  39.    
  40. };  
  41.   
  42. //////////////////////////////////////////////////////////////////////////  
  43.  // 主函数  
  44. int main()  
  45. {  
  46.  Fish  fs;  
  47.  return 0;  
  48. }  



 

输出结果:

animal 结构体
Fish 结构体
Fish 析构函数
animal 析构函数

流程:先执行    父类构造函数→子类构造函数 →  子类析构函数  → 父类析构函数。

 

=======================================  第二部分:函数重载(发生内部类) ======================================

函数重载构成条件:1、函数的参数类型不能相同;   2、函数的参数个数不能相同。

 

[cpp] view plaincopy
  1. class Test  
  2. {  
  3.  int x,y;  
  4. public:  
  5.     Test()  
  6.  {  
  7.     x = 10;  
  8.     y = 20;  
  9.  }  
  10.  void sum()  
  11.  {  
  12.   cout << this->x + this->y <<  endl;  
  13.  }  
  14.  void sum(int x, int y)  
  15.  {  
  16.   this->x = x;  
  17.   this->y = y;  
  18.         cout << this->x + this->y << endl;  
  19.  }  
  20. };  
  21.    
  22. //////////////////////////////////////////////////////////////////////////  
  23.  // 主函数  
  24. int main()  
  25. {  
  26.  Test test;  
  27.  test.sum();    // 重载 不带参数  
  28.  test.sum(8,3); // 重载带参数  
  29.  return 0;  
  30. }  



注意:以下两种情况不能够构成函数重载:

1、void   add();

      int     add();

 

2、 int  add( int  a , int b = 5 );

      int   add( int a);

 

=======================================  第三部分:函数覆盖(发生在父类与子类之间)=======================================

函数覆盖:子类与父类方法名称相同。

好处:当子类要特有自己的行为时候,可以通过函数覆盖来得到。
// 基类(父类)

[cpp] view plaincopy
  1. class Animal  
  2. {  
  3. public:  
  4.     Animal()   
  5.  {     
  6.  }  
  7.    
  8.  void breathe()   
  9.  {  
  10.     cout << "animal 呼吸方法"  << endl;  
  11.  }  
  12.    
  13.  ~Animal()  
  14.  {  
  15.  }  
  16. };  
  17.   
  18. //////////////////////////////////////////////////////////////////////////  
  19. // 派生类(子类)  
  20. class Fish : public Animal  
  21. {  
  22. public:  
  23.  Fish()  
  24.  {  
  25.  }  
  26.     
  27.  void breathe()  
  28.  {  
  29.              cout << "Fish breathe() 呼吸方法" << endl;  
  30.  }  
  31.       
  32.  ~Fish()  
  33.  {  
  34.  }  
  35. };//////////////////////////////////////////////////////////////////////////  
  36.  // 主函数  
  37. int main()  
  38. {  
  39.  Fish  fs; // 构造 Fish 对象  
  40.  fs.breathe();  // 执行子类的 方法  
  41.  return 0;  
  42. }  


 


 输出结果: Fish breathe() 呼吸方法

 

======================================= 第四部分:多态性 =======================================

 

 

[cpp] view plaincopy
  1. #include <iostream.h>  
  2.   
  3. //////////////////////////////////////////////////////////////////////////  
  4. // 基类(父类)  
  5. class Animal  
  6. {  
  7. public:  
  8.     Animal(int hight , int weight)   
  9.  {     
  10.    
  11.  }  
  12.    
  13.  void breathe()   
  14.  {  
  15.     cout << "animal 呼吸方法"  << endl;  
  16.  }  
  17.    
  18.  ~Animal()  
  19.  {  
  20.   
  21.  }  
  22.   
  23. protected:  
  24. private:   
  25.   
  26. };  
  27.   
  28. //////////////////////////////////////////////////////////////////////////  
  29. // 派生类(子类)  
  30. class Fish : public Animal  
  31. {  
  32. public:  
  33.  Fish():Animal(100,200)  
  34.  {  
  35.   
  36.  }  
  37.     
  38.  void breathe()  
  39.  {  
  40.         cout << "Fish breathe() 呼吸方法" << endl;  
  41.  }  
  42.       
  43.  ~Fish()  
  44.  {  
  45.  }  
  46.   
  47.   
  48. protected:  
  49. private:  
  50.    
  51. };  
  52.   
  53.    
  54. //////////////////////////////////////////////////////////////////////////  
  55.  // 主函数  
  56. int main()  
  57. {  
  58.   Fish  fs; // 构造 Fish 对象  
  59.   Animal *pAnimal;  // 构造Animal指针  
  60.   pAnimal = &fs; // Fish对象地址赋给pAnimal  
  61.   pAnimal->breathe();  
  62.   return 0;  
  63. }  



 

 

 输出结果:animal 呼吸方法
1.为什么输出的结果不是 Fish breathe() 呼吸方法 ?

           Fish 和 Animal 首地址是一样的,都是Animal  对象内存地址。

          Fish 对象内存布局如下:     


Animal 对象内存

 

Fish 继承部分

                               

 

 

      2.  假如想要输出 Fish 对象的呼吸方法怎么办?

              可以在父类呼吸方法加上关键字 virtual    。

        
// 基类(父类)

[cpp] view plaincopy
  1. class Animal  
  2. {  
  3.   .... //  这里加上 关键字 virtual   
  4.  virtual void breathe()   
  5.  {  
  6.     cout << "animal 呼吸方法"  << endl;  
  7.  }  
  8.   ....};  



此时输出结果是:Fish breathe() 呼吸方法。

多态性总结:当基类函数是虚函数时,如果子类有基类的方法,则调用子类的方法;如果子类没有基类的方法,则调用父类的方法。

 

=====================================   第五部分:抽象(纯虚函数) =====================================

 

如果一个类中至少有一个纯虚函数,那么这个类叫做抽象类(abstract)。

抽象类不能够实例化对象:
// 基类(父类)

[cpp] view plaincopy
  1. class Animal  
  2. {  
  3. public:  
  4.     Animal()   
  5.  {     
  6.  }  
  7.   
  8.  virtual void breathe() = 0 ;  
  9.    
  10.  ~Animal()  
  11.  {  
  12.  }  
  13. };  
  14.   
  15. // 派生类(子类)  
  16. class Fish : public Animal  
  17. {  
  18. public:  
  19.    Fish()  
  20.     {  
  21.     }   ~Fish()  
  22.    {  
  23.    }  
  24. };  
  25.  // 主函数  
  26. int main()  
  27. {  
  28.   Animal animal;  // 不能实例化对象          
[cpp] view plaincopy
  1. Fish fs;        // 同样不能实例化对象。因为 子类Fish 是从 父类Animal 继承而来,所以Fsih 也变成了抽象类。   
[cpp] view plaincopy
  1.   return 0;  
  2. }  
  3.   
  4.   
  5.  编译错误提示:cannot instantiate abstract class due to following members:  
  6.   
  7. 要想子类Fish 不变成抽象类,子类就要把 父类的纯虚函数breathe() 实现。  
  8.   
  9. 子类改为:  
  10. // 派生类(子类)  
  11. class Fish : public Animal  
  12. {  
  13. public:  
  14.  Fish()  
  15.  {  
  16.  }  
  17.  void breathe()  
  18.  {  
  19.         cout << "Fish breathe() 呼吸方法" << endl;  
  20.  }  
  21.  ~Fish()  
  22.  {  
  23.  }  
  24. };  



 


=====================================  第六部分: 带参数的基类(父类)构造函数 =======================================

[cpp] view plaincopy
  1. #include <iostream.h>  
  2.   
  3. //////////////////////////////////////////////////////////////////////////  
  4. // 基类(父类)  
  5. class Animal  
  6. {  
  7. public:  
  8.  Animal(int hight , int weight)  
  9.  {  
  10.   cout << "animal 结构体" << endl ;  // 第一  
  11.  }  
  12.    
  13.  void breathe()   
  14.  {  
  15.   cout << "animal 呼吸方法"  << endl;  
  16.  }  
  17.    
  18.       
  19.  ~Animal()  
  20.  {  
  21.   cout << "animal 析构函数" << endl;  // 第四  
  22.  }  
  23.   
  24. protected:  
  25. private:   
  26. };  
  27.   
  28. //////////////////////////////////////////////////////////////////////////  
  29. // 派生类(子类)  
  30. class Fish : public Animal  
  31. {  
  32. public:  
  33.  Fish()  
  34.  {  
  35.   cout << "Fish 结构体" << endl; // 第二  
  36.  }  
  37.     
  38.  void breathe()  
  39.  {  
  40.         cout << "Fish breathe() 呼吸方法" << endl;  
  41.  }  
  42.       
  43.  ~Fish()  
  44.  {  
  45.   cout << "Fish 析构函数" << endl; // 第三  
  46.  }  
  47.   
  48.   
  49. protected:  
  50. private:  
  51.    
  52. };  
  53.   
  54. //////////////////////////////////////////////////////////////////////////  
  55.  // 主函数  
  56. int main()  
  57. {  
  58.      Fish  fs;  
  59.      return 0;  
  60. }  


 


 编译提示:'Animal' : no appropriate default constructor available。(没有适合的默认构造函数)

更正如下:
//////////////////////////////////////////////////////////////////////////
// 派生类(子类)

[cpp] view plaincopy
  1. class Fish : public Animal  
  2. {  
  3. public:        // 子类 向  基类的带参数构造函数  传递参数。        // 当构造 Fish 对象时候,会调用带参数的Animal(int ,int)。   
[cpp] view plaincopy
  1.  Fish():Animal(100,200)  
  2.  {                // 注意:还没有进入到这里之前先调用 Animal  
  3.         cout << "Fish 结构体" << endl; // 第二  
  4.  }  
  5.     
  6.  void breathe()  
  7.  {  
  8.         cout << "Fish breathe() 呼吸方法" << endl;  
  9.  }  
  10.       
  11.  ~Fish()  
  12.  {  
  13.   cout << "Fish 析构函数" << endl; // 第三  
  14.  }  
  15.   
  16.   
  17. protected:  
  18. private:  
  19.    
  20. };  


 

输出结果:(同上)

animal 结构体
Fish 结构体
Fish 析构函数
animal 析构函数


 

=====================================第五部分:类常量成员初始化=======================

 

1、第一种:

[cpp] view plaincopy
  1. class Test  
  2. {  
  3. public:  
  4.  int sum;  
  5. public:  
  6.     Test()  
  7.  {  
  8.            this.sum = 10; // 初始化 成员变量  
  9.  }  
  10. };  
  11. 第二种:  
  12. class Test  
  13. {  
  14. public:  
  15.  int sum;// 定义变量public:  
  16.     Test():sum(10) // 初始化 成员变量  
  17.  {  
  18.        
  19.  }  
  20. };  
  21.   
  22.     


 

另外:

静态成员比较特殊,可以直接在类外面初始化,但是要指明其域。

 

[cpp] view plaincopy
  1. class Test  
  2. {  
  3. public:  
  4.    static const int sum;  
  5. public:  
  6.     Test()   
  7.  {  
  8.            cout << this.sum<<endl; // 输出 88  
  9.  }  
  10. };  
  11. const int Test::sum = 88;  // 初始化 静态类成员     

0 0