一.类型增强
1.把一个const类型的指针赋给非const类型的指针,C语言中可以通过,但是C++中则编不过去。
C语言中表达式通常不能作为左值的,即不可被赋值,c++中某些表达式是可以赋值的。
#include <stdio.h>
int main(int argc, char *argv[])
{
const int a = 100;
//a = 90; //erro 1
int *p = &a; //C语言中const类型的指针可以赋给非const类型的指针
char *q = malloc(100); //malloc可以不指定(char *)malloc
printf("*p = %d\n",*p);
printf("Hello World!\n");
int c, d = 5;
//(c = d) = 100;//C语言中是不可以的,表达式是不可以被赋值的
printf("c = %d, d = %d\n",c, d);
return 0;
}
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
const int a = 100;
//int *p = &a; //c++中不行 //const int * ---> int *
char *q = (char *)malloc(100);//right
//char *q = malloc(100);
cout << "Hello World!" << endl;
return 0;
}
2.布尔类型(bool)
C语言的逻辑真假用0和非0来表示,而C++中有了具体的类型。
#include <stdio.h>
//c++中可以直接使用
enum BOOL
{
false,
true,
};
typedef enum
{
hi_false,
hi_true,
}BOOLL;
int main(void)
{
//c++中可以直接省掉前面的enum
enum BOOL m = false;
//BOOL m = false; //error
if(!m)
{
printf("this is true!\n");
}
else
{
printf("this is false!\n");
}
BOOLL A = true;
printf("BOOLL A = %d\n", A);
BOOLL B = hi_false;
printf("BOOLL B = %d\n", B);
BOOLL C = 3;
printf("BOOLL C = %d\n", C);
printf("Hello World!\n");
return 0;
}
#include <iostream>
using namespace std;
enum number
{
zero,
two,
three,
four,
five
};
//枚举类型就是占据4字节的空间,而bool是占据1字节空间
int main(int argc, char *argv[])
{
number A = zero;
bool flag = true;
printf("flag = %d\n", flag);
printf("number A = %d\n", A);
cout << "sizeof(enum number)" << sizeof(enum number) <<endl;
cout << "sizeof(bool)" << sizeof(bool) <<endl;
return 0;
}
#include <iostream>
using namespace std;
enum BOOL
{
FALSE,
TRUE,
};
enum
{
Spr,
Sum,
Autu,
Win
};
/*-------------知识点一------------------*/
//以上的枚举相当于是宏定义
/*
#define Spr 0
#define Sum 1
#define Autu 2
#define Win 3
*/
const int BUFSIZE = 100;
int main(int argc, char *argv[])
{
int Array[BUFSIZE];
const int a = 100;
cout << "a = " << a <<endl;
char *q = (char *)malloc(100);
bool b = false;
if(!b)
{
cout << "b value is false\n" << endl;
}
else
{
cout << "b value is true\n" << endl;
}
BOOL c = FALSE;
if(!c)
{
cout << "c value is false\n" << endl;
}
else
{
cout << "c value is true\n" << endl;
}
bool bb;
cout << "sizeof(bb) = " << sizeof(bb) <<endl;
cout << "sizeof(bool) = " << sizeof(bool) << endl;
cout << "sizeof(BOOL) = " << sizeof(BOOL) << endl;
cout << "sizeof(c) = " << sizeof(c) << endl;
cout << "enum " << Spr <<endl;
cout << "enum " << Sum <<endl;
cout << "enum " << Autu <<endl;
/*---------------------知识点二------------------------*/
//c语言中表达式是不能作为左值的
int d, e = 5;
//d = e = 100; right
(d = e) = 100; //right C语言中是不可以的,表达式是不可以被赋值的
//上述的含义是d=e,d =100,e的值依然为5
cout <<" d = " << d << " e = " << e << endl;//e = 5,d = 100
int f =1 , g = 2;
//三目运算符构建的条件表达式,C语言中也是不可以的
(f < g ? f:g) =200;
cout <<" f = " << f << " g = " << g << endl;//f = 200, g = 2
/*---------------知识点三----------------------------------*/
//cin cout 类对象, scanf sprintf 函数,
char name[30];
//下面的输入方式都是有风险的,name的空间是有限的
//scanf("%s",name);
//cin >> name;
//gets(name);
//fgets(name,30,stdin);//读29个字符,最后一个是\0
string namestr;
cout << "namestr.max_size() = " << namestr.max_size() << endl;
cout << "Hello World! C++" << endl;
return 0;
}
3.输入和输出的区别
设置域宽及位数
按进制输出
设置填充符
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
int a = 123;
float b = 1234.567;
printf("%5c\n%5d\n%6.2f\n",'a',100,120.00);
printf("%8d*\n",a);
printf("%-8d--",a);
printf("%.2f\n",b);
//默认输出6位有效位数
cout << "default output b:"<<b<<endl;
//左对齐
cout << setiosflags(ios::left)<< setw(8) << a << "="<<endl;
//设置域宽
cout << setw(12) << setprecision(3) << setiosflags(ios::fixed) << b << "--" <<endl;
//进制输出
cout << hex << a << endl;
cout << dec << a << endl;
cout << oct << a << endl;
cout << setfill('*') << setiosflags(ios::left)<<setw(8)<<a<<endl;
cout << setbase(16) << a << endl;
cout << setfill('*') << setiosflags(ios::left)<<setw(8)<<a<<endl;
cout << setbase(10) << endl;
//时钟输出
int hour = 24,min = 0,second = 0;
cout << setfill('0') << setw(2) << hour << ":" << setw(2) << min << ":" << setw(2) << second << endl;
cerr << "output error message!" << endl;
clog << "cerr buffer output error message!" << endl;
return 0;
}