C++第一课(2013.9.26 )

时间:2023-03-08 18:46:58
 //C++三大特性:封装,继承,多态

 //C++新增的数据类型:bool型  一个字节   真 true  假 false

 //case 定义变量的问题
int nValue = ;
switch(nValue)
{
case :
{
printf("1\r\n");
break;
}
case :
{
//在case里定义变量要加括号
int n = ;
printf("2\r\n");
break;
}
case :
{
printf("3\r\n");
break;
}
} cout<<"Hello World"<<endl;
//endl = '\n' + flush 即endl的作用是插入换行符并刷新流 cout<<"Hello World";
//若没有加endl或者flush,则只在程序结束的时,才提交数据,并显示Hello World。
//"<<"的功能等价于printf函数的功能,可以理解为:"<<"重载了,printf函数的功能。 streambuf *lpBuff = cout.rdbuf(); //获取缓冲区 /*格式化输出:
C中:
%x 十六进制输出 %o 八进制输出
C++中:*/
cout<<hex<<<<endl; //十六进制输出,会影响到后面所有的输出
cout<<dec<<<<endl; //十进制输出,会影响到后面所有的输出
cout<<oct<<<<endl; //八进制输出,会影响到后面所有的输出 //设置输出格式
cout.setf(ios::hex); //设置为十六进制格式输出
//...................
cout.unsetf(ios::hex); //恢复为原来的输出格式 //设置输出的宽度
cout.width(); //设置宽度,有效一次
cout<<"HE"<<endl; //setw()设置宽度的函数 在头文件 iomani.h 中
cout<<hex<<setw()<<"HE"<<endl; //设置填充字符
cout.width();
char ch = cout.fill('#'); //设置填充字符,保留原来的填充字符
cout<<"HE"<<endl;
cout.fill(ch); //恢复为原来的填充字符 cout<<"0x"<<setfill('')<<hex<<setw()<<<<endl; //设置对齐方式
cout.setf(ios::left); //设置为左对齐
//....................
cout.unsetf(ios::left); //还原对齐方式 //格式化为科学记数法
cout.setf(ios::scientific); //设置为科学记数法格式输出
//.....................
cout.unsetf(ios::scientific); //还原输出格式 cout<<setiosflags(ios::scientific)<<313.567<<setiosflags(ios::scientific)<<endl; //设置浮点数输出的精度
cout.setf(ios::fixed);
cout.precision();
cout<<3.14f<<endl; cout<<setiosflags(ios::fixed)<<setprecision()<<3.14f<<endl; //防止输入溢出的方法:
char szBuff[] = {}; //1.使用getline函数
//getline()函数
cin.getline(szBuff, ,'\n'); //2.使用read函数
//read()函数从输入流中读取指定的数目的字符,并放在指定的地方
cin.read(szBuff, ); //清空缓冲区的方法
//获取缓冲区的大小
int n = cin.rdbuf()->in_avail();
//忽略缓冲区
cin.ignore(n, '\n');