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高效和简单直观是没得说的。
 
 

c vs c++ in strcut and class的更多相关文章

  1. golang基础--strcut结构体

    结构体struct类似python语言中的类class,结构体类的元素可以是一个变量,或者函数或者其它的类型,好比python的属性和方法. // struct结构体,类似python语言中的clas ...

  2. go strcut 封装

    package model import "fmt" type person struct { Name string age int //其它包不能直接访问.. sal floa ...

  3. ASP.NET MVC5+EF6+EasyUI 后台管理系统(41)-组织架构

    系列目录 本节开始我们要实现工作流,此工作流可以和之前的所有章节脱离关系,也可以紧密合并. 我们当初设计的项目解决方案就是可伸缩可以拆离,可共享的项目解决方案.所以我们同时要添加App.Flow文件夹 ...

  4. Linux C++中的时间函数(转)

    http://net.pku.edu.cn/~yhf/linux_c/function/03.html   asctime(将时间和日期以字符串格式表示) 相关函数 time,ctime,gmtime ...

  5. 33个超级有用必须要收藏的PHP代码样例

    作为一个正常的程序员,会好几种语言是十分正常的,相信大部分程序员也都会编写几句PHP程序,如果是WEB程序员,PHP一定是必备的,即使你没用开发过大型软件项目,也一定多少了解它的语法. 在PHP的流行 ...

  6. [汇编与C语言关系]3. 变量的存储布局

    以下面C程序为例: #include <stdio.h> ; ; ; int c; int main(void) { ; char b[] = "Hello World" ...

  7. C库函数使用与总结之时间函数

    1. localtime(取得当地目前时间和日期) [头文件]#include <time.h> [函数原型]struct tm *localtime(const time_t * tim ...

  8. 一个asp采集程序

    <% if request.QueryString="" then url="http://www.hbcz.gov.cn:7001/XZQHQueryWAR/xx ...

  9. 接触Matlab5年一个总结(Matlab要掌握的一些要点 )

    阅读目录 前言 Matlab的开发环境与简单介绍 Matlab的常见命令 Matlab的灵魂-矩阵操作 Matlab的.m或.fig的编程与技巧 从大二开始接触到matlab,讲真,这是一个我觉得很良 ...

随机推荐

  1. 对背包问题(Knapsack Problem)的算法探究

    对背包问题(Knapsack Problem)的算法探究 至繁归于至简,这次自己仍然用尽可能易理解和阅读的解决方式. 1.问题说明: 假设有一个背包的负重最多可达8公斤,而希望在背包中装入负重范围内可 ...

  2. 三、nginx301跳转302跳转

    301跳转设置: server { listen 80; server_name downcc.com; rewrite ^/(.*) http://www.downcc.com/$1 permane ...

  3. JVM调优总结(四)-垃圾回收面临的问题

    如何区分垃圾 上面说到的“引用计数”法,通过统计控制生成对象和删除对象时的引用数来判断.垃圾回收程序收集计数为0的对象即可.但是这种方法无法解决循环引用.所以,后来实现的垃圾判断算法中,都是从程序运行 ...

  4. 基于visual Studio2013解决C语言竞赛题之0809链表排序

     题目

  5. C#中对文件的操作

    详细介绍参考:http://blog.csdn.net/wangyue4/article/details/4616801 源码举例: public class FileSystemManager { ...

  6. 幻世(OurDream)2D图形引擎使用教程9——处理操作输入(3)

    声明:本教程版权归Lizcst Software Lab所有,欢迎转载,但是转载必须保留本段声明文字,并注明文章来源:http://blog.csdn.net/kflizcst 谢谢合作! 现在我们该 ...

  7. 打算自己做app,你们做过吗?

    最近手头上资金不足,想自己搞一下app赚点广告费,解决一下自己的零花钱问题,各位有没有做自己的app呢?打算用有米广告,各位觉得怎么样? 你们的收益是如何呢?能在这里晒晒吗?

  8. uva 620 Cellular Structure

    题目连接:620 - Cellular Structure 题目大意:给出一个细胞群, 判断该细胞的可能是由哪一种生长方式的到的, 输出该生长方式的最后一种生长种类, "SIMPLE&quo ...

  9. 11586 - Train Tracks

    Problem J: Train Tracks Andy loves his set of wooden trains and railroad tracks. Each day, Daddy has ...

  10. 基于visual Studio2013解决面试题之0504单链表逆序

     题目