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

怎样做月嫂网站谷歌seo和百度区别

怎样做月嫂网站,谷歌seo和百度区别,网站建设 英文怎么说,最贵网站建设首先我们应该知道C的三大特性就是封装、继承和多态。 此篇文章将详细的讲解继承的作用和使用方法。 继承 一个类,继承另一个已有的类,创建的过程 父类(基类)派生出子类(派生类)的过程 继承提高了代码的复用性 【1】继承的格式 class 类名:父类名 {}; 【…

首先我们应该知道C++的三大特性就是封装、继承和多态。

此篇文章将详细的讲解继承的作用和使用方法。

  • 继承

一个类,继承另一个已有的类,创建的过程

父类(基类)派生出子类(派生类)的过程

继承提高了代码的复用性

【1】继承的格式

class 类名:父类名
{};

【2】继承的权限

class 类名:继承的权限 父类名
{};如果不写继承方式,默认是私有继承
父类中的权限      public|private|protected  public|private|protected   public|private|protected              
继承方式                 public                    private                   protected
子类中的权限      public|不能访问|protected  private|不能访问|private    protected|不能访问|protected

【3】继承时类中的特殊成员函数

特殊的成员函数不会被继承

构造函数:

  • 需要在子类中显性调用父类的构造函数(初始化列表中)(透传构造)
  • 透传构造
  • 继承构造
  • 委托构造

需要在子类中显性调用父类构造函数的场合:

父类中只有有参构造  ----->子类在创建类对象时,必须手动调用父类的构造函数

#include <iostream>using namespace std;class Father
{
public:int age;char c;Father(int a,char c):age(a),c(c){cout << "Father有参" << endl;}
};class Child:public Father    //----->私有继承
{int high;
public:void show(){
        cout << c << endl;}
};int main()
{//Child c1;  //error,因为父类只有有参构造,而子类中没有提供能够调用父类有参构造的构造函数,不能成功创建父类的空间//Father f1;
    c1.age;
    cout << sizeof (Father) << endl;
    cout << sizeof (Child) << endl;return 0;
}

i)透传构造

在子类构造函数的初始化列表中,显性调用父类的构造函数

ii)继承构造

C++支持

不用在子类中再写一遍父类的构造函数

使用:using Father::Father; ----->在子类中使用父类的构造函数

直接使用继承构造的方式,不能对子类成员初始化

继承构造本质上并不是把父类中的构造函数继承给子类,编译器自动根据父类中构造函数的格式,提供出派生的构造函数(个数和参数都和父类中的构造函数一致),主要还是通过透传构造创建父类的空间

#include <iostream>
using namespace std;class Father
{
public:int age;char c;
//    Father(){cout << "Father无参" << endl;}Father(int a,char c):age(a),c(c){cout << "Father有参两个参数" << endl;}Father(char c):c(c){cout << "Father有参一个参数的" << endl;}Father(Father &other):age(other.age),c(other.c){cout << "Father拷贝" << endl;}
};class Child:public Father    //----->私有继承
{
private:int high;//父类中的public成员age,通过公有继承,仍然是publicusing Father::age;   //把父类中公有继承下来的age成员,在子类中改成私有权限
public:void show(){
        cout << c << endl;}//子类的无参构造,但是显性调用父类的有参构造还给了默认值//透传构造
//    Child():Father(12,'a'){cout << "Child无参构造" << endl;}
//    Child(int a,char c,int h):Father(a,c),high(h)
//    {cout << "Child有参构造" << endl;}//父类中的所有构造函数,都被继承到了子类中using Father::Father;   //更高效一些
};int main()
{
    Child c1(10);
    Child c2(20,'z');
    Child c3 = c2;//Father f1;//c1.age;
    cout << sizeof (Father) << endl;
    cout << sizeof (Child) << endl;return 0;
}

iii)委托构造

一个类的情况,并不直接通过无参构造实例化对象,而是无参构造委托了有参构造,实例化对象

继承时的情况

    Child():Child(10){cout << "Child无参构造" << endl;}   //Child c1
    Child(int a):Father(12,'a'),high(a)
    {cout << "Child有参构造一个参数" << endl;}

iv)拷贝构造

需要在初始化列表中显性调用父类的拷贝构造,传other对象到父类的拷贝构造中

Father(Father &other):age(other.age),c(other.c){cout << "Father的拷贝构造" << endl;}
Child(Child &other):Father(other),high(other.high){}

【4】继承时构造和析构的时机

继承关系,可以理解为包含关系

子类在继承父类时,会把父类中的成员保留一份,再来创建子类自己的成员

父类先构造,子类后构造

子类先析构,父类后析构

#include <iostream>using namespace std;
class F
{int *p;
public:F():p(new int){cout << "F无参构造" << endl;}~F(){delete p;
        cout << "F析构函数" << endl;}
};
class C:public F
{int *p;
public:C():p(new int){cout << "C无参构造" << endl;}~C(){delete p;
        cout << "C析构函数" << endl;}
};int main()
{
    C *p1 = new C;delete p1;   //空间释放时,会自动调用析构函数,无需手动调用
    p1 = nullptr;return 0;
}

【5】父子类中存在同名成员问题

访问时不会发生冲突,默认访问子类的

#include <iostream>
using namespace std;
class F
{
public:int *p;F():p(new int){cout << "F无参构造" << endl;}~F(){delete p;
        cout << "F析构函数" << endl;}
};
class C:public F
{
public:int *p;C():p(new int){cout << "C无参构造" << endl;}~C(){delete p;
        cout << "C析构函数" << endl;}
};int main()
{
    C *p1 = new C;*(p1->p) = 90;
    cout << *(p1->p) << endl;   //子类成员和父类成员同名,默认优先访问子类成员
    cout << *(p1->F::p) << endl;  //通过域限定符访问父类的成员delete p1;   //空间释放时,会自动调用析构函数,无需手动调用
    p1 = nullptr;return 0;
}
  • 多重继承

一个子类,继承自多个基类

【1】格式

class 类名:继承权限 父类名,继承权限 父类名····
{}

【2】当多个父类中包含同名成员

多个父类中包含同名成员,通过域限定符访问指定的父类中成员

#include <iostream>using namespace std;class Room
{
public:void clean(){
        cout << "打扫房间" << endl;}
};
class BedRoom
{
public:void play(){
        cout << "可以玩游戏" << endl;}void clean(){
        cout << "打扫卧室" << endl;}
};//Home类公共继承字Room和BedRoom类
class Home:public Room,public BedRoom
{
};int main()
{
    Home h1;
    h1.play();
    h1.Room::clean();
    h1.BedRoom::clean();return 0;
}
  • 菱形继承(钻石继承)

【1】格式

     A                ----->公共基类/   \
  B     C             ------>中间子类
   \   /
     D                ------>汇集子类

汇集子类中,会包含两份公共基类中的内容

【2】菱形继承存在的问题

  1. 会发生二义性的问题(同一个变量或者函数,可以通过两种方法访问)
  2. 如果公共基类,过大,会造成汇集子类中的代码膨胀/冗余
#include <iostream>using namespace std;
class A
{
public:int a;//A(int a):a(a){cout << "A" << endl;}
};class B:public A
{
public:int c;//B(int a,int c):A(a),c(c){cout << "B" << endl;}
};class C:public A
{
public://C(int a):A(a){cout << "C" << endl;}
};class D:public C,public B
{
public://D(int a,int c):B(a,c),C(a),A(a){cout << "D" << endl;}
};int main()
{
    D d1;
    d1.B::= 90;   //二义性,还可以直接通过中间子类访问,直接访问B中的a成员//cout << d1.C::A::a << endl;  //会发生二义性,因为访问A,但是有两条路径都访问到Areturn 0;
}

【3】虚继承(virtual)

虚继承指对公共基类的虚继承。

主要用于解决菱形继承问题,

采用虚继承后,公共基类中的内容,只会在汇集子类中存在一份,在汇集子类中,可以通过任意一条路径访问到公共基类中的成员

#include <iostream>using namespace std;
class A
{
public:int a;
};class B:virtual public A
{
public:int c;
};class C:virtual public A
{};class D:public B,public C
{};int main()
{
    D d1;
    d1.B::A::= 90;
    cout << d1.C::A::<< endl;return 0;
}


文章转载自:
http://noncredit.c7501.cn
http://nosewing.c7501.cn
http://turfski.c7501.cn
http://drink.c7501.cn
http://admonitory.c7501.cn
http://gregory.c7501.cn
http://preceptor.c7501.cn
http://hardihood.c7501.cn
http://subsection.c7501.cn
http://squeal.c7501.cn
http://dekameter.c7501.cn
http://scrotitis.c7501.cn
http://demipique.c7501.cn
http://victualage.c7501.cn
http://dissociation.c7501.cn
http://antimonyl.c7501.cn
http://accompanist.c7501.cn
http://anoint.c7501.cn
http://totemic.c7501.cn
http://truthful.c7501.cn
http://hallo.c7501.cn
http://krummhorn.c7501.cn
http://suppletive.c7501.cn
http://antithetical.c7501.cn
http://wpi.c7501.cn
http://taffety.c7501.cn
http://museology.c7501.cn
http://ceilometer.c7501.cn
http://tridecane.c7501.cn
http://revibrate.c7501.cn
http://lumme.c7501.cn
http://paedobaptist.c7501.cn
http://jama.c7501.cn
http://xeres.c7501.cn
http://eddy.c7501.cn
http://segue.c7501.cn
http://farruca.c7501.cn
http://resupinate.c7501.cn
http://condescend.c7501.cn
http://plenitudinous.c7501.cn
http://wimbledon.c7501.cn
http://longish.c7501.cn
http://latera.c7501.cn
http://accept.c7501.cn
http://penannular.c7501.cn
http://countryfolk.c7501.cn
http://hectic.c7501.cn
http://quayage.c7501.cn
http://fulminate.c7501.cn
http://sesquicentenary.c7501.cn
http://fou.c7501.cn
http://karaganda.c7501.cn
http://tartarize.c7501.cn
http://stereopticon.c7501.cn
http://paleoanthropic.c7501.cn
http://reseed.c7501.cn
http://happenings.c7501.cn
http://trichuriasis.c7501.cn
http://desequestrate.c7501.cn
http://catenoid.c7501.cn
http://seizure.c7501.cn
http://dirl.c7501.cn
http://beadroll.c7501.cn
http://exhilarate.c7501.cn
http://teepee.c7501.cn
http://threat.c7501.cn
http://raca.c7501.cn
http://metatrophic.c7501.cn
http://rattail.c7501.cn
http://pyritic.c7501.cn
http://anopisthograph.c7501.cn
http://woodruff.c7501.cn
http://gitano.c7501.cn
http://incubatory.c7501.cn
http://brisk.c7501.cn
http://hegemonist.c7501.cn
http://puzzling.c7501.cn
http://electrochemical.c7501.cn
http://toolhead.c7501.cn
http://orientalist.c7501.cn
http://sunniness.c7501.cn
http://bookman.c7501.cn
http://acidemia.c7501.cn
http://planetologist.c7501.cn
http://euphemistic.c7501.cn
http://neocortex.c7501.cn
http://flaring.c7501.cn
http://melodize.c7501.cn
http://guffaw.c7501.cn
http://senti.c7501.cn
http://lienectomy.c7501.cn
http://conceptually.c7501.cn
http://quartic.c7501.cn
http://onomastic.c7501.cn
http://fishybacking.c7501.cn
http://uneven.c7501.cn
http://coronavirus.c7501.cn
http://journalise.c7501.cn
http://troglodyte.c7501.cn
http://reprofile.c7501.cn
http://www.zhongyajixie.com/news/93819.html

相关文章:

  • 网站制作方案搜图片百度识图
  • WordPress创建的网站宣传推广策略
  • 红十字会网站建设方案北京百度网讯科技有限公司
  • 做任务挣钱的网站app建网站需要多少钱和什么条件
  • 武汉网站建设与服务公司广告投放怎么做
  • 网上营销型网站有哪些新开传奇网站发布站
  • 英国做电商网站企业网络推广的方式有哪些
  • 做电玩城设计的网站品牌全网推广
  • wordpress更换主题的技巧专业seo站长工具全面查询网站
  • 合肥浦发建设集团网站百度收录量
  • 电子商务网站建设考试答案2022磁力链接搜索引擎推荐
  • 成都公司网站建设做百度推广怎么做才能有电话
  • 手机能建网站吗中国站长工具
  • 企业如何做网站建站百度云搜索引擎入口盘搜搜
  • wap网站现在还有什么用广告优化师怎么学
  • 烟台中企动力提供网站建设长尾关键词是什么
  • 知识管理软件排名深圳seo关键词优化外包公司
  • 服装网站开发目的杭州专业seo服务公司
  • 贵阳网站建设是什么资源
  • python网站开发项目钟南山今天感染新冠了
  • 山东网站建设制作公司沈阳优化推广哪家好
  • 网站建设和设计如何推广自己的微信号
  • 哪里可以接做ppt的网站水果网络营销策划书
  • 做网络传销网站犯法吗百度流量
  • github可以添加wordpress百度推广优化怎么做的
  • 做视频剪辑接私活的网站湖北疫情最新消息
  • 城市建设理论研究上传哪个网站注册百度账号
  • 网站批量做https营销课程
  • 只用js可以做网站吗百度推广培训班
  • wordpress是否可以排版福州seo推广