小白学c++
目标:做个小游戏,俄罗斯方块
c++特点 :面向对象
命名空间
/**************************
namespace 空间名称
{
}
**************************8*/
namespace A
{
int a;
int b = 40;
void show()
{
std::cout << "您好,C++" << std::endl;
}
namespace B
{
int b = 60;
}
}
int main()
{
return 0;
}
/********************************
局部展开
using 命名空间::名称;
*******************************/
using A::b;
int main()
{
std::cout << b << std::endl;
std::cout << A::B::b << std::endl;
return 0;
}
/********************************
using namespace 命名空间名;
*******************************/
using namepasce A;
int mian ()
{
std::cout << b <<std::endl;
std::cout << B::b <<std::endl;
return 0;
}
d. 开放标准命名空间
using namespace std;
命名空间只能放在全局
3.引用
3.1引用格式
int main()
{
int a = 10;
int &p = a;
cout<< p <<endl;//p =10
p = 40;//a = 40;
cout<< a <<endl;
}
3.2用法
给变量起别名 ,常量指针
应用需要初始化,引用自带解地址效果
void swap(int &a,int &b)
{
int temp ;
temp = a;
a = b;
b = temp;
}
int main()
{
int x = 10;
int y = 20;
swap (x,y);
cout << x << y << endl;
return 0;
}
1、引用必须初始化
2、先有引用对象的空间才能定义引用
3、能用引用就用引用,如果是动态内存申请只能用指针。
3.3拓展
常引用: const 数据类型 &引用名 = 名称;
int a = 30;
const int &q = a;
cout << q << endl;
// q = 60; 报错 q是read-only
a = 50;
cout << q <<endl;//q = 50 虽然q无法改变,但是可以改变a
// 在函数内部如何只对引用的空间进行读操作就可以设计形参为常引用。
int add(const int &a,const int &b)
{
return a + b;
}
4.函数形参可以有默认值
#include<iostream>
using namespace std;
int add(int a=1,int b=2) //默认值a=1, b=2;
{
return a + b;
}
int main()
{
int res = add();
cout << res << endl; //默认值相加
int res1 = add(5);
cout << res1 << endl; // 只有一个数时,a的值是5,b的值默认
int res2 = add(5,6); // a的值是5,b的值是6
cout << res2 << endl;
//不会有a是默认值,b不是的情况
return 0;
}
注意事项:
函数声明和函数定义同时存在,将函数默认值放在函数声明里,不然容易报错。
某个参数设置默认值,在其后的所有参数都要设置默认值。
5.内联函数
inline修饰的函数,提高函数的执行效率,让函数减少跳转,但需要更多的内存空间。
内联函数力不能出现循环或者递归函数,一般只能是代码很少的函数
6.函数重载
对于同一个功能,可以使用同一个函数名,不同的形参。
底层到底怎么区分?
编译器检测到函数重载会使用形参和函数名的组合重新定义个名字。
int add(int a,int b)
{
return a+b;
}
// add 转换成 addii
float add(float a,float b)
{
return a + b;
}
// add 转换成 addff
如果函数有默认值,应避免冲突
int add(int a,int b=2)
{
return a + b;
}
int add(int a)
{
return a;
}
int main()
{
int res = add(3);//编译器不知奥编译哪个函数
cout << res << endl;
return 0;
}
7.强制类型转换
int = int (4.5);
与c语言反过来
8.string字符串
string a = "hello";
string b = " world";
string c = a+b;
cout<<c<<endl;
int a,b;
cout <<"请输入a,b的值"<< endl;
cin >> a;
cin >> b;
int res = add(a,b);
cout << "a+b = "<< res << endl;
double c,d;
cout <<"请输入c,d的值" << endl;
cin >> c;
cin >> d;
double res1 = add(c,d);
cout << "c+d = "<< res1 << endl;
string a,b;
cout << "请输入用户名"<<endl;
cin >> a;
string c = "admin";
string d = "123456";
if(a == c)
{
cout << "请输入密码" <<endl;
cin >> b;
if(b==d)
{
cout << "登陆成功" <<endl;
}
else
{
cout <<"请重新输入密码"<<endl;
}
}
else
{
cout <<"请重新输入用户名" <<endl;
}
9.new和delete
在堆区开空间,释放空间,人为申请,人为释放
new ---------- malloc ------ 开空间
delete ------- free --------- 释放空间
int *p;
p = new int;
*p = 30;
cout<< *p <<endl;
delete p;
p = NULL;
int *p = new int[2];
*q = 50;
*(q+1) = 60;
cout<<*q<<*(q+1)<<endl;
delete p;
p = NULL;
10.easyX
1.初始化绘图窗口
initgraph(640,480,EX_NOMINIMIZE);//用于去掉最小化按钮
2.绘制色彩
线色彩:
setlinecolor(0x0000ff);//0xff0000 ---- 蓝色
0x00ff00 ---- 绿色
0x0000ff ---- 红色
setlinecolor(RGB(0,0,0));//RGB(红色,绿色,蓝色)
RGB(255,0,0)
填充图案色彩
setlinecolor(RGB(0,255,0));
setfillcolor(RGB(255,0,0));
fillcircle(200,200,50);
#include <iostream>
#include<graphics.h>
#include <conio.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,EX_NOMINIMIZE);
int i;
setlinecolor(RGB(55,255,55));
setfillcolor(RGB(50,50,250));
for(i=0;i<280;i++)
{
cleardevice();
fillcircle(40+2*i,40,40);
Sleep(10);
}
int j;
for (j=0;j<=40;j++)
{
cleardevice();
fillcircle(600,40+2*j,40);
Sleep(10);
}
for(i=0;i<280;i++)
{
cleardevice();
fillcircle(600-2*i,120,40);
Sleep(10);
}
while(1);
return 0;
}