1.c++运用场景: 游戏、服务器、图像处理、科学运算、操作系统、嵌入式开发等。
2.iostream--- 输出输入流 <istream> <ostream>
main函数 -- 程序主入口
std:标准名称空间为了避免变量或者函数命名冲突,对于标准库里面的东西设置了标准命名空间 std
:: ---- 限定符
cout --- 输出
cin --- 输入
<< ------ 向屏幕输出内容
>> ------ 向内存写入内容
endl ---- 换行
#include<iostream>
int main()
{
std::cout<<"liuherui";
return 0;
}
#include<iostream>
int main()
{
int a;
std::cin>>a;
std::cout<<"输出的数据是:"<<a;
return 0;
}
#include<iostream>
int main()
{
int year;
int month;
int date;
std::cin>>year>>month>>date;
std::cout<<year<<" "<<month<<" "<<date ;
return 0;
}
3.引用就是给变量起别名,常量指针
引用需要初始化,引用在只用自带解地址的效果
使用格式: 数据类型 &引用名 = 变量名;
#include<iostream>
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
int main()
{
int x;
int y;
std::cin>>x>>y;
swap(x,y);
std::cout<<x<<" "<<y;
return 0;
}
注意事项:
1、引用必须初始化
2、先有引用对象的空间才能定义引用
3、能用引用就用引用,如果是动态内存申请只能用指针。
4.函数形参可以有默认值
#include<iostream>
using namespace std;
int add(int a=1,int b=2)
{
return a + b;
}
int main()
{
int res = add();
cout << res << endl; // 默认值 1+2
int res1 = add(5);
cout << res1 << endl; // a的值是5,b的值默认
int res2 = add(5,6); // a的值是5,b的值是6
cout << res2 << endl;
return 0;
}
5.内联函数
inline修饰的函数,提高函数的执行效率,让函数减少跳转
注意:
内联函数里面不能出现循环 或者 递归函数,如果里面出现了则编译器会当做普通的函数的函数来看。
一般内联函数只能是代码很少很简单的函数
6.函数重载
#include<iostream>
using namespace std;
int add(int a,int b)
{
cout<<"整形:";
return a+b;
}
float add(float a,float b)
{
cout<<"实形:";
return a+b;
}
double add(double a,double b)
{
cout<<"实形:";
return a+b;
}
int main()
{
int res=add(3,77);
cout<<res<<endl;
float res1=add(3.5,6.6);
cout<<res1<<endl;
double res2=add(2.5,.7);
cout<<res2<<endl;
return 0;
}
7.string字符串
#include<iostream>
using namespace std;
int main()
{
string a="admin";
string b="123456";
string c;
string d;
cin>>c>>d;
if(c==a&&d==b)
{
cout<<"登陆成功";
}
else
{
cout<<"登陆失败";
}
return 0;
}
8.new和delete
在堆区开空间,释放空间,人为申请,人为释放
new --- C语言:malloc --- 开空间
delete --- C语言:free --- 释放空间
9. easyX库使用
是windows平台上特有的一个图形库。
注意:建项目 dev-cpp项目->项目属性 ->参数 ->链接框 ->-leasyx
#include <iostream>
#include <graphics.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv)
{
initgraph(640,480);
circle(320,240,100);
while(1);
return 0;
}
#include <iostream>
#include <graphics.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv)
{
initgraph(640,480);
setlinecolor(RGB(0,250,0));
int i;
for(i=0;i<=440;i=i+40)
{
circle(40,40+i,40);
Sleep(50);
}
while (1);
return 0;
}
绘制多个图形
setlinecolor(RGB(0,255,0));
// 横线
int winWidth = 640;
int winHeight = 480;
int cols = 4;
int rows = 4;
int i;
for(i=0;i<rows;i++)
{
// 纵坐标
int y = winHeight / rows * i;
line(0,y,winWidth,y);
}
//line(0,160,640,160);
//line(0,320,640,320);
// 竖线
line(160,0,160,480);
line(320,0,320,480);
line(480,0,480,480);
绘制色彩
-------------------线的颜色-----------------------------
COLORREF color) 0xbbggrr (bb=蓝,gg=绿,rr=红)
0xff0000 ---- 蓝色
0x00ff00 ---- 绿色
0x0000ff ---- 红色
RGB(红色,绿色,蓝色) 每一个部分都是0~255
RGB(255,0,0)
setlinecolor(0x0000ff);
setlinecolor(RGB(0,0,0));
-------------------填充的颜色-----------------------------
setlinecolor(RGB(0,255,0));
setfillcolor(RGB(255,0,0));
fillcircle(200,200,50);