0
点赞
收藏
分享

微信扫一扫

量化交易之C++篇 - 继承(基础、虚基类、继承方式、构造与析构的调用顺序、同名处理、静态成员的处理、多继承、菱形继承)


// 继承的基础
#include <iostream>

using namespace std;

class BaseWeb {

public:
void header() {
cout << "公共头部" << endl;
}

void footer() {
cout << "公共底部" << endl;
}

void left() {
cout << "左侧列表" << endl;
}

};

class News: public BaseWeb { // 继承

public:

void content() {
cout << "新闻播放" << endl;
}

};

class Entertainment: public BaseWeb {

public:

void content() {
cout << "白百合..." << endl;
}

};

class Game : public BaseWeb {

public:
void content() {
cout << "KPL 直播" << endl;
}
};

void test01() {

News news;
news.header();
news.footer();
news.content();

Entertainment entertainment;
entertainment.header();
entertainment.footer();
entertainment.content();
}

int main() {

test01();

return EXIT_SUCCESS;
}

// 虚基类
#include <iostream>
#include <string>

using namespace std;


class Animal {
public:
int age;
};

class Sheep : virtual public Animal { // 虚基类

};

class Tuo : virtual public Animal {

};

class SheepTuo : public Sheep, public Tuo {

};


// 菱形继承的解决方案 利用虚基类
// 操作的是共享的一份数据
void test01() {

SheepTuo sheepTuo;
sheepTuo.Sheep::age = 10;
sheepTuo.Tuo::age = 20;

cout << sheepTuo.Sheep::age << endl;
cout << sheepTuo.Tuo::age << endl;
cout << sheepTuo.age << endl; // 可以直接访问了
}


int main() {

test01();

return EXIT_SUCCESS;
}

// 继承方式
#include <iostream>

using namespace std;

class Base1 {
public:
int a;
protected:
int b;
private:
int c;
};

class Son1: public Base1 {

public:
void publicFunction() {
cout << this->a << endl; // 父类中 共有的属性 可继承, 继承后是 public;
cout << this->b << endl; // 父类中 保护的属性 可继承, 继承后是 protected;
//cout << this->c << endl; // 父类中私有的属性 不可继承;
}
};

void test01() {

Son1 son1;

son1.a = 10;

cout << son1.a << endl;
}


class Base2 {
public:
int a = 0;
protected:
int b = 0;
private:
int c = 0;
};

class Son2 : protected Base2 {

public:
void func() {
cout << this->a << endl; // 父类中 共有的属性 可继承, 继承后是 protected
cout << this->b << endl; // 父类中 保护的属性 可继承, 继承后是 protected
//cout << this->c << endl; // 父类中私有的属性 不可继承;
}
};

class GrandSon2 : public Son2 {

public:
void publicFunction() {
cout << this->a << endl;
cout << this->b << endl;
}
protected:
void protectedFunction() {
cout << this->a << endl;
cout << this->b << endl;
}
private:
void privateFunction() {
cout << this->a << endl;
cout << this->b << endl;
}
};
void test02() {
Son2 son2;

GrandSon2 grandSon2;

//son1.a = 10; // 继承后的 a 是保护属性, 外部不可以访问
//cout << son1.a << endl;
//grandSon2.a = 10; // 再次继承后的 a 也是保护属性, 外部不可以访问
}


class Base3 {
public:
int a;
protected:
int b;
private:
int c;
};

class Son3 : private Base3 {

public:
void publicFunction() {
cout << this->a << endl; // 父类中 共有的属性 可继承, 继承后是 private;
cout << this->b << endl; // 父类中 保护的属性 可继承, 继承后是 private;
//cout << this->c << endl; // 父类中私有的属性 不可继承;
}
};

class GrandSon3 : public Son3 {

public:
void publicFunction() {
//cout << this->a << endl; // 父类中 私有的属性 不可继承;
//cout << this->b << endl; // 父类中 私有的属性 不可继承;
//cout << this->c << endl; // 父类中 私有的属性 不可继承;
}

};

void test03() {
Son3 son3;

son3.publicFunction();
}

int main() {

test03();

return EXIT_SUCCESS;
}

// 析构与构造函数的调用顺序
#include <iostream>

using namespace std;


class Base {

public:
int a;
protected:
int b;
private:
int c;

};

// 实际上 子类中会继承父类的 private 成员变量, 只是被编译器给隐藏起来了, 所以访问不到私有成员;
class Son : public Base {

public:
int d;

};

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


class Base2 {

public:
Base2(int a) {
cout << "Base2 有参构造函数 调用" << endl;
this->a = a;
}

~Base2() {
cout << "Base2 析构函数 调用" << endl;
}

int a;
};


class Son2 : public Base2 {
public:
Son2(int a): Base2(a) { // 利用初始化列表方式 显示调用 有参构造
cout << "Son2 有参构造函数 调用" << endl;
}

~Son2() {
cout << "Son2 析构函数 调用" << endl;
}
};

void test02() {
Son2 son2(1000); // 构造函数先调用父类, 后调用子类; 析构函数先调用父类, 后调用子类; 另外 子类不会继承父类的构造函数和析构函数;
}

int main() {

test02();

return EXIT_SUCCESS;
}

// 继承中的同名处理
#include <iostream>

using namespace std;

class Base {

public:
Base() {
this->a = 100;
}

void publicFunction() {
cout << "Base public function" << endl;
}

void publicFunction(int a) {
cout << "Base public function(int a)" << endl;
}

int a;
};

class Son : public Base {

public:
Son() {
this->a = 200;
}

void publicFunction() {
cout << "Son public function" << endl;
}

void publicFunction(int a) {
cout << "Son public function(int a)" << endl;
}

int a;
};

void test01() {
Son son1;

// 成员属性
cout << son1.a << endl; // 如无特殊声明, 子类对象会直接调用本类的属性;
cout << son1.Base::a << endl; // 子类对象 调用父类的 a 属性;

// 成员函数
son1.publicFunction(); // 直接调用时, 会先调用子类;
son1.Base::publicFunction(); // 父类的同名函数会被隐藏, 除非用作用域运算符去调用;

son1.publicFunction(10);
son1.Base::publicFunction(10);
}

int main() {

test01();

return EXIT_SUCCESS;
}

// 继承中静态成员的处理
#include <iostream>

using namespace std;

class Base {
public:
static int a;

static void publicFunction() {
cout << "Base public function: " << endl;
}

static void publicFunction(int a) {
cout << "Base public function(int a): " << a << endl;
}

};
int Base::a = 10;


class Son : public Base {

public:
static int a;

static void publicFunction() {
cout << "Son public function: " << endl;
}

static void publicFunction(int a) {
cout << "Son public function(int a): " << a << endl;
}

};
int Son::a = 20;


void test01() {

cout << "Son::a : " << Son::a << endl; // 静态成员属性, 子类可以继承
cout << "Son::Base::a : " << Son::Base::a << endl;

}

void test02() {
Son::publicFunction(100);
Son::Base::publicFunction(1000);

Son::publicFunction();
Son::Base::publicFunction();
}


int main() {

test02();

return EXIT_SUCCESS;
}

// 多继承
#include <iostream>
#include <string>

using namespace std;


class Animal {
public:
int age;
};

class Sheep : virtual public Animal { // 虚基类

};

class Tuo : virtual public Animal {

};

class SheepTuo : public Sheep, public Tuo {

};


// 菱形继承的解决方案 利用虚基类
// 操作的是共享的一份数据
void test01() {

SheepTuo sheepTuo;
sheepTuo.Sheep::age = 10;
sheepTuo.Tuo::age = 20;

cout << sheepTuo.Sheep::age << endl;
cout << sheepTuo.Tuo::age << endl;
cout << sheepTuo.age << endl; // 可以直接访问了
}

int main() {

test01();

return EXIT_SUCCESS;
}

// 菱形继承的概念以及问题
#include <iostream>
#include <string>

using namespace std;


class Animal {
public:
int age;
};

class Sheep : virtual public Animal { // 虚基类

};

class Tuo : virtual public Animal {

};

class SheepTuo : public Sheep, public Tuo {

};


// 菱形继承的解决方案 利用虚基类
// 操作的是共享的一份数据
void test01() {

SheepTuo sheepTuo;
sheepTuo.Sheep::age = 10;
sheepTuo.Tuo::age = 20;

cout << sheepTuo.Sheep::age << endl;
cout << sheepTuo.Tuo::age << endl;
cout << sheepTuo.age << endl; // 可以直接访问了
}


int main() {

test01();

return EXIT_SUCCESS;
}

 

举报

相关推荐

0 条评论