1.C++特点
C语言的超集,兼容C
面对对象思想,
C语言当中的想法是:设计一个程序让灯亮 --- 属于是面向过程
而C++则是面向对象:定义一个灯类,并进行相关操作。
2.C++应用场景
游戏,服务器,图像处理,科学运算,操作系统,嵌入式开发等
C++和C语言区别
C++ C
面向对象 面向过程
开发效率高 开发效率没有上层语言高
运行效率比java高,比C语言低 运行效率高
C与C++的最大区别在于它们用于解决问题的思路方法不一样
C++文件以.cpp后缀
3.C++里面新增特性
新增关键字:class friend virtual inline private public protected const this string
新增运算符:new delete operator
作用域限定符 ::
4.第一个C++程序
#include<iostream>
int main()
{
std::cout << "hello world" << std::endl;
std::cout << "hello world" << std::endl;
return 0;
}
iostream --- 输出输入流 <istream> <ostream>
main函数 -- 程序主入口
std:标准名称空间 为了避免变量或者函数命名冲突,对于标准库里面的东西设置了标准命名空间 std
:: ---- 限定符
cout --- 输出
cin --- 输入
<< ------ 向屏幕输出内容
>> ------ 向内存写入内容
endl ---- 换行
小试牛刀:
定义一个整型变量,对变量通过输入的形式赋值,通过输出的形式显示输入的值。
#include<iostream>
using namespace std;
int main()
{
int n;
std::cout << "请输入一个数据:";
std::cin >> n;
std::cout << "输入的数据是:" << n << std::endl;
return 0;
}
输入年月日输出年月日
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout << "年:";
std::cin >> a;
std::cout << "月:";
std::cin >> b;
std::cout << "日:";
std::cin >> c;
// std::cout << "输入年月日:";
// std::cin >> a >> b >> c;
std::cout << "输入的是:" << a << "年" << b << "月" << c << "日" << std::endl;
return 0;
}
5.命名空间
namespace
namespace 空间名称
{
}
例子:
namespace A
{
int a;
int b = 40;
void show()
{
std::cout << "您好,C++" << std::endl;
}
namespace B
{
int b = 60;
}
}
int main()
{
std::cout << A::b << std::endl; // 40
A::show(); // "您好,C++"
std::cout << A::B::b << std::endl; // 60
return 0;
}
命名空间展开:
局部展开
using 命名空间::名称;
例子:
#include<iostream>
namespace A
{
int a;
int b = 40;
void show()
{
std::cout << "您好,C++" << std::endl;
}
namespace B
{
int b = 60;
}
}
using A::b;
int main()
{
std::cout << b << std::endl;
std::cout << A::B::b << std::endl;
return 0;
}
全部展开:
using namespace 命名空间名;
例子:
#include<iostream>
namespace A
{
int a;
int b = 40;
void show()
{
std::cout << "您好,C++" << std::endl;
}
namespace B
{
int b = 60;
}
}
using namespace A;
int main()
{
std::cout << b << std::endl;
std::cout << B::b << std::endl;
return 0;
}
开放标准命名空间
using namespace std;
注意:命名空间只能放在全局
命名空间里面可以放
变量(可以带有初始化);常量;数(可以是定义或者声明);结构体;类;模板;命名空间
6.C++新特性
1、输入输出流 2、引用 3、函数默认参数 4、内联函数 5、函数重载 6、强制类型转换 7、string类型 8、new 和 delete
1.输入输出流
参考上面
2.引用
引用就是给变量起别名,常量指针
引用需要初始化,引用在只用自带解地址的效果。
使用格式:
数据类型 &引用名 = 变量名;
例子:
int a = 10;
int &p = a; // p就当做a用
cout << p << endl;
p = 40; // a = 40;
cout << a << endl;
使用场景:
一般作为函数的形参使用
例子:
交换两个变量值的函数。---需要操作外部空间 地址 --- 形参是指针
#include<iostream>
using namespace std;
void swap(int &a,int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int x = 24;
int y = 8;
swap(x,y);
cout << x << " " << y <<endl;
return 0;
}
注意事项:
1、引用必须初始化
2、先有引用对象的空间才能定义引用
3、能有引用就用引用,如果是动态内存申请只能用指针。
拓展:常引用:const 数据类型 &引用名 = 名称;
int a = 30;
const int &q = a;
cout << q << endl;
// q = 60; 报错 q是read-only
cout << q <<endl;
// 在函数内部如何只对引用的空间进行读操作就可以设计形参为常引用。
int add(const int &a,const int &b)
{
return a + b;
}
3.函数形参可以有默认值
#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;
}
注意事项:
1、如果有多个参数,某个参数设置了默认值,它后面所有的参数都要设置默认值。
例如:
int test(int a,int b=2,int c=3)
{
}
2、如果函数声明和函数定义同时存在,尽量将函数的默认值设置在函数的声明里面,只能有一个地方有默认值,不然会报错。
#include<iostream>
using namespace std;
int add(int a=1,int b=2);
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;
}
int add(int a,int b)
{
return a + b;
}
4.内联函数
inline修饰的函数,提高函数的执行效率,让函数减少跳转
inline int add(int a,int b)
{
return a+b;
}
int main()
{
int res = add(2,4); // 3 + 5
cout << res << endl;
return 0;
}
内联函数:提高函数的执行效率,需要更多的内存空间
注意:内联函数里面不能出现循环 或者 递归函数,如果里面出现了则编译器会被当做普通的函数来看。 一般内联函数只能是代码很晒很简单的函数
5.函数重载
对于同一个功能,可以使用同一个函数名,不同的形参(形参的类型,个数,顺序)。
代码执行的之后,判断传入的是什么数据,找到对应的函数进行执行。
那么底层到底怎么区分?
编译器检测到函数重载会使用形参和函数名的组合重新定义个名字。
int add(int a,int b)
{
return a+b;
}
// add 转换成 addii
float add(float a,float b)
{
return a + b;
}
// add 转换成 addff
注意:
1、函数的返回值类型不能作为函数重载的判断。
2、如果函数有默认值,小心冲突。
6.强制类型转换
C语言 int a = (int)4.5;
C++ int a = int(4.5);
7.string字符串
C语言里面用数组
char str[30] = "hello";
char *p = "hello";
字符串 拼接 strcat 比较 strcmp...很麻烦
C++:字符串类型
string a = "hello";
string b = " world";
string c = a + b;
cout << c << endl;
小试牛刀:
定义用户名和密码
输入用户名和密码
如果用户名是admin,密码是123456输出登录成功,否则登录失败
#include<iostream>
using namespace std;
int main()
{
string name = "admin";
string pasw = "123456";
string username;
string userpasw;
cout << "请输入用户名:";
cin >> username;
cout << "请输入密码:";
cin >> userpasw;
if(username == name && userpasw == pasw)
{
cout << "登录成功" << endl;
}
else
{
cout << "登录失败" << endl;
}
return 0;
}
8.new 和delete
在堆区开空间,释放空间,人为申请,人为释放
new --- C语言:malloc --- 开空间
delete --- C语言:free --- 释放空间
7.easyX库使用
是windows平台上特有的一个图形库。
注意:建项目 dev-cpp项目->项目属性 ->参数 ->链接框 ->-leasyx
1.initgraph初始化画布
初始化绘图窗口
initgraph(640,480,EX_NOMINIMIZE);
2.绘制简单的图形
line(160,0,160,480);//线
circle(100,100,100);//无填充圆
fillcircle(200,200,50);//填充圆
3.绘制色彩
线的颜色:
COLORREF color 0xbbggrr(bb=蓝,gg=绿,rr=红)
RGB(红色,蓝色,绿色) 每一部分都是0~255
填充颜色示例:
setlinecolor(RGB(0,255,0));//填充线的颜色
setfillcolor(RGB(255,0,0));//填充填充图形的颜色
4.绘制多个图形
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);
5.动画
将图形进行位置上的逐帧移动,通过速度的调整形成动画的效果。
circle(40,40,40);
Sleep(50);
cleardevice();
circle(42,40,40);
Sleep(50);
cleardevice();
circle(44,40,40);
Sleep(50);
cleardevice();
circle(46,40,40);
Sleep(50);
cleardevice();
circle(48,40,40);
Sleep(50);
cleardevice();
circle(50,40,40);
Sleep(50);
cleardevice();
circle(52,40,40);
可通过循环进行简化,代码如下:
#include<iostream>
#include<graphics.h>
/*runthisprogramusingtheconsolepauseroraddyourowngetch,system("pause")orinputloop*/
int main(int argc,char** argv)
{
initgraph(640,480);
int i,j;
for(i = 40,j = 40;i<=640,j<=480;i=i+2,j=j+2){
circle(i,j,40);
Sleep(50);
cleardevice();
}
return 0;
}