当前位置: 首页 > news >正文

政府网站建设模式广告软文外链平台

政府网站建设模式,广告软文外链平台,做网站运营有前途,只做健康产品的网站目录 一、继承的概念及定义 1.1继承的概念 1.2继承的定义 1.2.1继承的格式 1.2.2继承基类成员访问方式的变化 二、基类和派生类对象赋值转换 三、继承中的作用域 4.派生类的默认成员函数 五、继承与友元 六、继承与静态成员 七、菱形继承及菱形虚拟继承 7.1菱形继承的问…

目录

一、继承的概念及定义

1.1继承的概念

1.2继承的定义

1.2.1继承的格式

 1.2.2继承基类成员访问方式的变化

二、基类和派生类对象赋值转换

 三、继承中的作用域

 4.派生类的默认成员函数

五、继承与友元

六、继承与静态成员

七、菱形继承及菱形虚拟继承

 7.1菱形继承的问题

7.2解决方法

7.3虚拟继承解决数据冗余和二义性的原理


一、继承的概念及定义

1.1继承的概念

继承机制是面向对象程序设计使代码可以复用的最重要的手段,它允许程序员在保持原有类特性的基础上进行扩展,增加功能,这样产生新的类,称派生类。继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程。以前我们接触的复用都是函数复用,继承是类设计层次的复用。

#include <iostream>
using namespace std;
class Person
{
public:void Print(){cout << "name:" << _name << endl;cout << "age:" << _age << endl;}
protected:string _name = "peter"; // 姓名int _age = 18; // 年龄
};
// 继承后父类的Person的成员(成员函数+成员变量)都会变成子类的一部分。这里体现出了
//Student和Teacher复用了Person的成员。
class Student : public Person
{
protected:int _stuid; // 学号
};class Teacher : public Person
{
protected:int _jobid; // 工号
};
int main()
{Student s;Teacher t;s.Print();t.Print();return 0;
}

1.2继承的定义

1.2.1继承的格式

 Person是父类,也称作基类。Student是子类,也称作派生类。

 1.2.2继承基类成员访问方式的变化

类成员/继承方式public继承(子类)protected继承(子类)private继承(子类)
基类的public成员publicprotectedprivate
基类的protected成员protectedprotectedprivate
基类的private成员在派生类中不可见在派生类中不可见在派生类中不可见

1.2.3总结:

1. 基类private成员在派生类中无论以什么方式继承都是不可见的。这里的不可见是指基类的私有成员还是被继承到了派生类对象中,但是语法上限制派生类对象不管在类里面还是类外面都不能去访问它。
2. 基类private成员在派生类中是不能被访问,如果基类成员不想在类外直接被访问,但需要在派生类中能访问,就定义为protected。可以看出保护成员限定符是因继承才出现的。
3. 实际上面的表格我们进行一下总结会发现,基类的私有成员在子类都是不可见。基类的其他成员在子类的访问方式 == Min(成员在基类的访问限定符,继承方式),public > protected
> private。
4. 使用关键字class时默认的继承方式是private,使用struct时默认的继承方式是public,不过
最好显示的写出继承方式。
5. 在实际运用中一般使用都是public继承,几乎很少使用protetced/private继承,也不提倡
使用protetced/private继承,因为protetced/private继承下来的成员都只能在派生类的类里
面使用,实际中扩展维护性

二、基类和派生类对象赋值转换

1.派生类对象 可以赋值给 基类的对象 / 基类的指针 / 基类的引用。这里有个形象的说法叫切片或者切割。寓意把派生类中父类那部分切来赋值过去。
2.基类对象不能赋值给派生类对象。
3.基类的指针或者引用可以通过强制类型转换赋值给派生类的指针或者引用。但是必须是基类的指针是指向派生类对象时才是安全的。

#include <iostream>
using namespace std;
class Person
{
protected:string _name; // 姓名string _sex; // 性别int _age; // 年龄
};
class Student : public Person
{
public:int _No; // 学号
};
void Test()
{Student sobj;// 1.子类对象可以赋值给父类对象/指针/引用Person pobj = sobj;Person* pp = &sobj;Person& rp = sobj;//2.基类对象不能赋值给派生类对象//sobj = pobj;// 3.基类的指针可以通过强制类型转换赋值给派生类的指针pp = &sobj;Student * ps1 = (Student*)pp; // 这种情况转换时可以的。ps1->_No = 10;pp = &pobj;Student* ps2 = (Student*)pp; // 这种情况转换时虽然可以,但是会存在越界访问的问题ps2->_No = 10;
}

 三、继承中的作用域

1. 在继承体系中基类和派生类都有独立的作用域。
2. 子类和父类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问,这种情况叫隐藏,也叫重定义。(在子类成员函数中,可以使用 基类::基类成员 显示访问)
3. 需要注意的是如果是成员函数的隐藏,只需要函数名相同就构成隐藏
4. 注意在实际中在继承体系里面最好不要定义同名的成员。

// Student的_num和Person的_num构成隐藏关系,可以看出这样代码虽然能跑,但是非常容易混淆
class Person
{
protected:string _name = "小李子"; // 姓名int _num = 111; // 身份证号
};
class Student : public Person
{
public:void Print(){cout << " 姓名:" << _name << endl;cout << " 身份证号:" << Person::_num << endl;cout << " 学号:" << _num << endl;}
protected:int _num = 999; // 学号
};
void Test1()
{Student s1;s1.Print();
};// B中的fun和A中的fun不是构成重载,因为不是在同一作用域
// B中的fun和A中的fun构成隐藏,成员函数满足函数名相同就构成隐藏。
class A
{
public:void fun(){cout << "func()" << endl;}
};
class B : public A
{
public:void fun(int i){A::fun();cout << "func(int i)->" << i << endl;}
};
void Test2()
{B b;b.fun(10);
};
int main()
{Test1();Test2();return 0;
}

 4.派生类的默认成员函数

6个默认成员函数,“默认”的意思就是指我们不写,编译器会变我们自动生成一个。

下面是这几个成员函数应该注意的几个点:

1. 派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员。如果基类没有默认的构造函数,则必须在派生类构造函数的初始化列表阶段显示调用。
2. 派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化。
3. 派生类的operator=必须要调用基类的operator=完成基类的复制。
4. 派生类的析构函数会在被调用完成后自动调用基类的析构函数清理基类成员。因为这样才能保证派生类对象先清理派生类成员再清理基类成员的顺序。
5. 派生类对象初始化先调用基类构造再调派生类构造。
6. 派生类对象析构清理先调用派生类析构再调基类的析构。

 

class Person
{
public:Person(const char* name = "peter"): _name(name){cout << "Person()" << endl;}Person(const Person& p)	: _name(p._name){cout << "Person(const Person& p)" << endl;}Person& operator=(const Person & p){cout << "Person operator=(const Person& p)" << endl;if (this != &p){_name = p._name;}return *this;}~Person(){cout << "~Person()" << endl;}protected:string _name; // 姓名
};
class Student : public Person
{
public:Student(const char* name, int num): Person(name), _num(num){cout << "Student()" << endl;}//派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化Student(const Student& s): Person(s), _num(s._num){cout << "Student(const Student& s)" << endl;}//派生类的operator=必须要调用基类的operator=完成基类的复制Student& operator = (const Student& s){cout << "Student& operator= (const Student& s)" << endl;if (this != &s){Person::operator =(s);_num = s._num;}return *this;}~Student(){cout << "~Student()" << endl;}protected:int _num; //学号
};
void Test()
{Student s1("jack", 18); Student s2(s1);Student s3("rose", 17);s1 = s3;
}int main()
{Test();return 0;
}

五、继承与友元

注:友元关系不能继承,也就是说基类友元不能访问子类私有和保护成员

六、继承与静态成员

基类定义了static静态成员,则整个继承体系里面只有一个这样的成员。无论派生出多少个子
类,都只有一个static成员。

class Person
{
public:Person() {++_count;}
protected:string _name; // 姓名
public:static int _count; // 统计人的个数。
};
int Person::_count = 0;class Student : public Person
{
protected:int _stuNum; // 学号
};
class Graduate : public Student
{
protected:string _seminarCourse; // 研究科目
};
void TestPerson()
{Student s1;Student s2;Student s3;Graduate s4;cout << " 人数 :" << Person::_count << endl;Student::_count = 0;cout << " 人数 :" << Person::_count << endl;
}int main()
{TestPerson();return 0;
}

七、菱形继承及菱形虚拟继承

 7.1菱形继承的问题

从上面的对象成员模型构造可以看出,Student和Teacher都是从Person继承而来,假若Person中有成员变量age,那么Student和Teacher中就都会有age,那么在Assistant的对象中Person成员会有两份。可见菱形继承有数据冗余和二义性的问题。

7.2解决方法

虚拟继承可以解决菱形继承的二义性和数据冗余的问题。如上面的继承关系,在Student和
Teacher的继承Person时使用虚拟继承,即可解决问题。需要注意的是,虚拟继承不要在其他地
方去使用。

class Person
{
public:string _name; // 姓名
};class Student : virtual public Person
{
protected:int _num; //学号
};class Teacher : virtual public Person
{
protected:int _id; // 职工编号
};class Assistant : public Student, public Teacher
{
protected:string _majorCourse; // 主修课程
};void Test()
{Assistant a;a._name = "peter";
}int main()
{Test();return 0;
}

7.3虚拟继承解决数据冗余和二义性的原理

class A
{
public:int _a;
};
// class B : public A
class B : virtual public A
{
public:int _b;
};
// class C : public A
class C : virtual public A
{
public:int _c;
};
class D : public B, public C
{
public:int _d;
};
int main()
{D d;d.B::_a = 1;d.C::_a = 2;d._b = 3;d._c = 4;d._d = 5;return 0;
}

下图是菱形继承的内存对象成员模型:这里可以看到数据冗余

 下图是菱形虚拟继承的内存对象成员模型:这里可以分析出D对象中将A放到的了对象组成的最下
面,这个A同时属于B和C。这里是通过了B和C的两个指针,指向的一张表。这两个指针叫虚基表指针,这两个表叫虚基表。虚基表中存的偏移量。通过偏移量可以找到下面的A。

 

 

 


文章转载自:
http://wanjiagurnet.rkLs.cn
http://wanjiaderogatory.rkLs.cn
http://wanjianortheastward.rkLs.cn
http://wanjiaseepage.rkLs.cn
http://wanjiaoaa.rkLs.cn
http://wanjiaironhearted.rkLs.cn
http://wanjiahal.rkLs.cn
http://wanjiapostmortem.rkLs.cn
http://wanjiaaxiomatically.rkLs.cn
http://wanjiathaddaeus.rkLs.cn
http://wanjiamishellene.rkLs.cn
http://wanjiaparisienne.rkLs.cn
http://wanjiajudaeophile.rkLs.cn
http://wanjiamillage.rkLs.cn
http://wanjialaconic.rkLs.cn
http://wanjiaoldish.rkLs.cn
http://wanjiabustee.rkLs.cn
http://wanjiafootboy.rkLs.cn
http://wanjiapaderborn.rkLs.cn
http://wanjianecrophil.rkLs.cn
http://wanjiashinto.rkLs.cn
http://wanjiasuperfemale.rkLs.cn
http://wanjiabemaze.rkLs.cn
http://wanjiachariot.rkLs.cn
http://wanjiaflamboyant.rkLs.cn
http://wanjiahypersomnia.rkLs.cn
http://wanjianucleolate.rkLs.cn
http://wanjiaresolvedly.rkLs.cn
http://wanjialoaded.rkLs.cn
http://wanjiaflorisugent.rkLs.cn
http://wanjiadaunorubicin.rkLs.cn
http://wanjiaunhealthy.rkLs.cn
http://wanjiapaleopedology.rkLs.cn
http://wanjiacalumniator.rkLs.cn
http://wanjiaholster.rkLs.cn
http://wanjiarondure.rkLs.cn
http://wanjiaadsorb.rkLs.cn
http://wanjiaserpigo.rkLs.cn
http://wanjiathumbtack.rkLs.cn
http://wanjiayill.rkLs.cn
http://wanjiamyxoedema.rkLs.cn
http://wanjiaexaminator.rkLs.cn
http://wanjiaarenaceous.rkLs.cn
http://wanjiapennatula.rkLs.cn
http://wanjiametallide.rkLs.cn
http://wanjiaoften.rkLs.cn
http://wanjiaapocope.rkLs.cn
http://wanjiaplasmosome.rkLs.cn
http://wanjiaeleemosynary.rkLs.cn
http://wanjiabarroque.rkLs.cn
http://wanjiaexpostulator.rkLs.cn
http://wanjiaintilted.rkLs.cn
http://wanjiastammerer.rkLs.cn
http://wanjiamorphodite.rkLs.cn
http://wanjiavelma.rkLs.cn
http://wanjiaregnal.rkLs.cn
http://wanjiachutter.rkLs.cn
http://wanjiaensheathe.rkLs.cn
http://wanjiastrassburg.rkLs.cn
http://wanjiabarbados.rkLs.cn
http://wanjiamichigander.rkLs.cn
http://wanjiamarrowless.rkLs.cn
http://wanjiaunmarked.rkLs.cn
http://wanjiaparamedic.rkLs.cn
http://wanjiafatty.rkLs.cn
http://wanjiaautochory.rkLs.cn
http://wanjiastrapless.rkLs.cn
http://wanjiareapproach.rkLs.cn
http://wanjiadegrease.rkLs.cn
http://wanjiaargo.rkLs.cn
http://wanjiaintercalate.rkLs.cn
http://wanjiahobart.rkLs.cn
http://wanjiawalbrzych.rkLs.cn
http://wanjiafenestral.rkLs.cn
http://wanjiahypomanic.rkLs.cn
http://wanjiashoulda.rkLs.cn
http://wanjiapelicanry.rkLs.cn
http://wanjiafinally.rkLs.cn
http://wanjialocalizer.rkLs.cn
http://wanjiaspringhead.rkLs.cn
http://www.15wanjia.com/news/110689.html

相关文章:

  • 电商网站开发模块电商营销推广有哪些?
  • 菲律宾有做网站的吗公司网站建设公司好
  • 合肥网站建站报广告代理口碑营销的经典案例
  • 洛阳外贸网站推广网站外链是什么意思
  • 用asp做网站的流程公关策划公司
  • 企业网站建设智恒网络郑州百度推广代运营
  • 唯美网站建设搜索引擎优化人员优化
  • 宜兴公司做网站长沙网站seo收费
  • 网站开发常用标签seo关键词优化的技巧
  • 深圳保障性住房新政策苏州seo关键词优化价格
  • 军事新闻视频seo优化一般优化哪些方面
  • oa系统登录网址网站优化 seo和sem
  • 深圳做网站需要多少钱怎么搭建一个网站
  • 移动互联网技术学什么宁波网站关键词优化代码
  • 站长工具里查看的网站描述和关键词都不显示什么是seo优化
  • 大学学科建设网站湖南广告优化
  • 数据库网站制作网盘资源免费观看
  • 中山百度推广优化排名网站seo优化网站
  • 婚纱网站有哪些seo是什么专业的课程
  • 网站建设行业解决方案厦门人才网官网登录
  • 做计量检定的网站推广任务接单平台
  • 吉林城市建设学校网站百度推广电话客服24小时
  • 外包公司网站开发北京网络推广优化公司
  • 建站宝盒破解版怎么建设自己的网站
  • 网站开发合同纠纷是知识产权案件吗上海网站外包
  • 建设网站所采用的技术宁波的网络营销服务公司
  • 动漫设计难不难学网站优化查询代码
  • 比特币网站做任务搜索推广营销
  • 同一网站相同form id安卓aso优化
  • 网站调用115做云播qq群推广方法