C++程序员应该掌握的东西

C++程序员应该掌握的东西

一个C/C++程序员合不合格的必要条件,我觉得他应该理解了指针、理解了引用,知道通用类型数据在计算机中的内存表示方法,知道什么数据放在栈里,什么数据放在数据区,什么数据放在堆里,知道数据的对齐方式,知道怎么写代码更高效,知道对象的基本模型。下面通过实例逐个介绍这些大家应该掌握的内容:

1. 齐问题

       typedef union tagData1 {

           double a;

            char b;

            int c;

       } TData1;

       typedef union tagData2{

            char b;

           double a;

            int c;

       } TData2;

       typedef union tagData3 {

            char b;

            int c;

           double a;

       } TData3;

       printf(“%d %d %d”,sizeof(TData1), sizeof(TData2), sizeof(TData3));

 

       typedef struct tagData1 {

            double a;

            char b;

            int c;

       } TData1;

       typedef struct tagData2 {

            char b;

           double a;

            int c;

       } TData2;

       typedef struct tagData3 {

            char b;

            int c;

           double a;

       } TData3;

       printf(“%d %d %d”,sizeof(TData1), sizeof(TData2), sizeof(TData3));

2. 据在计算机中的表示问题    

    typedef union tagData1 {

             float a;

              unsigned char ch[4];

       } TData1;

       typedef union tagData2 {

              int a;

              unsignedchar ch[4];

       } TData2;

       TData1 data1;

       data1.a = -3.0;

    for (int i = 0; i < 4; i++)

          printf(“%02x ”, data1.ch[i]);

       TData2 data2;

       data2.a = -3;

       for (int i = 0; i < 4; i++)

          printf(“%02x ”, data2.ch[i]);

3. 据的存放位置

       static int a = 0;

       void funcint b, int c

       {

             static int d = 0;

             int e = 0;

             char *str = "hello";

             TData *pData = new TData;

       }

    class CData {

        public:

             void func(int bb, int cc)

             {

                 static int dd = 0;

                 int ee = 0;

                 char *str1 = "hello";

                 TData *pData1 = new TData;

             }

        private:

                 static int aa;

                 int ff;

        };

       a,b,c,d,e,str,pData,*str,*pData,分别存放在哪里呢?

         aa,bb,cc,dd,ee,ff,str1,pData1,*str1,*pData1存放在哪里呢?

4. 象模型问题

    class CBase {

       public:

            void f(){printf(“base f()\n”);}

            void g() {printf(“base g()\n”);}

       };

       class CDerived : public CBase{

           public:

            void f(){printf(“derived f()\n”);}

            void g() {printf(“derived g()\n”);}

       };

      

   CBase base;

       CDerived derived;

       base = derived;

       base.f();

       base.g();

       CBase *pBase = NULL;

       CDerived *pDerived = new CDerived();

       pBase = pDerived;

       pBase->f();

       pBase->g();

 

    class CBase {

           public:

            virtual void f() {printf(“base f()\n”);}

            virtual void g() { printf(“base g()\n”);}

       };

       class CDerived : public CBase{

           public:

            void f(){printf(“derived f()\n”);}

            void g() {printf(“derived g()\n”);}

       };

       CBase base;

       CDerived derived;

       base = derived;

       base.f();

       base.g();

       CBase *pBase = NULL;

       CDerived *pDerived = new CDerived();

       pBase = pDerived;

       pBase->f();

       pBase->g();

   base = *pDerived;

   base.f();

   base.g();

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值