第一个c++程序
1.新建项目,Win32控制台程序,空项目
2.源文件添加新建项,代码,c++文件.cpp
3.程序代码如下
#include<iostream>
using namespace std;
int main()//主函数,应用程序的入口,一个项目只能一个main函数
{
cout<<"hello haohao~"<<endl;//输出
system("pause");//卡住界面,方便观察结果
return 0;
}
4.注释
{
//单行注释,c++标准注释,--对代码功能描述,意义,完全对于程序员
/*多行注释*/c语言特有,但是c++也支持
#if0
#endif 预处理,利用语言的特性
}
5.主函数尽量写标准形式
void main(),在C语言和c++种均未定义,是一种错误形式
6.
#include<iostream>//stdio in(输入) out(输出) stream(流)
using namespace std;//使用命名空间,标准,c++标准写法
#include<iostream.h>//早期写法
int main()
{
return 0;
}
7.输入/输出
#include<iostream>
using namespace std;
int main()
{
//cout是个对象,不是关键字,也不是函数
cout<<"hello";//输出常量字符串
cout<<'a'<<12.13;//输出常量字符
// 1.可以连续输出
// 2.自动识别类型 智能
int a=13;
char b='b';
float f=123.12;
cout <<a<<' '<<b<<' '<<f;
system("pause");
return 0;
}
8.换行符
#include<iostream>
using namespace std;
int main()
{
cout<<"hello"<<endl;//endl换行符,换行,并且清空缓冲区
cout<<"hello\n";// \n换行符,仅仅是换行
system("pause");
return 0;
}
9.刷新缓冲区
endl,多了一个刷新缓冲区的操作,这个操作会使缓冲的字符立即显示到屏幕上
\n不能保证
endl比\n的效率要慢,因为多了刷新缓冲区操作,肉眼不可见
10.输入
cin;//是个对象,不是关键字,也不是函数
#include<iostream>
using namespace std;
int main()
{
char c;
int a;
double d;
cin>>c>>a>>d;
cout<<c<<' '<<a<<' '<<d<<endl;
system("pause");
return 0;
}
11.命名空间
区别同名的函数,变量
#include<iostream>
using namespace std;
namespace stu
{
void sort()
{
}
}
namespace stu1
{
void sort()
{
}
}
void sort()
{
}
int main()
{
using namespace stu;
stu:: sort();//打开第一个命名空间,::作用域运算符
//不打开也可以直接调用
system("pause");
return 0;
}