thinking in C++

来源:互联网 发布:java 发送邮件 maven 编辑:程序博客网 时间:2024/06/08 11:14

第三章:C++中的C

3.1创建函数:

  • 旧版本中的C,可以带任意个数和类型的参数调用函数,编译器都不会出错,缺乏对参数传递的协助以及会导致高深莫测的故障,所以当时被称为高级汇编语言。
  • 函数原型,用参数类型确保正确的处理返回值,传递参数。
  • 参数表中包含了应当传递给函数的参数类型和参数的标识符(对声明而言任选)
  • 参数的顺序和类型必须在声明,定义与函数调用中相匹配
  • 在C++中,函数定义可以用未命名的参数,当然,因为未命名,在函数体中无法使用,目的,为了将参数列表保留位置。
  • c中function()与c++中function()的区别。
  • 可变参数列表使用,

可以拒绝错误检查功能。

#include< iostream>  using namespace std;int add(int a, int b , ...)  

{
return a + b ;
}

int main(){      int final = add(1.7,2.5,4);      cout<<final;      return 0;  }  
3.1.1函数的返回值
  • C++中函数原型必须指明函数的返回类型(在C中,如果省略返回值,表示默认为整型)
  • 在一个函数定义中可以有多个return 语句
3.1.2使用C的函数库
  • 如果希望该应用程序具有可移植性,就应该限制使用编译器以自带非标准库函数。
  • 如果必须执行特定平台的活动,应当尽力把代码隔离在某一场所,以便移植到另一平台时容易进行修改.在C++中,经常把特定的活动封装在一个类中,这是一个理想的解决办法。
  • 包含头文件,调用函数
3.1.3通过库管理器创建自己的库
  • 如果想要创建一个库,那么就建立一个头文件,它包含库中的所有函数原型,把所有的对象模块联通建成后的库名传递给库管理器,将建成的库与其他库放在同一个位置,一边连接器能发现它
3.2一览
  • for(int i = 0 ; i < 10 ; i++ )是可以的。
  • 作用域。

3.4 数据类型简介

  • 数据类型定义使用存储空间(内存)的方式,通过定义数据类型,告诉编译器怎样创建一片特定的存储空间,以及怎样操纵这篇存储空间。
  • 内建数据类型是编译器本来能理解的数据类型,直接与编译器相关联
  • 用户定义的数据类型是我们与别的1程序员创建道德类型,作为一个类,一般被称为抽象数据类型。
3.4.1.基本内建类型
  • 标准C的内建类型规范不说明每一个内建类型必须有多少位,规范只规定内建类型必须能存储的最大值和最小值。
BCD
二进制编码的十进制(Binary-Coded Decimal)
- 机器基于二进制,则最大值可以直接转换为容纳这个值的所需的最少位数,若基于BCD,在机器中容纳每一种数据类型的最大数值的空间是不同的。详见< climits> 与< cfloat>
- the code
#include<iostream>#include<string>#include <limits>using namespace std;int main(){    cout << "type: \t\t" << "************size**************" << endl;    //about the bool    cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);    cout << "\t最大值:" << (numeric_limits<bool>::max)();    cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;    //about the char    cout << "char: \t\t" << "所占字节数:" << sizeof(char);    cout << "\t最大值:" << (numeric_limits<char>::max)();    cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;    //about the signed char    cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);    cout << "\t最大值:" << (numeric_limits<signed char>::max)();    cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;    //about the unsigned char    cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);    cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();    cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;    //about the wchar_t    cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);    cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();    cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;    //about the short    cout << "short: \t\t" << "所占字节数:" << sizeof(short);    cout << "\t最大值:" << (numeric_limits<short>::max)();    cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;    //about the int    cout << "int: \t\t" << "所占字节数:" << sizeof(int);    cout << "\t最大值:" << (numeric_limits<int>::max)();    cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;    //about the unsigned int    cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);    cout << "\t最大值:" << (numeric_limits<unsigned>::max)();    cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;    //about the long    cout << "long: \t\t" << "所占字节数:" << sizeof(long);    cout << "\t最大值:" << (numeric_limits<long>::max)();    cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;    //about the unsigned long    cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);    cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();    cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;    //about the double    cout << "double: \t" << "所占字节数:" << sizeof(double);    cout << "\t最大值:" << (numeric_limits<double>::max)();    cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;    //about the long double    cout << "long double: \t" << "所占字节数:" << sizeof(long double);    cout << "\t最大值:" << (numeric_limits<long double>::max)();    cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;    //about the float    cout << "float: \t\t" << "所占字节数:" << sizeof(float);    cout << "\t最大值:" << (numeric_limits<float>::max)();    cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;    //about the size_t    cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);    cout << "\t最大值:" << (numeric_limits<size_t>::max)();    cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;    //about the string    cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;    // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;    cout << "type: \t\t" << "************size**************" << endl;    return 0;}
  • the result
    result
3.4.1bool类型与true 和false
元素 布尔类型的用法 &&,||,! 带布尔参数并产生bool结果 if,for,while,do 条件表达式转化为bool值 >,<,<=,>=,==,!= 产生bool结果 ?: 第一个操作数转化为bool

- 编译器隐式转换intbool

0 0
原创粉丝点击