type enhancement
1. The type is more strict
If it is not initialized, it cannot be compiled; if C is not initialized, it will be randomly assigned
#include <iostream>
#include <stdlib.h>
int main()
{
const int a = 100; //真正的const,无法修改
// int *p = &a; 报错
const int *p = &a;
char *p = (char)malloc(100); //malloc 得到的是void *
return 0;
}
C++ doesn't like pointers and casts
2. Added bool type (actually added enumeration)
In C language, true and false are represented by 0 and non-zero
string s;
bool b = true / false;
enum BOOL{ FALSE, TRUE};
BOOL a = FALSE;
3. Enumerate equivalence relations
#define Spr 0
#define Sum 1 宏
enum
{
Spr=1, Sum, ...
};
Try not to use macros in C++, use enumeration instead of enumeration
input and output
In C semantics, an expression cannot be assigned a value
#include <stdio.h>
int main()
{
int a, b = 5;
a = b = 100;
(a = b) = 100; //100是对a的赋值 C语言中报错
printf("a = %d b = %d", a, b);
return 0;
};
C++ expressions can be assigned
#include <iostream>
int main()
{
int a, b = 10;
a = b = 100;
printf("a = %d b= %d\n", a, b);
(a != b ? a : b) = 1000; //输出a=100, b=1000
return 0;
}
input and output
# include <iostream>
using namespace std;
//cin cout 类对象,scanf sprinf 相同的功能,函数
int main()
{
//char name[30];
//scanf("%s", name); //不安全 超过30个字符会崩溃
//gets(name); //不关心溢出问题,不安全
// 键盘中的输入流入到name中
//cin>>name; // >> 流输入运算符(重载 也代表右移) 超过30个字符会崩溃
string name; //安全 从语言层规定
cin>>name;
cout<<"name="<<name<<endl;
//"name=" name endl 均流入到cout中,等价于一个一个赋值
cout<<"name="<<name<<endl;
printf("name = %s\n", name);
return 0;
}
# include <iostream>
# include <iomanip>
using namespace std;
int main()
{
//C++ 不受空格控制
int a = 12345; int b = 1234.567;
cin>>a>>b; // cin>>a cin>>b
cout<<a<<b<<endl; //endl \n
cout<<setw(8)<<a<<endl; //引入头文件 iomanip
cout<<setiosflags(ios::left)<<setw(8)<<a<<endl; //左对齐
cout<<b<<endl; //输出两位小数点
return 0;
}
function overloading
# include <iostream>
//函数重载(静多态)
// 函数名相同,参数列表不同 类型 个数 顺序
int abs(int data)
{
return data>0?data:-data;
}
float abs(float data)
{
return data>0?data:-data;
}
int main()
{
float ret = abs(-5.5f); //ambiguous 二义性
cout<<ret<<endl;
int ret2 = abs(5);
cout<<ret2<<endl;
return 0;
}
Matching principle:
1. Strict match, call if found; 2. Seek a match through implicit conversion, call if found.
rolling technique
C++ is fully compatible with C (syntax, standard lib library will not participate in the competition) string.h stdlib.h stdio.h
The underlying implementation principle of C++ function overloading is that C++ uses name mangling (rolling) technology to rename function names and distinguish functions with the same name with different parameters.
C++ named function cannot be called by C language. C++ functions must be callable without exception. Using a function declared extern "C" will prohibit naming conflicts , so that C++ functions can be called by C language.
str.cpp
#include <iostream>
int mystrlen(char *p)
{
int len = 0;
while(*p)
{
len++;
p++;
}
return 0;
}
str.h header file
#ifndef STR_H
#define STR_H
int mystrlen(char *p);
// extern "C" int mystrlen(char *p);
#endif //STR_H
main.cpp
#include <iostream>
#include "str.h"
using namespace std;
int main()
{
int n = mystrlen("china");
cout<<n<<endl;
return 0;
}
Operator Overload Operator Overload
C++ considers all operators to be functions. Functions can be overloaded (not all operators can be overloaded).
#include <iostream>
using namespace std;
struct Complex //结构体 复数
{
float real;
float image;
};
Complex operator+(Complex a, Complex b)
{
Complex c;
c.real = a.real + b.real;
c.image = a.image + b.image;
return c;
}
int main()
{
int a = 1; int b = 2;
int c = a + b;
cout << "c: " << c << endl;
Complex aa = {1, 2}, bb = {2, 3};
Complex cc = aa + bb; // 报错--增加了operator+,可执行
Complex cc = operator+(aa, bb); //可执行
cout<<"c = "<<"("<<c.real<<","<<c.image<<")"<<endl;
return 0;
}
default parameters default parameters
#include <iostream>
#include <time.h>
using namespace std;
void weatherCast(string w = "pm2.5")
{
time_t t = time(0); //170,0,0,0
char tmp[64];
strftime(tmp, sizeof(tmp), "%Y/%m/%d %X %A ", localtime(&t) );
cout<<tmp<<"today is weather "<<w<<endl;
}
//从右向左默认,中间不能跳跃
//实参的个数+默认参数的个数 >= 形参的个数
int volume(int l, int w, int h=5)
{
return l*w*h;
}
int main()
{
weatherCast();
weatherCast();
weatherCast("sunshine");
cout<<volume(1, 2)<<endl;
cout<<volume(1, 2, 10)<<endl;
return 0;
}
include <iostream>
using namespace std;
//void print(int a)
//{
// printf("void print(int a)\n");
//}
void print(int a, int b = 10)
{
print("void print(int a, int b = 10)\n");
}
int main()
{
print(1, 20); // 1个参数或2个参数的形式,重载,默认参数都可以实现,但是不可同时存在
return 0;
}
Reference
- A reference is a declaration relationship, which must be initialized when it is declared. References do not open up space and do not allocate memory.
- Such a declared relationship, once declared, cannot be changed.
- References can be referenced again. The result of multiple references is that multiple references point to the same address.
- The reference must be consistent with the original type.
#include <iostream>
using namespace std;
int main()
{
int a = 500; //变量名实质是一段内存空间的别名
//*(int *)0xb0002345 = 500;
int& ra = a; // 声明 ra 是a的引用(别名)
printf("sizeof(a) = %d, sizeof(ra) = %d\n", sizeof(a), sizeof(ra));
printf("&a = %p &ra = %p", &a, &ra);
a = 100;
printf("a = %d ra = %d\n", a, ra);
ra = 2000;
printf("a = %d ra = %d\n", a, ra);
int b = 200;
ra = b; // 赋值
int &ra = b; //声明
int& rr = ra; //再次引用
printf("a = %d ra = %d rr = %d\n", a, ra, rr);
return 0;
}
#include <iostream>
using namespace std;
void swap(int *pa, int *pb)
{
*pa ^= *pb;
*pb ^= *pa;
*pa ^= *pb;
}
void swap(int &ra, int &rb)
{
ra ^= rb;
rb ^= ra;
ra ^= rb;
}
//平级内解决问题,然后不开辟多余的空间
//引用的目的是对指针的再次包装。指针是有引用的,但不应该有引用的指针。
void swap(char* &p, char* &q)
{
char *t = q;
q = p;
p = t;
}
int main()
{
int a = 5; int b = 3;
swap(a, b);
cout<<"a= "<<a<<" b= "<<b<<endl;
char *p = "china"; // 定义指针的引用 char* & rp = p
char *q = "america";
cout<<"p = "<<p<<" q = "<<q<<endl;
swap(p, q);
cout<<"p = "<<p<<" q = "<<q<<endl;
return 0;
}
To avoid the defects of C language pointers, references to references cannot be defined, but pointers to pointers (secondary pointers) can be defined
#include <iostream>
using namespace std;
int main()
{
int a;
int * p = &a; //定义指针
int **pp = &p; //定义指针的指针(二级指针)
int b;
int &rb = b; //定义引用
int * &rp = p; //定义指针的引用
//报错,不能定义引用的引用
int & & rrp = rp; ❌
//不能定义引用的指针,引用就是对指针的包装,不希望再打开
int& * p = &rb; ❌
int x, y, z;
int * p[] = {&x, &y, &z} //可以建立指针的数组
int & rp [] = {x, y, z}; ❌ //不可以定义引用数组 rp int& *
return 0;
}
array reference
int arr[]= {1, 2, 3, 4, 5};
int(&rarr)[5] = arr;
Often quoted
- A reference to a const object must be const, and it is illegal to bind a normal reference to a const object
- A const reference can be initialized with an object of the associated type (constant, non-identical variable, or expression)
const int a = 100;
const int &ra = a;
int a;
const double & ra = a + 5;
First define double tmp = a and then define const double rb = tmp
use const whatever possible Cause:
- Use const to avoid programming errors that inadvertently modify data
- Using const can handle const and non-const arguments, otherwise only non-const data can be accepted
- Using const references allows the function to correctly generate and use temporary variables (if the actual participation reference parameters do not match, a temporary variable will be generated)