第三天 C++核心编程(2)

接上文

4.类和对象

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

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

在C++中,类内的成员变量和成员函数分开存储

只有非静态成员变量才属于类的对象上

#include<iostream>
using namespace std;
class Person
{
public:
Person();
~Person();
void ppp()
{

}
static void sss()
{

}
public:
int m_a;
static int m_b;
};

Person::Person()
{
}

Person::~Person()
{
}
int main()
{
cout << sizeof(Person) << endl;
system("pause");
return 0;
}
//非静态成员变量占对象空间
//静态成员变量不占对象空间
//函数也不占对象空间,所有函数共享一个函数实例
//静态成员函数也不占对象空间

从零开始学编程---C++篇(3)_ios

4.3.2 this指针概念

通过4.3.1我们知道在C++中成员变量和成员函数是分开存储的

每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码

那么问题是:这一块代码是如何区分那个对象调用自己的呢?

c++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象

this指针是隐含每一个非静态成员函数内的一种指针

this指针不需要定义,直接使用即可

this指针的用途:

  • 当形参和成员变量同名时,可用this指针来区分
  • 在类的非静态成员函数中返回对象本身,可使用return *this
#include<iostream>
using namespace std;
class Person
{
public:
Person();
Person(int a);
~Person();
Person& PersonAddPerson(Person p)
{
this->a += p.a;
//返回对象本身
return *this;
}
public:
int a;

};
void test01()
{
Person p1(10);
cout << "p1.a=" << p1.a << endl;
Person p2(10);
cout << "p2.a=" << p2.a << endl;
p2.PersonAddPerson(p1).PersonAddPerson(p1).PersonAddPerson(p1);
cout << "p2.a=" << p2.a << endl;
}
Person::Person()
{
}
Person::Person(int a)
{
this->a = a;
}
Person::~Person()
{
}
int main()
{
test01();
system("pause");
return 0;
}

从零开始学编程---C++篇(3)_#include_02

4.3.3 空指针访问成员函数

C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针

如果用到this指针,需要加以判断保证代码的健壮性

#include<iostream>
using namespace std;
//空指针访问成员函数
class Person {
public:

void ShowClassName() {
cout << "我是Person类!" << endl;
}

void ShowPerson() {
if (this == NULL) {
return;
}
cout << mAge << endl;
}

public:
int mAge;
};

void test01()
{
Person * p = NULL;
p->ShowClassName(); //空指针,可以调用成员函数
p->ShowPerson(); //但是如果成员函数中用到了this指针,就不可以了
}

int main() {

test01();

system("pause");

return 0;
}
4.3.4 const修饰成员函数

常函数:

  • 成员函数后加const后我们称为这个函数为常函数
  • 常函数内不可以修改成员属性
  • 成员属性声明时加关键字mutable后,在常函数中依然可以修改

常对象:

  • 声明对象前加const称该对象为常对象
  • 常对象只能调用常函数
#include<iostream>
using namespace std;
class Person
{
public:
Person();
~Person();
void func() const
{
//m_a = 100; const常函数不可修改成员变量
m_b = 200;
}
void func1()
{
m_a = 100;
m_b = 20;
}
public:
int m_a;
mutable int m_b;
};

Person::Person()
{
m_a = 0;
m_b = 0;
}

Person::~Person()
{
}
void test01()
{
const Person p;
p.func();
cout << person.m_A << endl;
//person.mA = 100; //常对象不能修改成员变量的值,但是可以访问
//p.m_a = 10; const修饰的常对象不可修改成员变量
p.m_b = 20;
//p.func1(); 常对象只能调用常变量
}
int main()
{
test01();
system("pause");
return 0;
}

4.4 友元

生活中你的家有客厅(Public),有你的卧室(Private)

客厅所有来的客人都可以进去,但是你的卧室是私有的,也就是说只有你能进去

但是呢,你也可以允许你的好闺蜜好基友进去。

在程序里,有些私有属性 也想让类外特殊的一些函数或者类进行访问,就需要用到友元的技术

友元的目的就是让一个函数或者类 访问另一个类中私有成员

友元的关键字为 friend

友元的三种实现

  • 全局函数做友元
  • 类做友元
  • 成员函数做友元
4.4.1 全局函数做友元
#include<iostream>
using namespace std;
class Buliding
{
friend void GoodGay(Buliding buliding);
public:
Buliding();
~Buliding();

public:
string m_settingroom;
private:
string m_bedroom;
};

Buliding::Buliding()
{
this->m_settingroom = "客厅";
this->m_bedroom = "卧室";
}

Buliding::~Buliding()
{
}
void GoodGay(Buliding buliding)
{
cout << "好朋友正在访问" << buliding.m_settingroom << endl;
cout << "好朋友正在访问" << buliding.m_bedroom << endl;
}
void test01()
{
Buliding b;
GoodGay(b);
}
int main()
{
test01();
system("pause");
return 0;
}

从零开始学编程---C++篇(3)_#include_03

4.4.2 类做友元
#include<string>
#include<iostream>
using namespace std;
class Buliding;
class goodGay
{
public:
goodGay();
~goodGay();
void visit();
private:
Buliding* building;
};
class Buliding
{
friend class goodGay;
public:
Buliding();
~Buliding();

public:
string settingroom;
private:
string bedroom;

};
goodGay::goodGay()
{
building = new Buliding;
}

goodGay::~goodGay()
{
}
Buliding::Buliding()
{
this->bedroom = "卧室";
this->settingroom = "客厅";
}

Buliding::~Buliding()
{
}
void goodGay::visit()
{
cout << "好基友正在访问" << building->settingroom << endl;
cout << "好基友正在访问" << building->bedroom << endl;
}
void test01()
{
goodGay gg;
gg.visit();
}
int main()
{
test01();
system("pause");
return 0;
}

从零开始学编程---C++篇(3)_成员函数_04

4.4.3 成员函数做友元
#include<string>
#include<iostream>
using namespace std;
class Building;
class goodGay
{
public:

goodGay();
void visit(); //只让visit函数作为Building的好朋友,可以发访问Building中私有内容
void visit2();

private:
Building *building;
};


class Building
{
//告诉编译器 goodGay类中的visit成员函数 是Building好朋友,可以访问私有内容
friend void goodGay::visit();

public:
Building();

public:
string m_SittingRoom; //客厅
private:
string m_BedRoom;//卧室
};

Building::Building()
{
this->m_SittingRoom = "客厅";
this->m_BedRoom = "卧室";
}

goodGay::goodGay()
{
building = new Building;
}

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

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

void test01()
{
goodGay gg;
gg.visit();

}

int main(){

test01();

system("pause");
return 0;
}

从零开始学编程---C++篇(3)_#include_05

4.5 运算符重载

运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

4.5.1 加号运算符重载

作用:实现两个自定义数据类型相加计算

1.成员函数重载加号运算符

#include<iostream>
using namespace std;
class Person
{
public:
Person();
//有参构造函数
Person(int a, int b);
~Person();
//成员函数重载运算+
Person operator+(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::Person()
{
}
Person::Person(int a, int b)
{
m_a = a;
m_b = b;
}
Person::~Person()
{
}
void test01()
{
Person p(10, 10);
Person p1(20, 30);
Person p2 = p + p1;
cout << "p2.m_a=" << p2.m_a <<"p2.m_b"<<p2.m_b<< endl;
}
int main()
{
test01();
system("pause");
return 0;
}

从零开始学编程---C++篇(3)_成员函数_06

2.全局函数重载加号运算符

#include<iostream>
using namespace std;
class Person
{
public:
Person();
//有参构造函数
Person(int a, int b);
~Person();

public:
int m_a;
int m_b;

};

Person::Person()
{
}
Person::Person(int a, int b)
{
m_a = a;
m_b = b;
}
Person::~Person()
{
}
Person operator+(Person& p, Person& p1)
{
Person temp;
temp.m_a = p.m_a + p1.m_a;
temp.m_b = p.m_b + p1.m_b;
return temp;
}
void test01()
{
Person p(10, 10);
Person p1(20, 30);
Person p2 = p + p1;
cout << "p2.m_a=" << p2.m_a <<"p2.m_b"<<p2.m_b<< endl;
}
int main()
{
test01();
system("pause");
return 0;
}

总结1:对于内置的数据类型的表达式的的运算符是不可能改变的

总结2:不要滥用运算符重载

4.5.2 左移运算符重载

作用:可以输出自定义数据类型

#include<string>
#include<iostream>
using namespace std;
class Person {
friend ostream& operator<<(ostream& out, Person& p);

public:

Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}

//成员函数 实现不了 p << cout 不是我们想要的效果
//void operator<<(Person& p){
//}

private:
int m_A;
int m_B;
};

//全局函数实现左移重载
//ostream对象只能有一个
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 递增运算符重载

作用:通过重载递增运算符,实现自己的整形数据

#include<iostream>
using namespace std;
class MyInteger
{
friend ostream& operator<<(ostream& out, MyInteger my);
public:
MyInteger();
~MyInteger();
//前置++//返回引用是为了一直对一个数进行操作
MyInteger& operator++()
{
m_num++;
return *this;
}
//后置++
MyInteger operator++(int)
{
MyInteger t = *this;
m_num++;
return t;

}
private:
int m_num;
};

MyInteger::MyInteger()
{
m_num = 0;
}

MyInteger::~MyInteger()
{
}
ostream& operator<<(ostream& out, MyInteger my)
{
out << my.m_num;
return out;
}
void test01()
{
MyInteger my;
cout << ++my << endl;
cout << my << endl;
cout << my++ << endl;
cout << my << endl;
}
int main()
{
test01();
system("pause");
return 0;
}

从零开始学编程---C++篇(3)_ios_07

4.5.4 赋值运算符重载

c++编译器至少给一个类添加4个函数

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝
  4. 赋值运算符 operator=, 对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

#include<iostream>
using namespace std;
class Person
{
public:
Person();
Person(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();

public:
int* m_age;
};

Person::Person()
{
}
Person::Person(int age)
{
m_age = new int(age);
}
Person::~Person()
{
if (m_age!=NULL)
{
delete m_age;
m_age = NULL;
}
}
void test01()
{
Person p(18);
Person p2(20);
Person p3(30);
p = p2 = p3;
cout << "p=" << *p.m_age << endl;
cout << "p2=" << *p2.m_age << endl;
cout << "p3=" << *p3.m_age << endl;
}
int main()
{
test01();
system("pause");
return 0;
}

从零开始学编程---C++篇(3)_#include_08

4.5.5 关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
Person();
Person(string name, int age);
~Person();
bool operator==(Person& p)
{
if (this->m_age==p.m_age&&this->m_name==p.m_name)
{
return true;

}
else
{
return false;
}
}
public:
string m_name;
int m_age;

};
void test01()
{
Person p1("zhangsan", 10);
Person p2("zhangsan", 12);
if (p1==p2)
{
cout << "p1和p2相等" << endl;
}
else
{
cout << "p1和p2不相等" << endl;
}
}
Person::Person()
{
}
Person::Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
};
Person::~Person()
{
}
int main()
{
test01();
system("pause");
return 0;
}

从零开始学编程---C++篇(3)_ios_09

4.5.6 函数调用运算符重载
  • 函数调用运算符 () 也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活
#include<iostream>
using namespace std;
class Myprint
{
public:
Myprint();
~Myprint();
public:
void operator()(string str)
{
cout << str << endl;
}

};

Myprint::Myprint()
{
}

Myprint::~Myprint()
{
}
class MyAdd
{
public:
MyAdd();
MyAdd(int a, int b);
~MyAdd();
int operator()(int a, int b)
{
return a + b;
}
public:
int m_a;
int m_b;

};

MyAdd::MyAdd()
{
}
MyAdd::MyAdd(int a, int b)
{
m_a = a;
m_b = b;
}

MyAdd::~MyAdd()
{
}
void test01()
{
Myprint my;
my("vnivn");
}
void test02()
{
MyAdd ma(20,30);
int ret=ma(20, 30);
cout << "ret=" << ret << endl;
}

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

从零开始学编程---C++篇(3)_成员函数_10

4.6 继承

继承是面向对象三大特性之一

有些类与类之间存在特殊的关系,例如下图中:

从零开始学编程---C++篇(3)_成员函数_11

我们发现,定义这些类时,下级别的成员除了拥有上一级的共性,还有自己的特性。

这个时候我们就可以考虑利用继承的技术,减少重复代码

4.6.1 继承的基本语法

例如我们看到很多网站中,都有公共的头部,公共的底部,甚至公共的左侧列表,只有中心内容不同

接下来我们分别利用普通写法和继承的写法来实现网页中的内容,看一下继承存在的意义以及好处

普通实现:

#include<iostream>
using namespace std;
//java界面
class Java
{
public:
Java();
~Java();
void header()
{
cout << "首页、公开课、登录、注册...(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
}
void left()
{
cout << "Java,Python,C++...(公共分类列表)" << endl;
}
void content()
{
cout << "Java学科视频" << endl;
}
};

Java::Java()
{
}

Java::~Java()
{
}
//python界面
class Python
{
public:
Python();
~Python();
void header()
{
cout << "首页、公开课、登录、注册...(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
}
void left()
{
cout << "Java,Python,C++...(公共分类列表)" << endl;
}
void content()
{
cout << "Python学科视频" << endl;
}

};

Python::Python()
{
}

Python::~Python()
{
}
//c++界面
class Cpp
{
public:
Cpp();
~Cpp();
void header()
{
cout << "首页、公开课、登录、注册...(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
}
void left()
{
cout << "Java,Python,C++...(公共分类列表)" << endl;
}
void content()
{
cout << "Cpp学科视频" << endl;
}

};

Cpp::Cpp()
{
}

Cpp::~Cpp()
{
}
void test01()
{
//Java页面
cout << "Java下载视频页面如下: " << endl;
Java ja;
ja.header();
ja.footer();
ja.left();
ja.content();
cout << "--------------------" << endl;

//Python页面
cout << "Python下载视频页面如下: " << endl;
Python py;
py.header();
py.footer();
py.left();
py.content();
cout << "--------------------" << endl;

//C++页面
cout << "C++下载视频页面如下: " << endl;
Cpp cp;
cp.header();
cp.footer();
cp.left();
cp.content();

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

从零开始学编程---C++篇(3)_#include_12

继承实现:

#include<iostream>
using namespace std;
//公共界面
class base
{
public:
base();
~base();
void header()
{
cout << "首页、公开课、登录、注册...(公共头部)" << endl;
}

void footer()
{
cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
}
void left()
{
cout << "Java,Python,C++...(公共分类列表)" << endl;
}

};

base::base()
{
}

base::~base()
{
}
//Java
class Java:public base
{
public:
Java();
~Java();
void content()
{
cout << "java学科视频" << endl;
}

};

Java::Java()
{
}

Java::~Java()
{
}
//python
class Python:public base
{
public:
Python();
~Python();
void content()
{
cout << "Python学科视频" << endl;
}

};

Python::Python()
{
}

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

};

cpp::cpp()
{
}

cpp::~cpp()
{
}
void test01()
{
//Java页面
cout << "Java下载视频页面如下: " << endl;
Java ja;
ja.header();
ja.footer();
ja.left();
ja.content();
cout << "--------------------" << endl;

//Python页面
cout << "Python下载视频页面如下: " << endl;
Python py;
py.header();
py.footer();
py.left();
py.content();
cout << "--------------------" << endl;

//C++页面
cout << "C++下载视频页面如下: " << endl;
cpp cp;
cp.header();
cp.footer();
cp.left();
cp.content();


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

从零开始学编程---C++篇(3)_成员函数_13

总结:

继承的好处:可以减少重复的代码

class A : public B;

A 类称为子类 或 派生类

B 类称为父类 或 基类

派生类中的成员,包含两大部分

一类是从基类继承过来的,一类是自己增加的成员。

从基类继承过过来的表现其共性,而新增的成员体现了其个性。

4.6.2 继承方式

继承的语法:​​class 子类 : 继承方式 父类​

继承方式一共有三种:

  • 公共继承
  • 保护继承
  • 私有继承

从零开始学编程---C++篇(3)_#include_14

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; //不可访问
}
};

void myClass()
{
Son1 s1;
s1.m_A; //其他类只能访问到公共权限
}

//保护继承
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; //不可访问
}
};
void myClass2()
{
Son2 s;
//s.m_A; //不可访问
}

//私有继承
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;
}
};
4.6.3 继承中的对象模型

问题: 从父类继承过来的成员,哪些属于子类对象中?

示例:

#include<iostream>
using namespace std;
class Base
{
public:
Base();
~Base();

public:
int m_a;
protected:
int m_b;
private:
int m_c;

};

Base::Base()
{
}

Base::~Base()
{
}
class son :public Base
{
public:
son();
~son();

private:
int m_d;

};

son::son()
{
}

son::~son()
{
}
void test01()
{
cout << "sizeof snotallow=" << sizeof(son) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}

从零开始学编程---C++篇(3)_成员函数_15

 结论: 父类中私有成员也是被子类继承下去了,只是由编译器给隐藏后访问不到 

4.6.4 继承中构造和析构顺序

子类继承父类后,当创建子类对象,也会调用父类的构造函数

问题:父类和子类的构造和析构顺序是谁先谁后?

#include<iostream>
using namespace std;
class Base
{
public:
Base();
~Base();


};

Base::Base()
{
cout << "调用构造函数Base()" << endl;
}

Base::~Base()
{
cout << "调用析构函数~Base()" << endl;

}
class Son:public Base
{
public:
Son();
~Son();


};

Son::Son()
{
cout << "调用构造函数Son()" << endl;

}

Son::~Son()
{
cout << "调用析构函数~Son()" << endl;
}
void test01()
{
Son s;
}
int main()
{
test01();
system("pause");
return 0;
}

从零开始学编程---C++篇(3)_#include_16

4.6.5 继承同名成员处理方式

问题:当子类与父类出现同名的成员,如何通过子类对象,访问到子类或父类中同名的数据呢?

  • 访问子类同名成员 直接访问即可

访问父类同名成员 需要加作用域

#include<iostream>
using namespace std;
class Base
{
public:
Base();
~Base();
void func()
{
cout << "调用Base中的函数func()" << endl;
}
void func(int a)
{
cout << "调用Base中的函数func(int a)" << endl;
}
public:
int m_a;

};

Base::Base()
{
m_a = 100;
}

Base::~Base()
{
}
class Son:public Base
{
public:
Son();
~Son();
void func()
{
cout << "调用Son中的函数func()" << endl;
}
public:
int m_a;
};

Son::Son()
{
m_a = 200;
}

Son::~Son()
{
}
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;
}

从零开始学编程---C++篇(3)_ios_17

总结:

  1. 子类对象可以直接访问到子类中同名成员
  2. 子类对象加作用域可以访问到父类同名成员
  3. 当子类与父类拥有同名的成员函数,子类会隐藏父类中同名成员函数,加作用域可以访问到父类中同名函数
4.6.6 继承同名静态成员处理方式

问题:继承中同名的静态成员在子类对象上如何进行访问?

静态成员和非静态成员出现同名,处理方式一致

  • 访问子类同名成员 直接访问即可
  • 访问父类同名成员 需要加作用域
#include<iostream>
using namespace std;
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;
}

总结:同名静态成员处理方式和非静态处理方式一样,只不过有两种访问的方式(通过对象 和 通过类名)

4.6.7 多继承语法

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

语法:​​class 子类 :继承方式 父类1 , 继承方式 父类2...​

多继承可能会引发父类中有同名成员出现,需要加作用域区分

C++实际开发中不建议用多继承

#include<iostream>
using namespace std;
class Base1
{
public:
Base1();
~Base1();

public:
int m_a;

};

Base1::Base1()
{
m_a = 100;
}

Base1::~Base1()
{
}
class Base2
{
public:
Base2();
~Base2();

public:
int m_a;

};

Base2::Base2()
{
m_a = 200;
}

Base2::~Base2()
{
}
class Son:public Base1,public Base2
{
public:
Son();
~Son();

public:
int m_b;
int m_c;

};

Son::Son()
{
m_b = 300;
m_c = 400;
}

Son::~Son()
{
}
void test01()
{
Son s;
cout << "sizeof snotallow=" << sizeof(s) << endl;
cout << "Base1中的m_a=" << s.Base1::m_a << endl;
cout << "Base2中的m_a=" << s.Base2::m_a << endl;
cout << "Son中的m_c=" << s.m_c << endl;


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

从零开始学编程---C++篇(3)_成员函数_18

总结: 多继承中如果父类中出现了同名情况,子类使用时候要加作用域

4.6.8 菱形继承

菱形继承概念:

两个派生类继承同一个基类

又有某个类同时继承者两个派生类

这种继承被称为菱形继承,或者钻石继承

菱形继承问题:

  1. 羊继承了动物的数据,驼同样继承了动物的数据,当羊驼使用数据时,就会产生二义性。
  2. 羊驼继承自动物的数据继承了两份,其实我们应该清楚,这份数据我们只需要一份就可以。
#include<iostream>
using namespace std;
class Animal
{
public:
Animal();
~Animal();

public:
int m_age;

};

Animal::Animal()
{
}

Animal::~Animal()
{
}
class Sheep:virtual public Animal
{
public:
Sheep();
~Sheep();


};

Sheep::Sheep()
{
}

Sheep::~Sheep()
{
}
class Tuo:virtual public Animal
{
public:
Tuo();
~Tuo();



};

Tuo::Tuo()
{
}

Tuo::~Tuo()
{
}
class SheepTuo:public Sheep,public Tuo
{
public:
SheepTuo();
~SheepTuo();


};

SheepTuo::SheepTuo()
{
}

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

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

从零开始学编程---C++篇(3)_成员函数_19