第一个C++程序
#include<iostream>
using namespace std;
int main()
{
cout << "hello word" << endl;
system("pause");//请按任意键继续
return 0;
}
变量
给一段制定的内存空间起名,方便操作这段内存
#include<iostream>
using namespace std;
int main()
{
int a = 10;
cout <<a << endl;
system("pause");//请按任意键继续
return 0;
}
常量
#include<iostream>
using namespace std;
#define day 7//常量定义方式1
int main()
{
const int mouth = 30;//常量定义方式2
cout << "一周一共有:" << day << "天" << endl;
cout << "一个月有:" << mouth << "天" << endl;
system("pause");//请按任意键继续
return 0;
}
sizeof关键字
主要统计数据类型的内存大小
#include<iostream>
using namespace std;
int main()
{
short a = 1;
cout << sizeof(short) << endl;
int b = 2;
cout << sizeof(int) << endl;
system("pause");
return 0;
}
浮点型(小数)
#include<iostream>
using namespace std;
int main()
{
float f1 = 3.14f;
double f2 = 3.14;
cout << f1 << " " << f2 << endl;
//科学计数法
double f3 = 2e2;
double f4 = 2e-2;
cout << f3 << " " << f4 << endl;
system("pause");
return 0;
}
string字符串
#include<iostream>
#include<string>
using namespace std;
int main()
{
std::string a = "hello world";
cout << a << endl;
system("pause");
return 0;
}
字符型
#include<iostream>
using namespace std;
int main()
{
char ch1 = 'a';//一定是单引号,且里面只能有1个字母
cout << ch1 << endl;
cout << sizeof(ch1) << endl;
cout << int(ch1) << endl;//ASCII码的位置,a是97
system("pause");
return 0;
}
转义字符
#include<iostream>
using namespace std;
int main()
{
cout << "hello world\n";
cout << "aaa\thelloword" << endl;
cout << "\\" << endl;
system("pause");
return 0;
}
字符串string
#include<iostream>
#include<string>
using namespace std;
int main()
{
char ch1[] = "hello world";//C语言风格
string ch2 = "hello world2";//C++风格
cout << ch1 << ch2 << endl;
system("pause");
return 0;
}
布尔类型bool
只要非0都是真
#include<iostream>
using namespace std;
int main()
{
bool ch1 = true;
cout << ch1 << endl;//输出为1
system("pause");
return 0;
}
数据输入cin
语法:cin>>
#include<iostream>
using namespace std;
int main()
{
int a = 1;
cout << "请输入:" << endl;
cin >> a;
cout << a << endl;
system("pause");
return 0;
}
运算符(加减乘除)(取模)
加减乘除:±*/
取模(取余数):%
note:小数不能取模
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int b = 3;
cout << a % b << endl;//结果是1;10/3取余
system("pause");
return 0;
}
前置运算和后置运算i++、++i
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int b = 10;
//前置运算,先加1,在运算
int c = ++a * 10;
cout << a << endl;//运算结果11
cout << c << endl;//运算结果110
//后置运算,先计算,在加1
int d = b++ * 10;
cout << b << endl;//运算结果11
cout << d << endl;//运算结果100
system("pause");
return 0;
}
赋值运算符
#include<iostream>
using namespace std;
int main()
{
int a = 10;
a += 2;
cout << a << endl;//结果是12
system("pause");
return 0;
}
if语句
#include<iostream>
using namespace std;
int main()
{
double source;
cout<<"请输入分数"<<endl;
cin>>source;
if(source>=80)
{
cout<<"你太秀了"<<endl;
}
else if(source>=60&&source<80)
{
cout<<"你及格了"<<endl;
}
else
{
cout<<"你不及格"<<endl;
}
//system("pause");
}
练习案例(if语句)
#include<iostream>
using namespace std;
int main()
{
cout<<"请输入三只小猪的体重"<<endl;
int a,b,c;
cout<<"请输入小猪a的体重"<<endl;
cin>>a;
cout<<"请输入小猪b的体重"<<endl;
cin>>b;
cout<<"请输入小猪c的体重"<<endl;
cin>>c;
if (a>b)
{
if (a>c)
{
cout<<"a最重"<<endl;
/* code */
}
else
{
cout<<"c最重"<<endl;
}
/* code */
}
else
{
if (b>c)
{
cout<<"b最重"<<endl;
/* code */
}
else
{
cout<<"c最重"<<endl;
}
/* code */
}
//system("pause");
}
三目运算
#include<iostream>
using namespace std;
int main()
{
int a=1,b=2,c=3;
c=(a>b?a:b);//三目运算
cout<<"a"<<a<<"b"<<b<<"c"<<c<<endl;//输出结果a1b2c2
//system("pause");
}
switch语句
#include<iostream>
using namespace std;
int main()
{
cout<<"请输入分数"<<endl;
int source;
cin>>source;
cout<<"您输入的分数为"<<source<<endl;
switch (source)
{
case 10/* constant-expression */:
/* code */
cout<<"你的分数是优秀"<<endl;
break;
case 9:
cout<<"你的分数是良好"<<endl;
default:
cout<<"你的分数是合格"<<endl;
break;
}
}
类class
#include<iostream>
using namespace std;
const double pi=3.14;
class circle
{
public://权限
int r;//属性
//行为 获取圆的周长
double zhouchang()
{
double s=2*pi*r;
return s;
}
};
int main()
{
circle yuan1;
yuan1.r=1;
cout<<yuan1.zhouchang()<<endl;//输出结果6.28
}