一个C++对象赋值的例子

[复制链接]
833|4
 楼主| keer_zu 发表于 2015-11-25 20:36 | 显示全部楼层 |阅读模式
C++, ST, TE, TEST, AN
本帖最后由 keer_zu 于 2015-11-25 20:41 编辑

头文件:
  1. #ifndef ANIMAL_H
  2. #define ANIMAL_H

  3. //===============================================================
  4. //
  5. //                                  animal
  6. //                                 鍔ㄧ墿鍩虹被
  7. //
  8. //===============================================================
  9. //#include <strings.h>
  10. //#include <string.h>
  11. #include <string>
  12. using namespace std;
  13. typedef enum {
  14.         test0 = 0,
  15.         test1 = 1,
  16.         test2 = 2,
  17.         test3 = 3
  18. }TEST;
  19. class animal
  20. {
  21. private:
  22.         string m_s;
  23.         int m_foot;
  24.         int m_food;
  25.         TEST m_test;
  26.        
  27. public:
  28.         animal():m_foot(4),m_food(1),m_test(test2),m_s("Hell luojianfeng!\n"){}
  29.         animal& operator =(const animal& other);
  30.         void breathe();                // 闈炶櫄鍑芥暟
  31.         void set_foot(int foot){m_foot = foot;}
  32.         int get_foot(){return m_foot;}
  33.         void set_food(int food){m_food = food;}
  34.         int get_food(){return m_food;}
  35.         void set_test(TEST test){m_test = test;}
  36.         void set_s(string s){m_s = s;}
  37. };

  38. //===============================================================
  39. //
  40. //                                       animal
  41. //                                 楸肩被锛岄泦鎴愪簬鍔ㄧ墿鍩虹被
  42. //
  43. //===============================================================
  44. class fish : public animal
  45. {
  46. public:
  47.         void breathe();                // 闈炶櫄鍑芥暟
  48. };

  49. #endif


 楼主| keer_zu 发表于 2015-11-25 20:37 | 显示全部楼层
本帖最后由 keer_zu 于 2015-11-25 20:43 编辑

.cpp文件:
  1. #include "animal.h"
  2. #include <iostream>
  3. //#include "animal.h"

  4. using namespace std;

  5. //===============================================================
  6. //
  7. //                                  animal
  8. //                                 鍔ㄧ墿鍩虹被
  9. //
  10. //===============================================================

  11. void animal::breathe()
  12. {
  13.         cout << "animal breathe" << endl;
  14. }

  15. animal& animal::operator =(const animal& other)
  16. {

  17. }

  18. //===============================================================
  19. //
  20. //                                       animal
  21. //                                 楸肩被锛岄泦鎴愪簬鍔ㄧ墿鍩虹被
  22. //
  23. //===============================================================

  24. void fish::breathe()
  25. {
  26.         cout << "fish bubble" << endl;
  27. }



  28. void ExamAnimal()
  29. {
  30.         // 灏嗗瓙绫绘寚閽堢洿鎺ヨ祴缁欏熀绫绘寚閽堬紝涓嶉渶瑕佸己鍒惰浆鎹紝C++缂栬瘧鍣ㄨ嚜鍔ㄨ繘琛岀被鍨嬭浆鎹?
  31.         // 鍥犱负fish瀵硅薄涔熸槸涓€涓猘nimal瀵硅薄
  32.         animal* pAn;
  33.         fish* pfh = new fish;
  34.         pAn = pfh;
  35.         animal *an1 = new animal;
  36.         delete pfh;
  37.         animal an2 = *an1;
  38.         an1->set_test(test3);
  39.         an1->set_s("yes i an luojianfeng! hahahahahahahahahahahahahahaah,heheheheheheheheh");
  40.         animal *an3 = new animal;
  41.         *an3 = *an1;

  42.         pfh = NULL;
  43.        
  44.         // 灏嗗熀绫绘寚閽堢洿鎺ヨ祴缁欏瓙绫绘寚閽堬紝闇€瑕佸己鍒惰浆鎹紝C++缂栬瘧鍣ㄤ笉浼氳嚜鍔ㄨ繘琛岀被鍨嬭浆鎹?
  45.         // 鍥犱负animal瀵硅薄涓嶆槸涓€涓猣ish瀵硅薄
  46.         fish* fh1;
  47.         //animal* an1 = new animal;
  48.         // 淇敼澶勶細
  49.         // 杩涜寮哄埗绫诲瀷杞寲
  50.         fh1 = (fish*)an1;

  51.         delete an1;
  52.         an1 = NULL;
  53. }

  54. int main(int argc, char* argv[])
  55. {
  56.         ExamAnimal();

  57.         return 0;
  58. }



 楼主| keer_zu 发表于 2015-11-25 20:38 | 显示全部楼层
可以编译一下,用gdb看看执行每一条赋值后对象的结果。
 楼主| keer_zu 发表于 2015-11-26 09:39 | 显示全部楼层
yyy71cj 发表于 2015-11-26 09:05
你用的是什么字符集?

乱码了是吧
 楼主| keer_zu 发表于 2015-11-26 09:39 | 显示全部楼层
yyy71cj 发表于 2015-11-26 09:05
你用的是什么字符集?

乱码了是吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1488

主题

12953

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部