【C++初学】C++核心编程技术详解(二):类与继承

  1. 函数提高
    3.1 函数默认参数
    函数的形参可以有默认值,调用时可以省略这些参数。
    示例代码:
cpp 复制代码
int func(int a, int b = 10, int c = 10) {
    return a + b + c;
}

int main() {
    cout << "ret = " << func(20, 20) << endl;
    cout << "ret = " << func(100) << endl;
    system("pause");
    return 0;
}

3.2 函数占位参数

占位参数在函数声明中占位,调用时必须提供值。

示例代码:

cpp 复制代码
void func(int a, int) {
    cout << "this is func" << endl;
}

int main() {
    func(10, 10); // 占位参数必须提供值
    system("pause");
    return 0;
}

3.3 函数重载

函数重载允许函数名相同,但参数类型、个数或顺序不同。

示例代码:

cpp 复制代码
void func() {
    cout << "func 的调用!" << endl;
}

void func(int a) {
    cout << "func (int a) 的调用!" << endl;
}

void func(double a) {
    cout << "func (double a)的调用!" << endl;
}

void func(int a, double b) {
    cout << "func (int a ,double b) 的调用!" << endl;
}

int main() {
func();
func(10);
func(3.14);
func(10, 3.14);
func(3.14, 10);
system("pause");
return 0;
}
cpp 复制代码
void func(int& a)
{
	cout << "func(int &a)调用" << endl;
}


void func(const int& a)
{
	cout << "func(const int &a)调用" << endl;
}

void func2(int a, int b = 10)
{
	cout << "func2(int a , int b = 10)调用" << endl;
}

void func2(int a)
{
	cout << "func2(int a)调用" << endl;
}
int main()
{
	int a = 10;
	func(a);
	func(10);

	//func2(10);
	system("pause");
	return 0;
}


4 类和对象

面向对象的三大特性是封装继承多态。类是对象的抽象,对象是类的实例。

4.1 封装

封装是将属性和行为作为一个整体,表现生活中的事物,并加以权限控制。

4.1.1 封装的意义

示例代码:

cpp 复制代码
// 圆类
const double PI = 3.14;
class Circle {
public:
    int m_r; // 半径
    double calculateZC() { // 计算周长
        return 2 * PI * m_r;
    }
};

int main() {
    Circle c1;
    c1.m_r = 10;
    cout << "圆的周长为: " << c1.calculateZC() << endl;
    system("pause");
    return 0;
}

案例2:

cpp 复制代码
#include <string>
class Student
{
public:
	void setName(string name)
	{
		m_name = name;
	}
	void setID(int id)
	{
		m_id = id;
	}

	void showStudent()
	{
		cout << "name: " << m_name << "id: " << m_id << endl;
	}
public:
	string m_name;
	int m_id;
};
int main()
{
	Student stu;
	stu.setName("德玛西亚");
	stu.setID(250);
	stu.showStudent();
	system("pause");
	return 0;
}

4.1.2 struct 和 class 的区别

struct 默认权限为公共。

class 默认权限为私有。

示例代码:

cpp 复制代码
class c1   //默认私有
{
	int m_A;

};
struct c2 //默认公共
{
	int m_A;

};
int main()
{
	c1 c1;
	c1.m_A = 10;//err

	c2 c2;
	c2.m_A = 10;
	system("pause");
	return 0;
}

4.1.3 成员属性设置为私有

将成员属性设置为私有可以控制读写权限,并检测数据有效性。

示例代码:

cpp 复制代码
class Person {
public:
    void setName(string name) {
        m_Name = name;
    }
    string getName() {
        return m_Name;
    }
    int getAge() {
        return m_Age;
    }
    void setAge(int age) {
        if (age < 0 || age > 150) {
            cout << "你个老妖精!" << endl;
            return;
        }
        m_Age = age;
    }
    void setLover(string lover) {
        m_Lover = lover;
    }
private:
    string m_Name; // 姓名
    int m_Age; // 年龄
    string m_Lover; // 情人
};

int main() {
    Person p;
    p.setName("张三");
    cout << "姓名: " << p.getName() << endl;
    p.setAge(50);
    cout << "年龄: " << p.getAge() << endl;
    p.setLover("苍井");
    system("pause");
    return 0;
}

4.2 对象的初始化和清理

构造函数和析构函数是C++中用于对象初始化和清理的重要机制。

4.2.1 构造函数和析构函数

构造函数用于对象创建时的初始化,析构函数用于对象销毁前的清理。

示例代码:

cpp 复制代码
class Person {
public:
    Person() {
        cout << "Person的构造函数调用" << endl;
    }
    ~Person() {
        cout << "Person的析构函数调用" << endl;
    }
};

void test01() {
    Person p;
}

int main() {
    test01();
    system("pause");
    return 0;
}

4.2.2 构造函数的分类及调用

构造函数可以分为有参构造和无参构造,还可以分为普通构造和拷贝构造。

示例代码:

cpp 复制代码
class Person {
public:
    Person() {
        cout << "无参构造函数!" << endl;
    }
    Person(int a) {
        age = a;
        cout << "有参构造函数!" << endl;
    }
    Person(const Person& p) {
        age = p.age;
        cout << "拷贝构造函数!" << endl;
    }
    ~Person() {
        cout << "析构函数!" << endl;
    }
public:
    int age;
};

void test01() {
    Person p; // 调用无参构造函数
}

void test02() {
    Person p1(10); // 调用有参构造函数
    Person p2 = Person(10); // 调用拷贝构造函数
    Person p3 = p1; // 调用拷贝构造函数
}

int main() {
    test01();
    test02();
    system("pause");
    return 0;
}

4.2.3 拷贝构造函数调用时机

拷贝构造函数在以下三种情况下被调用:

使用一个已经创建完毕的对象来初始化一个新对象。

以值传递的方式给函数参数传值。

以值方式返回局部对象。

示例代码:

cpp 复制代码
class Person
{
public:
	Person()
	{
		cout << "无参构造函数" << endl;
	}
	Person(int a)
	{
		age = a;
		cout << "有参构造函数" << endl;
	}
	Person(const Person& p)
	{
		cout << "拷贝构造函数" << endl;

		age = p.age;
	}
	~Person()
	{
		cout << "析构函数" << endl;
	}
public:
	int age;
};

void test01()
{
	Person p;
}

void test02()
{
	Person p1(10);
	Person p2 = Person(10);
	Person p3 = Person(p2);


	Person p4 = 10;
	Person p5 = p4;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
cpp 复制代码
class Person
{
public:
	Person()
	{
		cout << "无参构造函数" << endl;
		mAge = 0;
	}
	Person(int age)
	{
		cout << "有参构造函数" << endl;
	}
	Person(const Person& p)
	{
		cout << "拷贝构造函数" << endl;
		mAge = p.mAge;
	}
	~Person()
	{
		cout << "析构函数" << endl;
	}
public:
	int mAge;
};

void test01()
{
	Person man(100);
	Person newman(man);
	Person newman2 = man;
}

void dowork(Person p1) {};
void test02()
{
	Person p;
	dowork(p);
}

Person dowork2()
{
	Person p1;
	cout << (int*)&p1 << endl;
	return p1;
}

void test03()
{
	Person p = dowork2();
	cout << (int*)&p << endl;
}
int main()
{
	//test01();
	//test02();
	test03();
	system("pause");
	return 0;
}



4.2.4 构造函数调用规则

如果用户定义了有参构造函数,编译器不会再提供默认无参构造函数,但会提供默认拷贝构造函数。

示例代码:

cpp 复制代码
class Person {
public:
    Person() {
        cout << "无参构造函数!" << endl;
    }
    Person(int a) {
        age = a;
        cout << "有参构造函数!" << endl;
    }
    Person(const Person& p) {
        age = p.age;
        cout << "拷贝构造函数!" << endl;
    }
    ~Person() {
        cout << "析构函数!" << endl;
    }
public:
    int age;
};

void test01() {
    Person p1(18);
    Person p2(p1); // 调用拷贝构造函数
    cout << "p2的年龄为: " << p2.age << endl;
}

void test02() {
    Person p1; // 如果用户没有提供默认构造函数,会报错
    Person p2(10); // 用户提供的有参构造函数
}

int main() {
    test01();
    test02();
    system("pause");
    return 0;
}


4.2.5 深拷贝与浅拷贝

浅拷贝只是简单地赋值,而深拷贝会在堆区重新申请空间。

示例代码:

cpp 复制代码
class Person {
public:
    Person() {
        cout << "无参构造函数!" << endl;
    }
    Person(int age, int height) {
        cout << "有参构造函数!" << endl;
        m_age = age;
        m_height = new int(height);
    }
    Person(const Person& p) {
        cout << "拷贝构造函数!" << endl;
        m_age = p.m_age;
        m_height = new int(*p.m_height); // 深拷贝
    }
    ~Person() {
        cout << "析构函数!" << endl;
        if (m_height != NULL) {
            delete m_height;
        }
    }
public:
    int m_age;
    int* m_height;
};

void test01() {
    Person p1(18, 180);
    Person p2(p1);
    cout << "p1的年龄: " << p1.m_age << " 身高: " << *p1.m_height << endl;
    cout << "p2的年龄: " << p2.m_age << " 身高: " << *p2.m_height << endl;
}

int main() {
    test01();
    system("pause");
    return 0;
4.2.6 初始化列表

初始化列表用于在构造函数中初始化成员变量。

示例代码:

cpp 复制代码
class Person {
public:
    Person(int a, int b, int c) : m_A(a), m_B(b), m_C(c) {
        cout << "构造函数调用" << endl;
    }
    void PrintPerson() {
        cout << "mA:" << m_A << endl;
        cout << "mB:" << m_B << endl;
        cout << "mC:" << m_C << endl;
    }
private:
    int m_A;
    int m_B;
    int m_C;
};

int main() {
    Person p(1, 2, 3);
    p.PrintPerson();
    system("pause");
    return 0;
}

4.2.7 类对象作为类成员

类中的成员可以是另一个类的对象。

示例代码:

cpp 复制代码
class Phone {
public:
    Phone(string name) {
        m_PhoneName = name;
        cout << "Phone构造" << endl;
    }
    ~Phone() {
        cout << "Phone析构" << endl;
    }
    string m_PhoneName;
};

class Person {
public:
    Person(string name, string pName) : m_Name(name), m_Phone(pName) {
        cout << "Person构造" << endl;
    }
    ~Person() {
        cout << "Person析构" << endl;
    }
    void playGame() {
        cout << m_Name << " 使用" << m_Phone.m_PhoneName << " 牌手机! " << endl;
    }
    string m_Name;
    Phone m_Phone;
};

void test01() {
    Person p("张三", "苹果X");
    p.playGame();
}

int main() {
    test01();
    system("pause");
    return 0;
}

4.2.8 静态成员

静态成员变量和静态成员函数是类的所有对象共享的。

示例代码:

cpp 复制代码
class Person {
public:
    static int m_A; // 静态成员变量
    static void func() {
        cout << "func调用" << endl;
        m_A = 100;
    }
    int m_B; // 非静态成员变量
};

int Person::m_A = 10;

void test01() {
    Person p1;
    p1.func();
    Person::func();
    cout << "p1.m_A = " << p1.m_A << endl;
    cout << "m_A = " << Person::m_A << endl;
}

int main() {
    test01();
    system("pause");
    return 0;
}
cpp 复制代码
class Person
{
public:
	static int m_A;
private:
	static int m_B;

};
int Person::m_A = 10;
int Person::m_B = 10;


void test01()
{
	Person p1;
	p1.m_A = 100;
	cout << "p1.m_A = " << p1.m_A << endl;

	Person p2;
	p2.m_A = 200;
	cout << "p1.m_A = " << p1.m_A << endl;
	cout << "p2.m_A = " << p2.m_A << endl;


	cout << "m_A = " << Person::m_A << endl;

}
int main()
{
	test01();
	system("pause");
	return 0;
}

4.3 C++对象模型和this指针

成员变量和成员函数分开存储

this指针指向被调用的成员函数所属的对象

示例代码:

cpp 复制代码
class Person {
public:
    Person(int age) {
        this->age = age;
    }
    Person& PersonAddPerson(Person p) {
        this->age += p.age;
        return *this;
    }
    int age;
};

void test01() {
    Person p1(10);
    cout << "p1.age = " << p1.age << endl;
    Person p2(10);
    p2.PersonAddPerson(p1).PersonAddPerson(p1).PersonAddPerson(p1);
    cout << "p2.age = " << p2.age << endl;
}

int main() {
    test01();
    system("pause");
    return 0;
}


空指针访问成员数组

4.4 友元

友元可以访问类的私有成员。

4.4.1 全局函数做友元

示例代码:

cpp 复制代码
class Building {
    friend void goodGay(Building *building);
    string m_BedRoom; // 卧室
public:
    Building() {
        m_BedRoom = "卧室";
    }
    string m_SittingRoom; // 客厅
};

void goodGay(Building *building) {
    cout << "好基友正在访问: " << building->m_SittingRoom << endl;
    cout << "好基友正在访问: " << building->m_BedRoom << endl;
}

void test01() {
    Building b;
    goodGay(&b);
}

int main() {
    test01();
    system("pause");
    return 0;
}

4.4.2 类做友元

示例代码:

cpp 复制代码
class Building {
    friend class goodGay;
    string m_BedRoom; // 卧室
public:
    Building() {
        m_BedRoom = "卧室";
    }
    string m_SittingRoom; // 客厅
};

class goodGay {
public:
    void visit(Building *building) {
        cout << "好基友正在访问" << building->m_SittingRoom << endl;
        cout << "好基友正在访问" << building->m_BedRoom << endl;
    }
};

void test01() {
    Building b;
    goodGay gg;
    gg.visit(&b);
}

int main() {
    test01();
    system("pause");
    return 0;
}

4.4.3 成员函数做友元

示例代码:

cpp 复制代码
class Building {
    friend void goodGay::visit(Building *building);
    string m_BedRoom; // 卧室
public:
    Building() {
        m_BedRoom = "卧室";
    }
    string m_SittingRoom; // 客厅
};

class goodGay {
public:
    void visit(Building *building) {
        cout << "好基友正在访问" << building->m_SittingRoom << endl;
        cout << "好基友正在访问" << building->m_BedRoom << endl;
    }
};

void test01() {
    Building b;
    goodGay gg;
    gg.visit(&b);
}

int main() {
    test01();
    system("pause");
    return 0;
}

4.5 运算符重载

运算符重载可以为自定义数据类型重新定义运算符的行为。

4.5.1 加号运算符重载

示例代码:

cpp 复制代码
class Person
{
public:
	Person() {};
	Person(int a, int b)
	{
		this->m_A = a;
		this->m_B = b;
	}
	Person operator+(const Person& p)
	{
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;
	}
public:
	int m_A;
	int m_B;

};

Person operator+(const Person& p2, int val)
{
	Person temp;
	temp.m_A = p2.m_A + val;
	temp.m_B = p2.m_B + val;
	return temp;
}

void test()
{
	Person p1(10, 10);
	Person p2(20, 20);

	Person p3 = p2 + p1;
	cout << "mA:" << p3.m_A << "mB:" << p3.m_B << endl;

	Person p4 = p3 + 10;
	cout << "mA:" << p4.m_A << "mB:" << p4.m_B << endl;

}
int main()
{
	test();
	system("pause");
	return 0;
}

4.5.2 左移运算符重载

示例代码:

cpp 复制代码
class Person {
    friend ostream& operator<<(ostream& out, Person& p);
public:
    Person(int a, int b) {
        this->m_A = a;
        this->m_B = b;
    }
private:
    int m_A;
    int m_B;
};

ostream& operator<<(ostream& out, Person& p) {
    out << "a:" << p.m_A << " b:" << p.m_B;
    return out;
}

void test() {
    Person p1(10, 20);
    cout << p1 << "hello world" << endl;
}

int main() {
    test();
    system("pause");
    return 0;
}

4.5.3 递增运算符重载

示例代码:

cpp 复制代码
class MyInteger {
    friend ostream& operator<<(ostream& out, MyInteger myint);
public:
    MyInteger() {
        m_Num = 0;
    }
    MyInteger& operator++() {
        m_Num++;
        return *this;
    }
    MyInteger operator++(int) {
        MyInteger temp = *this;
        m_Num++;
        return temp;
    }
private:
    int m_Num;
};

ostream& operator<<(ostream& out, MyInteger myint) {
    out << myint.m_Num;
    return out;
}

void test01() {
    MyInteger myInt;
    cout << ++myInt << endl;
    cout << myInt << endl;
}

void test02() {
    MyInteger myInt;
    cout << myInt++ << endl;
    cout << myInt << endl;
}

int main() {
    test01();
    test02();
    system("pause");
    return 0;
}

4.5.4 赋值运算符重载

示例代码:

cpp 复制代码
class Person {
public:
    Person(int age) {
        m_Age = new int(age);
    }
    Person& operator=(Person &p) {
        if (m_Age !=NULL) {
delete m_Age;
m_Age = NULL;
}
m_Age = new int(*p.m_Age);
return *this;
}
~Person() {
if (m_Age != NULL) {
delete m_Age;
m_Age = NULL;
}
}
int *m_Age;
};
void test01() {
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1;
cout << "p1的年龄为:" << *p1.m_Age << endl;
cout << "p2的年龄为:" << *p2.m_Age << endl;
cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main() {
test01();
system("pause");
return 0;
}
4.5.5 关系运算符重载

示例代码:

cpp 复制代码
class Person {
public:
    Person(string name, int age) {
        this->m_Name = name;
        this->m_Age = age;
    }
    bool operator==(Person & p) {
        if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
            return true;
        } else {
            return false;
        }
    }
    bool operator!=(Person & p) {
        if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
            return false;
        } else {
            return true;
        }
    }
    string m_Name;
    int m_Age;
};

void test01() {
    Person a("孙悟空", 18);
    Person b("孙悟空", 18);
    if (a == b) {
        cout << "a和b相等" << endl;
    } else {
        cout << "a和b不相等" << endl;
    }
    if (a != b) {
        cout << "a和b不相等" << endl;
    } else {
        cout << "a和b相等" << endl;
    }
}

int main() {
    test01();
    system("pause");
    return 0;
}

4.5.6 函数调用运算符重载

示例代码:

cpp 复制代码
class MyPrint {
public:
    void operator()(string text) {
        cout << text << endl;
    }
};

class MyAdd {
public:
    int operator()(int v1, int v2) {
        return v1 + v2;
    }
};

void test01() {
    MyPrint myFunc;
    myFunc("hello world");
}

void test02() {
    MyAdd add;
    int ret = add(10, 10);
    cout << "ret = " << ret << endl;
    cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}

int main() {
    test01();
    test02();
    system("pause");
    return 0;
}

5. 继承

继承是面向对象的三大特性之一,允许子类继承父类的成员。

5.1 继承的基本语法

示例代码:

cpp 复制代码
class BasePage {
public:
    void header() {
        cout << "首页、公开课、登录、注册...(公共头部)" << endl;
    }
    void footer() {
        cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
    }
    void left() {
        cout << "Java,Python,C++...(公共分类列表)" << endl;
    }
};

class Java : public BasePage {
public:
    void content() {
        cout << "JAVA学科视频" << endl;
    }
};

class Python : public BasePage {
public:
    void content() {
        cout << "Python学科视频" << endl;
    }
};

class CPP : public BasePage {
public:
    void content() {
        cout << "C++学科视频" << endl;
    }
};

void test01() {
    cout << "Java下载视频页面如下: " << endl;
    Java ja;
    ja.header();
    ja.footer();
    ja.left();
    ja.content();
    cout << "--------------------" << endl;
    cout << "Python下载视频页面如下: " << endl;
    Python py;
    py.header();
    py.footer();
    py.left();
    py.content();
    cout << "--------------------" << endl;
    cout << "C++下载视频页面如下: " << endl;
    CPP cp;
    cp.header();
    cp.footer();
    cp.left();
    cp.content();
}

int main() {
    test01();
    system("pause");
    return 0;
}

5.2 继承方式

继承方式有三种:公共继承、保护继承和私有继承。

示例代码:

cpp 复制代码
class Base1 {
public:
    int m_A;
protected:
    int m_B;
private:
    int m_C;
};

class Son1 : public Base1 {
public:
    void func() {
        m_A; // 可访问 public 权限
        m_B; // 可访问 protected 权限
        // m_C; // 不可访问
    }
};

class Base2 {
public:
    int m_A;
protected:
    int m_B;
private:
    int m_C;
};

class Son2 : protected Base2 {
public:
    void func() {
        m_A; // 可访问 protected 权限
        m_B; // 可访问 protected 权限
        // m_C; // 不可访问
    }
};

class Base3 {
public:
    int m_A;
protected:
    int m_B;
private:
    int m_C;
};

class Son3 : private Base3 {
public:
    void func() {
        m_A; // 可访问 private 权限
        m_B; // 可访问 private 权限
        // m_C; // 不可访问
    }
};

class GrandSon3 : public Son3 {
public:
    void func() {
        // Son3 是私有继承,所以继承 Son3 的属性在 GrandSon3 中都无法访问到
        // m_A;
        // m_B;
        // m_C;
    }
};

5.3 继承中的对象模型

父类中的私有成员也会被子类继承,但无法直接访问。

示例代码:

cpp 复制代码
class Base {
public:
    int m_A;
protected:
    int m_B;
private:
    int m_C;
};

class Son : public Base {
public:
    int m_D;
};

void test01() {
    cout << "sizeof Son = " << sizeof(Son) << endl;
}

int main() {
    test01();
    system("pause");
    return 0;
}

5.4 继承中构造和析构顺序

创建子类对象时,先调用父类的构造函数,再调用子类的构造函数。析构顺序相反。

示例代码:

cpp 复制代码
class Base {
public:
    Base() {
        cout << "Base构造函数!" << endl;
    }
    ~Base() {
        cout << "Base析构函数!" << endl;
    }
};

class Son : public Base {
public:
    Son() {
        cout << "Son构造函数!" << endl;
    }
    ~Son() {
        cout << "Son析构函数!" << endl;
    }
};

void test01() {
    Son s;
}

int main() {
    test01();
    system("pause");
    return 0;
}

5.5 继承同名成员处理方式

子类与父类出现同名成员时,可以通过作用域区分。

示例代码:

cpp 复制代码
class Base {
public:
    void func() {
        cout << "Base - func()调用" << endl;
    }
    void func(int a) {
        cout << "Base - func(int a)调用" << endl;
    }
    int m_A;
};

class Son : public Base {
public:
    void func() {
        cout << "Son - func()调用" << endl;
    }
    int m_A;
};

void test01() {
    Son s;
    cout << "Son下的m_A = " << s.m_A << endl;
    cout << "Base下的m_A = " << s.Base::m_A << endl;
    s.func();
    s.Base::func();
    s.Base::func(10);
}

int main() {
    test01();
    system("pause");
    return 0;
}

5.6 继承同名静态成员处理方式

静态成员同名处理方式与非静态成员相同。

示例代码:

cpp 复制代码
class Base {
public:
    static void func() {
        cout << "Base - static void func()" << endl;
    }
    static void func(int a) {
        cout << "Base - static void func(int a)" << endl;
    }
    static int m_A;
};

int Base::m_A = 100;

class Son : public Base {
public:
static void func() {
cout << "Son - static void func()" << endl;
}
static int m_A;
};
int Son::m_A = 200;
void test01() {
cout << "通过对象访问: " << endl;
Son s;
cout << "Son 下 m_A = " << s.m_A << endl;
cout << "Base 下 m_A = " << s.Base::m_A << endl;
cout << "通过类名访问: " << endl;
cout << "Son 下 m_A = " << Son::m_A << endl;
cout << "Base 下 m_A = " << Son::Base::m_A << endl;
}
void test02() {
cout << "通过对象访问: " << endl;
Son s;
s.func();
s.Base::func();
cout << "通过类名访问: " << endl;
Son::func();
Son::Base::func();
Son::Base::func(100);
}
int main() {
test01();
test02();
system("pause");
return 0;
}

5.7 多继承语法

C++允许一个类继承多个类。

示例代码:

cpp 复制代码
class Base1 {
public:
    Base1() {
        m_A = 100;
    }
    int m_A;
};

class Base2 {
public:
    Base2() {
        m_A = 200;
    }
    int m_A;
};

class Son : public Base2, public Base1 {
public:
    Son() {
        m_C = 300;
        m_D = 400;
    }
    int m_C;
    int m_D;
};

void test01() {
    Son s;
    cout << "sizeof Son = " << sizeof(s) << endl;
    cout << s.Base1::m_A << endl;
    cout << s.Base2::m_A << endl;
}

int main() {
    test01();
    system("pause");
    return 0;
}

5.8 菱形继承

菱形继承是指两个派生类继承同一个基类,而另一个类又同时继承这两个派生类。

示例代码:

cpp 复制代码
class Animal {
public:
    int m_Age;
};

class Sheep : virtual public Animal {};

class Tuo : virtual public Animal {};

class SheepTuo : public Sheep, public Tuo {};

void test01() {
    SheepTuo st;
    st.Sheep::m_Age = 100;
    st.Tuo::m_Age = 200;
    cout << "st.Sheep::m_Age = " << st.Sheep::m_Age << endl;
    cout << "st.Tuo::m_Age = " << st.Tuo::m_Age << endl;
    cout << "st.m_Age = " << st.m_Age << endl;
}

int main() {
    test01();
    system("pause");
    return 0;
}
相关推荐
mahuifa44 分钟前
(2)VTK C++开发示例 --- 绘制多面锥体
c++·vtk·cmake·3d开发
23级二本计科1 小时前
C++ Json-Rpc框架-3项目实现(2)
服务器·开发语言·c++·rpc
rigidwill6662 小时前
LeetCode hot 100—搜索二维矩阵
数据结构·c++·算法·leetcode·矩阵
矛取矛求2 小时前
栈与队列习题分享(精写)
c++·算法
摆烂能手3 小时前
C++基础精讲-02
开发语言·c++
胡乱儿起个名3 小时前
C++ 标准库中的 <algorithm> 头文件算法总结
开发语言·c++·算法
政安晨4 小时前
【嵌入式人工智能产品开发实战】(二十)—— 政安晨:小智AI嵌入式终端代码解读:【C】关于项目中的MQTT+UDP核心通信交互理解
网络·c++·mqtt·网络协议·udp·小智ai·实时打断
六bring个六5 小时前
C++双链表介绍及实现
开发语言·数据结构·c++
Ring__Rain8 小时前
visual studio 常用的快捷键(已经熟悉的就不记录了)
c++·git·visual studio
辛姜_千尘红回8 小时前
AT_abc398_e [ABC398E] Tree Game 题解
c语言·c++·笔记·算法