c vs c++ in strcut and class

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