1. C和C++区别

C是结构化语言。C++是面向对象的语言。

2. C++中如何调用C编译器编译后的函数。

要在C++中添加extern “C”, 因为C++支持函数重载。编译后的函数的名字中包含参数的类型。C语言不支持函数重载。编译后,直接使用C++无法识别。

3. C++中的#ifndef #define #endif 的作用。

防止头文件被重复引用。

4. 宏定义问题

最小值的MIN(x,y)  (x<= y ? (x): (y))

最大值的MAX(x,y) (x>= y ? (x): (y))

5. C++语言中的常用变量的字节数。

#include<iostream>

void main()
{
    std::cout << "char            =" <<sizeof(char)<<std::endl;
    std::cout << "unsigned char   =" <<sizeof(unsigned char)<<std::endl;
    std::cout << "int             =" <<sizeof(int)<<std::endl;
    std::cout << "unsigned int    =" <<sizeof(unsigned int)<<std::endl;
    std::cout << "short int       =" <<sizeof(short int)<<std::endl;
    std::cout << "unsigned short int  =" <<sizeof(unsigned short int)<<std::endl;
    std::cout << "long int        =" <<sizeof(long int)<<std::endl;
    std::cout << "unsigned long int  =" <<sizeof(unsigned long int)<<std::endl;

    std::cout << "float           =" <<sizeof(float)<<std::endl;
    std::cout << "doulbe          =" <<sizeof(double)<<std::endl;
    std::cout << "long double     =" <<sizeof(long double)<<std::endl;
}

image

6. const的用法讨论。

(1) 定义const常量。

(2) const修饰函数的参数和返回值,函数的定义体。

7. mutable定义变量

mutalbe的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词。

#include <iostream>

class TestClass
{
public:
  TestClass();
  void Output() const;

private:
  mutable int m_iTimes;
};

TestClass::TestClass()
{
    m_iTimes = 0;
};

void TestClass::Output() const
{
  m_iTimes++;

  std::cout << "times = " << m_iTimes << std::endl;
};

void main()
{
    TestClass tc;
    tc.Output();
}

由于使用了mutable,const的函数可以对它进行修改。

输出为 times = 1;

posted on 2013-02-18 23:59  GreenLight  阅读(181)  评论(0)    收藏  举报