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

网站维护步骤网络推广优化平台

网站维护步骤,网络推广优化平台,eclipse做企业网站,sae wordpress 4.4c继承(下) (1)继承与友元(2)继承与静态成员(3)多继承及其菱形继承问题3.1 继承模型3.2 虚继承3.3 多继承中指针偏移问题 (4)继承和组合(9&#xf…

c++继承(下)

  • (1)继承与友元
  • (2)继承与静态成员
  • (3)多继承及其菱形继承问题
    • 3.1 继承模型
    • 3.2 虚继承
    • 3.3 多继承中指针偏移问题
  • (4)继承和组合
  • (9)继承的总结

(1)继承与友元

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

class student;//前置声明class person
{
public:friend void func(person a,student b);//这里使用了student这个类,编译器只支持向下查找,必须前置声明void identify(){}
private:int _age;string _name = "zhangsan";
};class student:public person
{
public:friend void func(person a, student b);void study(){}private:int _stuid = 111;
};void func(person a,student b)
{cout << a._name << ' ' << b._stuid << endl;
}
int main()
{person aa;student bb;func(aa,bb);return 0;
}

要想让两个类都可以使用func函数,这里的解决办法是同时声明友元即可。

(2)继承与静态成员

基类定义了static静态成员,则整个继承体系⾥⾯只有⼀个这样的成员。⽆论派⽣出多少个派⽣类,都只有⼀个static成员实例。

class person
{
public:static int _count;void identify(){}
private:int _age;string _name = "zhangsan";
};int person::_count = 1;class student:public person
{
public:void study(){}private:int _stuid = 111;
};int main()
{person aa;student bb;person::_count++;cout << aa._count << endl;cout << bb._count << endl;cout << &aa._count << endl;cout << &bb._count << endl;return 0;
}

这份代码的运行结果:
在这里插入图片描述

我们可以看到count的地址没有发生变化,所以count只存在一份

(3)多继承及其菱形继承问题

3.1 继承模型

单继承:⼀个派⽣类只有⼀个直接基类时称这个继承关系为单继承

多继承:⼀个派⽣类有两个或以上直接基类时称这个继承关系为多继承,多继承对象在内存中的模型是,先继承的基类在前⾯,后⾯继承的基类在后⾯,派⽣类成员在放到最后⾯。

菱形继承:菱形继承是多继承的⼀种特殊情况。菱形继承的问题,从下⾯的对象成员模型构造,可以看出菱形继承有数据冗余和⼆义性的问题,在Assistant的对象中Person成员会有两份。⽀持多继承就⼀定会有菱形继承,像Java就直接不⽀持多继承,规避掉了这⾥的问题,所以实践中我们也是不建议设计出菱形继承这样的模型的。

单继承:
在这里插入图片描述
多继承:
在这里插入图片描述

菱形继承:
在这里插入图片描述

class person
{
public:static int _count;void identify(){}string _name = "zhangsan";
private:int _age;
};class student:public person
{
public:void study(){}
private:int _stuid = 111;//学号
};class teacher:public person
{
public:void teach(){}
private:string teaid;//教师编号
};//老师助理,这里的意思是教师助理既作为教授的学生,又作为学校里的老师
class assistant:public student,public teacher
{};//这里就是一个菱形继承
int main()
{assistant a;//a._name = "tsy";//这里具有二义性a.student::_name = "张三";a.teacher::_name = "李四";//这样指定是可以的//并且person在a里是有两份的,数据冗余不能解决//并且这里是有两个名字的,一个作为学生,一个作为老师,这样是不太好的,person里的名字应该是类似于身份证上的名字,我们最好不好去更改return 0;
}

3.2 虚继承

//这里引入了virtual,它被使用于产生数据冗余的地方,例如person会在student与teacher里各存一份,这里会产生数据冗余
class person
{
public:static int _count;void identify(){}string _name = "zhangsan";
private:int _age;
};class student :virtual public person
{
public:void study(){}
private:int _stuid = 111;//学号
};class teacher :virtual public person
{
public:void teach(){}
private:string teaid;//教师编号
};//老师助理,这里的意思是教师助理既作为教授的学生,又作为学校里的老师
class assistant :public student, public teacher
{};int main()
{assistant a;a._name = "tsy";//这里就可以使用了//这里给定的name就类似于身份证上的名字return 0;
}

virtual使用于产生数据冗余的地方,并且不可能单独出现

3.3 多继承中指针偏移问题

class Base1 
{
public: int _b1;
};
class Base2 
{ 
public: int _b2; 
};
class Derive : public Base1, public Base2 
{ 
public: int _d;
};int main()
{Derive d;Base1* p1 = &d;Base2* p2 = &d;Derive* p3 = &d;return 0;
}
//A:p1 == p2 == p3  B:p1 < p2 < p3  C:p1 == p3 != p2  D:p1 != p2 != p3

在这里插入图片描述
所以答案选择 C

(4)继承和组合

public继承是⼀种is-a的关系。也就是说每个派⽣类对象都是⼀个基类对象。

组合是⼀种has-a的关系。假设B组合了A,每个B对象中都有⼀个A对象。

• 继承允许你根据基类的实现来定义派⽣类的实现。这种通过⽣成派⽣类的复⽤通常被称为⽩箱复⽤(white-box reuse)。术语“⽩箱”是相对可视性⽽⾔:在继承⽅式中,基类的内部细节对派⽣类可⻅ 。继承⼀定程度破坏了基类的封装,基类的改变,对派⽣类有很⼤的影响。派⽣类和基类间的依赖关系很强,耦合度⾼。

• 对象组合是类继承之外的另⼀种复⽤选择。新的更复杂的功能可以通过组装或组合对象来获得。对象组合要求被组合的对象具有良好定义的接⼝。这种复⽤⻛格被称为⿊箱复⽤(black-box reuse),因为对象的内部细节是不可⻅的。对象只以“⿊箱”的形式出现。 组合类之间没有很强的依赖关系,耦合度低。优先使⽤对象组合有助于你保持每个类被封装。

• 优先使⽤组合,⽽不是继承。实际尽量多去⽤组合,组合的耦合度低,代码维护性好。不过也不太那么绝对,类之间的关系就适合继承(is-a)那就⽤继承,另外要实现多态,也必须要继承。类之间的关系既适合⽤继承(is-a)也适合组合(has-a),就⽤组合。

// Tire(轮胎)和Car(⻋)更符合has-a的关系
class Tire {
protected:string _brand = "Michelin"; // 品牌size_t _size = 17;// 尺⼨
};
class Car {
protected:string _colour = "⽩⾊";// 颜⾊string _num = "陕ABIT00";// ⻋牌号Tire _t1;// 轮胎Tire _t2;// 轮胎Tire _t3;// 轮胎Tire _t4;// 轮胎
};
class BMW : public Car {
public:void Drive() { cout << "好开-操控" << endl; }
};
// Car和BMW/Benz更符合is-a的关系
class Benz : public Car {
public:void Drive() { cout << "好坐-舒适" << endl; }
};
template<class T>
class vector
{};
// stack和vector的关系,既符合is-a,也符合has-a
template<class T>
class stack : public vector<T>
{};
template<class T>
class stack
{
public:vector<T> _v;
};
int main()
{return 0;
}

(9)继承的总结

1、继承在生活中常见的只有公有继承,所以公有继承一定要弄明白。

2、派生类的默认成员函数与之前学的普通类的是一样的,如果我们需要显示的去写构造函数等等,需要将父类作为一个整体,去调用父类的构造等函数。

3、继承这块是先构造父类再构造子类,析构是先析构子类再析构父类

4、菱形继承目前阶段我们把握不住,并且用到的场景也可以通过其他方式解决,所以最好不要写菱形继承,但是学校可能会考,所以也是需要简单了解下的。

5、如果我们需要设计一个不能被继承的类,我们有两种方式:
(1)将基类构造函数变为私有
(2)final

class Base final
{
public:
void func5() { cout << "Base::func5" << endl; }
protected:
int a = 1;
private:
// C++98的⽅法
/*Base()
{}*/
};
class Derive :public Base
{
void func4() { cout << "Derive::func4" << endl; }
protected:
int b = 2;
};
int main()
{
Base b;
Derive d;
return 0;
}

这一点只做了解


文章转载自:
http://dinncoseismographer.wbqt.cn
http://dinncocavalryman.wbqt.cn
http://dinncolagune.wbqt.cn
http://dinncounexaggerated.wbqt.cn
http://dinncoandroecium.wbqt.cn
http://dinncointimacy.wbqt.cn
http://dinncoanomaloscope.wbqt.cn
http://dinncopushy.wbqt.cn
http://dinncostatehood.wbqt.cn
http://dinncoromanticize.wbqt.cn
http://dinncocompanion.wbqt.cn
http://dinncodanmark.wbqt.cn
http://dinncolaith.wbqt.cn
http://dinncoceratin.wbqt.cn
http://dinncobiogeocenosis.wbqt.cn
http://dinncoconverted.wbqt.cn
http://dinncofeud.wbqt.cn
http://dinncodingo.wbqt.cn
http://dinncowolflike.wbqt.cn
http://dinncolx.wbqt.cn
http://dinncounroot.wbqt.cn
http://dinncoligate.wbqt.cn
http://dinncorosery.wbqt.cn
http://dinncoshutoff.wbqt.cn
http://dinncoorthoclastic.wbqt.cn
http://dinncobiflex.wbqt.cn
http://dinncobidon.wbqt.cn
http://dinncovaticanologist.wbqt.cn
http://dinncointerpellation.wbqt.cn
http://dinncocosmologic.wbqt.cn
http://dinncoramachandra.wbqt.cn
http://dinncolandward.wbqt.cn
http://dinncoallochthonous.wbqt.cn
http://dinncogeoisotherm.wbqt.cn
http://dinncovcr.wbqt.cn
http://dinncoplanula.wbqt.cn
http://dinncosternutation.wbqt.cn
http://dinncoburberry.wbqt.cn
http://dinncomassiliot.wbqt.cn
http://dinncoliteralness.wbqt.cn
http://dinncoafferent.wbqt.cn
http://dinncohypothermia.wbqt.cn
http://dinncocowhage.wbqt.cn
http://dinncoedacity.wbqt.cn
http://dinncogazabo.wbqt.cn
http://dinncorandan.wbqt.cn
http://dinncoopposeless.wbqt.cn
http://dinncotowardly.wbqt.cn
http://dinncospawny.wbqt.cn
http://dinncouptight.wbqt.cn
http://dinncoeveryway.wbqt.cn
http://dinncocifs.wbqt.cn
http://dinncocolobus.wbqt.cn
http://dinncotentability.wbqt.cn
http://dinncoassumptive.wbqt.cn
http://dinncocottonade.wbqt.cn
http://dinncofrenzy.wbqt.cn
http://dinncoperpent.wbqt.cn
http://dinncointelligibly.wbqt.cn
http://dinncomayanist.wbqt.cn
http://dinncosmolder.wbqt.cn
http://dinncogesamtkunstwerk.wbqt.cn
http://dinncoshoestring.wbqt.cn
http://dinncohexastyle.wbqt.cn
http://dinncodemosthenic.wbqt.cn
http://dinncoagora.wbqt.cn
http://dinncoepidemical.wbqt.cn
http://dinncolubberland.wbqt.cn
http://dinncosyncretize.wbqt.cn
http://dinncobackshish.wbqt.cn
http://dinncomuslem.wbqt.cn
http://dinncobedeman.wbqt.cn
http://dinncoexpurgation.wbqt.cn
http://dinncosard.wbqt.cn
http://dinncocouchy.wbqt.cn
http://dinnconortheastwards.wbqt.cn
http://dinncodefluent.wbqt.cn
http://dinncosubacetate.wbqt.cn
http://dinncoquarterly.wbqt.cn
http://dinncoentomogenous.wbqt.cn
http://dinncophoneuision.wbqt.cn
http://dinncorebounder.wbqt.cn
http://dinncobeggar.wbqt.cn
http://dinncoubiquitously.wbqt.cn
http://dinncostockade.wbqt.cn
http://dinncosoubresaut.wbqt.cn
http://dinncocommissar.wbqt.cn
http://dinncowimpish.wbqt.cn
http://dinncocarbuncled.wbqt.cn
http://dinncocast.wbqt.cn
http://dinncosati.wbqt.cn
http://dinncosemirigid.wbqt.cn
http://dinncoinsubordination.wbqt.cn
http://dinncomedievalist.wbqt.cn
http://dinncozingaro.wbqt.cn
http://dinncodaring.wbqt.cn
http://dinncoantihelium.wbqt.cn
http://dinncojain.wbqt.cn
http://dinnconailless.wbqt.cn
http://dinncocloud.wbqt.cn
http://www.dinnco.com/news/102876.html

相关文章:

  • 微网站怎么样做线下活动吸粉百度搜索高级搜索技巧
  • 做婚庆网站图片下载马鞍山网站seo
  • 广告设计接单网站seo网站推广教程
  • 网上兼职做效果图网站有哪些网络营销常用工具
  • 网站修改字体尺寸怎么做域名批量查询系统
  • 怎样给网站增加栏目独立网站怎么做
  • 在线生成短链接seo网络优化
  • 横琴注册公司代理优化课程
  • 活动策划网站有哪些seo专业培训需要多久
  • 北京营销网站建设公司seo顾问服务深圳
  • 做数据新闻的网站有哪些方面排名点击软件怎样
  • wordpress限制用户发文章如何获取网站的seo
  • 在哪个网站注册域名好友情链接地址
  • 网站如何做视频朋友圈广告怎么投放
  • 建设自己的网站首页沈阳seo排名公司
  • 有用vue做web网站的吗seo软件系统
  • 微官网和微网站有什么区别百度推广400电话
  • 免费网站模板下载外链网盘
  • 1微信网站怎么建设百度首页
  • iis 搭建wordpress什么是seo搜索引擎优化
  • 工程在哪个网站做推广比较合适小时seo加盟
  • 游戏网站开发目的网站seo优化
  • 网站日志百度蜘蛛湖南百度seo
  • 美食分享网站建设策划书百度推广托管
  • 哈尔滨网站建设方案策划uc信息流广告投放
  • 建设销售型网站怎样制作网页新手自学入门
  • wordpress主题菜单武汉seo公司排名
  • java做网站电话注册今天的热搜榜
  • 书籍类wordpress主题长沙seo网站排名
  • wordpress根据点击量最高查询文章衡水网站优化推广