c++拥有更加严格的类型检查
#include <stdio.h>
#include <stdlib.h>
int main()
{
const int a = 10;
//int* pa = &a; //编译不能通过,必须得是const类型的
//char* p = malloc(100); //c++类型检查更加严格,编译不通过
int arr[2][3];
//int** pArr = arr; //无法从“int [2][3]”转换为“int **”
return 0;
}
c 语言的逻辑真假用 0 和非 0 来表示。而 C++中有了具体的类型,但其本质,仍是
一个 char 类型的变量,可以被0或者非0进行赋值。
#include <iostream>
using namespace std;
int main()
{
bool flag = true;
printf("sizeof(flag) = %d ,sizeof(bool) = %d\n",sizeof(flag), sizeof(bool)); //1 1
flag = 10;
printf("flag = %d\n", flag); //1
return 0;
}
c 语言中枚举本质就是整型,可用任意整型赋值。 C++中枚举变量,只能用被枚举出来的元素初始化。枚举的常被用来取代宏常量。
#include <iostream>
using namespace std;
enum gender { MALE, FEMALE };
int main()
{
enum gender g = MALE;
//g = 0; //编译不通过,无法从“int”转换为“gender”,类型检查更加的严格
return 0;
}
c 语言中表达式通常不能作为左值的,不可被赋值,C++中某些表达式是可以赋值的。
#include <iostream>
using namespace std;
int main(void)
{
int a, b = 10;
(a = b) = 100;
cout << "a = " << a << " b = " << b << endl; //a=100,b=10
(a < b ? a : b) = 500;
cout << "a = " << a << " b = " << b << endl; //a=100,b=500
return 0;
}
为了避免引起调用或语义上的混淆,C++11,此入了 nullptr 用于区分,NULL 和0。
#include <iostream>
using namespace std;
void func(int a) {
printf("void func(int a)");
}
void func(int* p) {
printf("void fuoo(int *p)");
}
int main()
{
func(0);
printf("\n-----\n"); //调用void func(int a)
func(NULL);
printf("\n++++++++\n");//调用void func(int a)
func(nullptr); //调用void func(int* p)
return 0;
}
cin 和 cout 是 C++的标准输入和输出流对象。他们在头文件 iostream 中定义,
其意义作用类似于 c 语言中的 scanf 和 printf。
#include <iostream>
using namespace std;
using namespace std;
int main()
{
char a; int b; char c[100];
//scanf 和 cin 对比
scanf("%c%d%s",&a,&b,c);
printf("a = %c b = %d c = %s\n",a,b,c);
cin>>a>>b>>c;
cout<<"a = "<<a<<" b = "<<b <<" c = "<<c<<endl;
//char 与其它类型混输
scanf("%d%c",&b,&a); //第二个输入回车,会把回车打印输出
printf("a = %c b = %d\n",a,b);
cin>>b>>a; //直接吸收回车,不打印回车
cout<<"b = "<<b <<" a = "<<a<<endl;
//更安全的输入字符串
fgets(c,10,stdin);
printf("c = %s\n",c);
cin.getline(c,10); //遇回车或 n-1 个字符
cout<<"c = "<<c<<endl;
string s;
cin>>s;
cout<<"s = "<<s<<endl;
return 0;
}
cout 格式输出
1.对进制的输出
C++中iomaip库的setbase()方法用于基于指定为该方法参数的参数来设置ios库基域标志。
用法:
setbase (int base)
参数:此方法接受base作为参数,它是要设置底数所对应的整数参数。 10代表dec,16代表十六进制,8代表oct,其他任何值代表0(复位)。
返回值:此方法不返回任何内容。它仅充当流操纵器。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i = 123;
cout << i << endl; //123
cout << "dec:" << dec << i << endl; //123
cout << "hex:" << hex << i << endl; //7b
cout << "oct:" << oct << i << endl; //173
//需要引入头文件<iomanip>
cout << setbase(5) << i << endl; //123
return 0;
}
域宽/对齐/填充
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setw(10) << 1234 << endl; // 1234
cout << setw(10) << setfill('0') << 1234 << endl; //0000001234
cout << setw(10) << setfill('0') << setiosflags(ios::left) << 1234 << endl; //1234000000
cout << setw(10) << setfill('-') << setiosflags(ios::right) << 1234 << endl;//------1234
return 0;
}
设置浮点数精度
#include <iostream>
#include <iomanip>
#include <iosfwd>
using namespace std;
int main()
{
float f = 1.234;
cout << "default :" << f << endl; //default :1.234
cout << "custom :" << setprecision(2) << setiosflags(ios::fixed) << f; //custom :1.23
return 0;
}