C++基础之C与C++的区别一
C与C++的区别一
具体的语言区别
- C语言有32个关键字,C++有63个关键字
- C语言后缀名是.c,C++后缀名的.cpp
- C语言函数没有返回值,默认返回int类型;C++函数没有返回值必须指定为void
- C语言函数没有参数列表,默认可以接收多个;C++因为严格的参数类型检测,没有参数列表的函数,默认为 void,不接收任何参数
- C语言不支持缺省参数
- C语言没有函数重载,C++支持函数重载
其他不同
-
C语言是面向过程的语言。C++是面向对象的语言
-
相对于C语言来说,需要一个前缀
-
:: 作用域分辨符
-
C++支持许多附加特性
-
C++是C的超集
-
C++使用枚举比C严格
-
在C中,全局的const具有外部链接,但是在C++中,具有内部链接。也就是说,下面C++的声明
头文件的改变
- 标准输入输出头文件: #include
- 包含C语言的文件: #include 也可以用#include <xxx.h>
- 自己写的头文件还是用C语言的方式包含 #include ”myhead.h“
基本输入输出的改变
- 不再需要格式控制字符
- 依然支持转义字符
- 换行: endl替换\n
- 输出:cout 加上<<
- 输入: cin 加上>>
命名空间
-
语法
//空间名 namespace 标识符 { //变量 //函数 //结构体 //类 } -
最基础的访问方式
-
空间名::空间中的成员
AA::age=1001; strcpy(MM::name,"灰姑凉");
-
-
省略前缀的访问方式
-
使用using 语法注意点是: 防止空间变量或者函数不要和空间外的变量或者函数名字相同
using namespace 空间名; //省略当前这个空间名 -
命名空间嵌套
namespace A { int num; namespace B { int age; } } //访问方式:剥洋葱 A::num=1001; A::B::age=18; //省略前缀 using namespace A; using namespace A::B; num=1991; age=1992
-
最简单的C++程序
#include <iostream>
using namespace std;
int main()
{
return 0;
}
C++简单输入输出程序
#include <iostream>
void testInputPrint()
{
char name[20];
int age;
int num;
//单个数据输入
std::cout << "请输入一个整数:";
std::cin >> age;
std::cout << "请输入name,age,num:";
std::cin >> name >> age >> num;
std::cout << name << "\t" << age << "\t" << num << std::endl;
}
int main()
{
testInputPrint();
return 0;
}
数据类型的改变
-
空指针由 NULL 改为nullptr
-
引入了bool类型
- 正常赋值为: true ,false
- 非零值非空值表示成立,只有0和空表示不成立
- bool占用字节数是1
- 打印结果只有两个值: true:1 false: 0
- 一般条件表达式或者逻辑表达式,或者充当开关变量,标记变量
-
引用类型
-
C++极为重要的类型
-
基本用法
- 起别名
类型& 标识符1=标识符2 //标识符2有一个别名字的叫做标识符1, 他们是一个东西
-
-
常引用类型
-
给常量起别名
const 类型& 标识符=常量;
-
-
右值引用
-
给右值起别名
类型&& 标识符=右值
-
-
引用的方法
- 当做函数参数
- 防止拷贝本的产生
- 当做函数返回值
- 增加左值使用用法(等效返回值一个变量)
- 当做函数参数
-
自动推断类型
- auto 类型
- 不能单独定义变量
- 一般结合赋值使用
- auto 类型
void testAuto()
{
auto intNum = 1;
cout << intNum << endl; //int
int (*pMax)(int, int) = Max;
auto ppMax = Max;
cout << ppMax(1, 2) << endl;
auto pp = printMax;
pp(ppMax, 1, 2);
}
-
作用域分辨符
-
: :
-
就近原则
int num = 100000; cout << num << endl; cout << ::num << endl; //代表是全局区变量
-
-
#include <iostream>
using namespace std;
bool empty(int size)
{
return size == 0;
}
//Reference types 引用类型
int returnValue(int num)
{
return num;
}
int num = 0;
int& returnValueReference()
{
return num; //warning C4172: 返回局部变量或临时变量的地址: num
}
void SwapC(int a, int b) //int a=实参1 int b=实参2
{
int temp = a;
a = b;
b = temp;
}
void SwapCpp(int& a, int& b) //int &a =实参1 ,int &b=实参2
{
//以后想要在子函数中修改什么,传入相应的引用
int temp = a;
a = b;
b = temp;
}
void modify(int* &p)
{
p = #
}
//const在C++上面更为严格,类型需要严重性的一致
//如果你想要既可以传入常量也可以传入变量,需要const修饰
void printConstValue(const char* str)
{
cout << str << endl;
}
//只需要传入常量
void printOnlyConstValue(int&& num)
{
cout << num << endl;
}
void testReferenceTypes()
{
//3.1 基本用法
int a = 1;
int& b = a; //a就是b b就是a
b = 100;
cout << a << endl;
cout << b << endl;
//int& constNum = 12; //常量的引用,这样是错误的
const int& constNum = 12;
//右值引用也可以表示常量
int&& rightValue = 1001;
returnValueReference() = 1111;
cout << num << endl;
//充当函数
int aa = 1;
int bb = 2;
SwapC(aa, bb);
cout << aa << "\t" << bb << endl;
SwapCpp(aa, bb); //注意不需要取地址传参
cout << aa << "\t" << bb << endl;
int* p = nullptr;
modify(p);
cout << *p << endl;
//常引用传参
printConstValue("ILoveyou");
//右值引用当做函数参数,只能传入右值
printOnlyConstValue(1212);
//move可以把左值变成右值
printOnlyConstValue(move(num));
}
//auto类型
int Max(int a, int b)
{
return a > b ? a : b;
}
void printMax(int(*p)(int, int), int a, int b)
{
cout << p(a, b) << endl;
}
void testAuto()
{
//5.1 不能单独定义变量
//auto a; 错误
//5.2 auto一般结合赋值使用
auto intNum = 1;
cout << intNum << endl; //int
int (*pMax)(int, int) = Max;
auto ppMax = Max;
cout << ppMax(1, 2) << endl;
auto pp = printMax;
pp(ppMax, 1, 2);
}
int main()
{
//1.基本数据类型的改变
int* p = nullptr;
//2.bool类型引入
bool bNum = true; //正常赋值,用true和false
//非正常的赋值
bNum = -111;
cout << bNum << endl; //1
//3.引用类型
testReferenceTypes();
//4. :: 作用分辨符
//就近原则
int num = 100000;
cout << num << endl;
cout << ::num << endl; //代表是全局区变量
//5.自动推断类型
testAuto();
return 0;
}
C++函数的改变
内联思想
-
函数以二进制形式存在,去提高效率
-
内联函数
- 用inline修饰的函数
-
短小精悍
-
在结构体中或者类型实现的函数,默认为内联
inline int Max(int a, int b) { return a > b ? a : b; }
-
重载思想
-
C++允许同名不同参的函数存在
-
参数个数不同
void print(int a, int b, int c) { cout << a + b + c << endl; } void print(int a, int b) { cout << a + b << endl; } -
参数类型不同
void print(char a, char b) { cout << a + b << endl; } -
参数顺序不同(建立在存在不同类型)
void print(int age, double num) { cout << age << "\t" << num << endl; } void print(double num, int age) { cout << age << "\t" << num << endl; } -
常属性的成员函数和类中的普通函数重载(这个后面讲)
-
const属性不构成重载
缺省思想
-
给函数形参初始化,达到不同形态的函数调用
-
缺省的顺序 必须是从右往左,连续缺省
-
缺省时候,要避免存在普通函数,防止存在二义性
void printValue(int a=1, int b=2, int c=3,int d=4) { cout << a + b + c + d << endl; }
-
Lambda表达式
-
函数的定义 并且返回一个函数指针,所以一般在C++中会结合auto使用
-
Lambda表达式组成部分
[捕获方式](函数参数)能否修改 是否存在异常->函数返回值类型{函数体;}; -
捕获方式
- 可以理解为使用函数外面的变量的方式
- []
- [=]
- [&]
- [&x,=]
- [this] 类中数据成员的捕获
- 可以理解为使用函数外面的变量的方式
-
函数参数
- 自己写的函数的函数参数
-
能否修改
- mutable 可以修改
-
是否存在异常
- throw() 不存在异常
-
函数返回值类型
- 自己写的函数的函数返回值
-
函数体
- 原来函数的函数体
-
注意
- 一般在用的时候,能否修改 是否存在异常->函数返回值类型 是可以省略
void testLambda() { //int Max(int a, int b) int(*pMax)(int, int) = [](int a, int b)mutable throw()->int {return a > b ? a : b; }; //一般用的时候,怎么简单怎么来,结合auto+省略的Lambda表达式去使用 auto p= [](int a, int b){return a > b ? a : b; }; cout << p(1, 2) << endl; //进阶一下: 函数的定义和调用写在一起 cout << [](int a, int b) {return a > b ? a : b; }(1, 3) << endl; printMax([](int a, int b) {return a + b; }, 3, 4); printMax([](int a, int b) {return a - b; }, 3, 4); //捕获方式 int num = 1; //[]() {cout << num << endl; };错误 没有捕获方式,表示没用权力 auto p1=[=]() {cout << num << endl; }; auto p2 = [&]() {cout << num << endl; }; p1(); //打印1 p2(); num = 1001; p1(); //不会因为值的改变而改变调用 ,打印1 p2(); }
本文来自博客园,作者:{oy},转载请注明原文链接:https://www.cnblogs.com/Oysen/p/17005601.html

浙公网安备 33010602011771号