补充:
构造函数:一种特殊的成员函数,不能声明为const 的
常量指针this:this类型为“A *const”,总是指向调用成员函数的“这个”对象;
const成员函数:用于修改隐式this指针的类型,修改后this指针类型为“const A *const”。相当于为this指针增加了“底层const”,能够引起重载;
可变数据成员(mutable关键字):对于可变数据成员,任何成员函数,包括const函数在内都能改变它的值。
使用友元类时注意:
|
#include <iostream> #include <string> using namespace std; //友元类 class cFriend { private: int va; int vb; public: cFriend() { va = 0; vb = 0; } cFriend(int a, int b) { va = a; vb = b; } ~cFriend() { cout << "执行结束!!!" << endl; } void set_va(int a) { va = a; } void set_vb(int b) { vb = b; } void disp(); friend class cFriends; }; class cFriends { private: int x; int y; public: cFriends() { x = 0; y = 0; } cFriends(int a, int b) { x = a; y = b; } ~cFriends() { cout << "友元类调用结束!!!" << endl; } void disps() { cout << "友元类显示:" << endl; cout << " x = " << x << endl; cout << " y = " << y << endl; } void dispc(cFriend data); void dispMember(cFriend data) { cout << "友元类访问公有:\n"; data.disp(); } }; void cFriends::dispc(cFriend data) { cout << "友元类访问:" << endl; cout << " va = " << data.va << endl; cout << " vb = " << data.vb << endl; } void cFriend::disp() { cout << "成员函数调用:" << endl; cout << " va = " << va << endl; cout << " vb = " << vb << endl; } int main() { cFriend data1(4, 5); cFriends data2(5, 6); data1.disp(); data2.disps(); data2.dispc(data1); data2.dispMember(data1); return 0; }
-----------------------------------------补充完------------------------------------------------------------------------------------
1.
默认构造函数是椭圆的,矩形的需要初始化,因此用new数组
2. 继承
B 是 C 的直接基类,A 是B 的直接基类
3. public、protected、private继承
public继承:
private继承:子类可以访问父类的 public 和 protected 成员
class Base { int v1; public: int v2; Base(int a = 0, int b = 0, int c = 0) :v1(a), v2(b), v5(c) {} protected: int v5; }; class Drived :private Base { int v3; public: int v4; Drived(int a = 0, int b = 0) :v3(a), v4(b) {} void func() { //cout << "v1=" << v1 << endl;//不可访问 cout << "v2=" << v2 << endl; cout << "v3=" << v3 << endl; cout << "v4=" << v4 << endl; cout << "v5=" << v5 << endl; } }; int main() { Drived obj = Drived(5, 6); //不可访问 /*obj.v1 = 8; obj.v2 = 6; obj.v3 = 4;*/ obj.v4 = 9; //obj.v5 = 10; //不可访问 obj.func(); cout << endl; return 0; }
protected继承:
class Base { int v1; public: int v2; Base(int a = 0, int b = 0, int c = 0) :v1(a), v2(b), v5(c) {} protected: int v5; }; class Drived :protected Base { int v3; public: int v4; Drived(int a = 0, int b = 0) :v3(a), v4(b) {} void func() { //cout << "v1=" << v1 << endl;//不可访问 cout << "v2=" << v2 << endl; cout << "v3=" << v3 << endl; cout << "v4=" << v4 << endl; cout << "v5=" << v5 << endl; } }; int main() { Drived obj = Drived(5, 6); //不可访问 /*obj.v1 = 8; obj.v2 = 6; obj.v3 = 4;*/ obj.v4 = 9; //obj.v5 = 10; //不可访问 obj.func(); cout << endl; return 0; }
5. 基类和派生类同名成员
6. 单继承派生类和基类的类对象转换
7. 单继承派生类初始化
8。 派生类和基类的构造和析构函数调用顺序
class Base { public: Base() { cout << "Base obj created.\n"; } ~Base() { cout << "Base obj deleted.\n"; } }; class Drived :public Base { public: Drived() { cout << "Drived obj created.\n"; } ~Drived() { cout << "Drived obj delete.\n"; } }; int main() { Drived obj; cout << endl; return 0; } /*output Base obj created. Drived obj created. Drived obj delete. Base obj deleted. */
9. 多继承派生类构造和析构
基类对象的调用顺序
基类对象与成员对象的调用顺序
析构函数
10. 访问不同基类成员的二义性
11. 访问相同基类成员的二义性
更好的解决方法:设置共同基类为虚基类
先调用谁,与声明顺序有关
11.1
//由于先调用level2中的base基类,因此level1中的bese是假基类,整体函数不调用level1的base基类
11.2
----------------------明天有补充----------------------------
12. 多态
12.1 编译时的多态的实现方式
A. 函数重载
B. 运算符重载
12.2 运行时的多态的实现方式
A. 虚函数
---------------------明天有补充--------------------------
13. 类类型