程序设计基本概念(2)-2.19

1. C和C++区别

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

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

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

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

防止头文件被重复引用。

  1. 宏定义问题

最小值的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;
原文链接: https://www.cnblogs.com/bruce81/archive/2013/02/18/2916392.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/78203

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午6:27
下一篇 2023年2月9日 下午6:27

相关推荐