1. delete 和 new
2. 函数重载
3. 内联函数
4. 常量
5. 引用
常量引用
int x = 1;
const int& y = x;
x = 2;
cout << y << endl;
/*output
2
*/
函数引用
int& func2(int* pint)
{
return *pint;
}
int a = 10, b;
b = func2(&a) * 5;
func2(&a) = 70; //不能省略int&的&
cout << "a = " << a << ", b = " << b << endl;
/*output
a = 70, b = 50
*/
6. 对象指针
7. 拷贝构造函数
.
8. 类对象做参数类型的三种方式
9. 类对象数组初始化 数据初始化
数组初始化
class example1
{
public:
int val3;
};
class example
{
public:
int val1;
int val2;
example1 exam;
};
//如果example发生值初始化,则定义在example中的exam成员变量发生值初始化。
//如果example发生默认初始化,则定义在example中的exam成员变量发生默认初始化。
example ex; //ex 值初始化
void printClass()
{
example ex1; //ex1 默认初始化
example exArr[2] = {}; //exArr[1],exArr[2] 值初始化
cout << "ex.val1 = " << ex.val1 << endl; //ex.val1值初始化 为0
cout << "ex.val2 = " << ex.val2 << endl; //ex.val2值初始化 为0
cout << "ex.exam.val3 = " <<ex.exam.val3 << endl; //ex.exam值初始化 ex.exam.val3值初始化 为0
cout << "ex1.val1 = " << ex1.val1 << endl; //ex1.val1 默认初始化 未定义值
cout << "ex1.val2 = " << ex1.val2 << endl; //ex1.val2 默认初始化 未定义值
cout << "ex1.exam.val3 = " <<ex1.exam.val3 << endl; //ex1.exam 默认初始化 ex1.exam.val3 默认初始化 未定义值
cout << "exArr[1].val1 = " << exArr[1].val1 << endl; //exArr[1].val1值初始化 为0
cout << "exArr[1].val2 = " << exArr[1].val2 << endl; //exArr[1].val2值初始化 为0
cout << "exArr1[].exam.val3 = " << exArr[1].exam.val3 << endl; //exArr[1].exam值初始化 exArr[1].exam.val3值初始化 为0
}
10. 静态数据成员:只能在类外定义初始化,在类内声明。所有类对象都有同一个静态数据成员,这个静态数据成员都有同一个地址。不属于类对象,属于类
静态数据成员
class Counter
{
public:
void setCount(int num)
{
count = num;
}
void showCount()
{
cout << "count = " << count << endl;
}
private:
static int count;
};
int Counter::count = 0;
int main()
{
Counter c1, c2;
c1.showCount();
c2.showCount();
c1.setCount(20);
c1.showCount();
c2.showCount();
cout << endl;
return 0;
}
/*output
count = 0
count = 0
count = 20
count = 20
*/
上图只能得到一个静态实例,只会生成一次静态 instance
11. 常对象
12. 常成员函数
13. 友元
友元类
14. 类的组合