c vs c++ in strcut and class

总习惯用c的用法,现在学习C++,老爱拿来比较。声明我用的是g++4.2.1 SUSE Linux。看例子吧

  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. enum zoo_obj_kind{
  8.         null = 0,
  9. #define null null
  10.         no = 0,
  11. #define no no
  12.         animal = 2,
  13. #define animal animal
  14.         plant = 4,
  15. #define plant plant
  16.         others = 8
  17. #define others others
  18.  
  19. };
  20.  
  21. struct zoo_obj{
  22.         zoo_obj_kind zo_kind;
  23.         char name [40];
  24. };
  25. class zoo_obj_1{
  26.         zoo_obj_kind zo_kind;
  27.         char name [40];
  28.  
  29. };
  30.  
  31. int main(void){
  32.  
  33.         cout << "struct :" << sizeof(struct zoo_obj) << endl;
  34.         cout << "clsas :" << sizeof( zoo_obj_1) << endl;
  35.  
  36.  
  37. }
结果
  1. struct size:44
  2. clsas size:44
-------------------------------
  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. enum zoo_obj_kind{
  8.         null = 0,
  9. #define null null
  10.         no = 0,
  11. #define no no
  12.         animal = 2,
  13. #define animal animal
  14.         plant = 4,
  15. #define plant plant
  16.         others = 8
  17. #define others others
  18.  
  19. };
  20.  
  21. struct zoo_obj{
  22.         zoo_obj_kind zo_kind;
  23.         char name [40];
  24.         void (*say)(struct zoo_obj *);
  25. };
  26. void say(struct zoo_obj *obj){
  27.         if(!obj) {
  28.                 printf("null\n");
  29.                 return ;
  30.         }
  31.         printf("name:%s\n",obj->name);
  32.  
  33. }
  34. class zoo_obj_1{
  35.         zoo_obj_kind zo_kind;
  36.         char name [40];
  37.         void say(zoo_obj_1 &obj){
  38.                 cout << "name:" << name << endl;
  39.  
  40.         }
  41. };
  42.  
  43. int main(void){
  44.  
  45.         cout << "struct :" << sizeof(struct zoo_obj) << endl;
  46.         cout << "clsas :" << sizeof( zoo_obj_1) << endl;
  47.  
  48.  
  49. }
结果
  1. struct size:48
  2. clsas size:44
呵呵,有意思吧,在class中成员函数不占空间。下面你看看他们有多像
 
  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. enum zoo_obj_kind{
  8.         null = 0,
  9. #define null null
  10.         no = 0,
  11. #define no no
  12.         animal = 2,
  13. #define animal animal
  14.         plant = 4,
  15. #define plant plant
  16.         others = 8
  17. #define others others
  18.  
  19. };
  20.  
  21. struct zoo_obj{
  22.         zoo_obj_kind zo_kind;
  23.         char name [40];
  24.         void (*say)(struct zoo_obj &);
  25. };
  26. void say(struct zoo_obj &obj){
  27.         printf("name:%s\n",obj.name);
  28.  
  29. }
  30. class zoo_obj_1{
  31.         public:
  32.         zoo_obj_kind zo_kind;
  33.         char name [40];
  34.         void say(){cout << "name:" << name << endl;}
  35.         void say(zoo_obj_1 &obj){cout << "name:" << obj.name << endl;}
  36. };
  37.  
  38. typedef struct zoo_obj s_zoo_obj;
  39. typedef zoo_obj_1 c_zoo_obj;
  40.  
  41. int main(void){
  42.  
  43.         s_zoo_obj s_obj = {animal,"dog",say};
  44.         zoo_obj_1 c_obj = {animal,"cat"};
  45.  
  46.         cout << "struct size:" << sizeof(struct zoo_obj) << endl;
  47.         cout << "clsas size:" << sizeof( zoo_obj_1) << endl;
  48.  
  49.         s_obj.say(s_obj);
  50.         c_obj.say(c_obj);
  51.  
  52. }
结果
  1. struct size:48
  2. clsas size:44
  3. name:dog
  4. name:cat
这是同时使用了引用,那么指针呢。struct的指针当然没有问题,那么class的指针呢?看看代码
  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. enum zoo_obj_kind{
  8.         null = 0,
  9. #define null null
  10.         no = 0,
  11. #define no no
  12.         animal = 2,
  13. #define animal animal
  14.         plant = 4,
  15. #define plant plant
  16.         others = 8
  17. #define others others
  18.  
  19. };
  20.  
  21. struct zoo_obj{
  22.         zoo_obj_kind zo_kind;
  23.         char name [40];
  24.         void (*say)(struct zoo_obj *);
  25. };
  26. void say(struct zoo_obj *obj){
  27.         !obj
  28.         ? printf("null\n")
  29.         : printf("name:%s\n",obj->name);
  30.  
  31. }
  32. class zoo_obj_1{
  33.         public:
  34.         zoo_obj_kind zo_kind;
  35.         char name [40];
  36.         void say(){cout << "name:" << name << endl;}
  37.         void say(zoo_obj_1 *obj){
  38.                 !obj
  39.                 ? cout << "null\n"
  40.                 : cout << "name:" << obj->name << endl;
  41.         }
  42. };
  43.  
  44. typedef struct zoo_obj s_zoo_obj;
  45. typedef zoo_obj_1 c_zoo_obj;
  46.  
  47. int main(void){
  48.  
  49.         s_zoo_obj s_obj = {animal,"dog",say};
  50.         zoo_obj_1 c_obj = {animal,"cat"};
  51.  
  52.         cout << "struct size:" << sizeof(struct zoo_obj) << endl;
  53.         cout << "clsas size:" << sizeof( zoo_obj_1) << endl;
  54.  
  55.         s_obj.say(&s_obj);
  56.         c_obj.say(&c_obj);
  57.  
  58.         s_obj.say(NULL);
  59.         c_obj.say(NULL);
  60.  
  61. }
哈哈,结果仍然是
 
  1. struct size:48
  2. clsas size:44
  3. name:dog
  4. name:cat
更高级的特性呢?
比如在继承,接口。。。
个人认为C的这类名词没有,但他确实能出色的实现诸如此类的功能,而且更直观。可能是我的C++还不行吧。C高效和简单直观是没得说的。