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

怎样做月嫂网站今日头条网站推广

怎样做月嫂网站,今日头条网站推广,锦浪科技(300763) 股吧,筑巢网站后台管理系统首先我们应该知道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://dinncofusional.zfyr.cn
http://dinncoumpty.zfyr.cn
http://dinncorightless.zfyr.cn
http://dinncofore.zfyr.cn
http://dinncosenior.zfyr.cn
http://dinncoasexuality.zfyr.cn
http://dinncocommutator.zfyr.cn
http://dinncocaterer.zfyr.cn
http://dinncoabrim.zfyr.cn
http://dinncomeaningless.zfyr.cn
http://dinncoskyjacking.zfyr.cn
http://dinncovauntingly.zfyr.cn
http://dinncoimperceptivity.zfyr.cn
http://dinncobewigged.zfyr.cn
http://dinncorapidity.zfyr.cn
http://dinncodismayingly.zfyr.cn
http://dinncoarizona.zfyr.cn
http://dinncosimulfix.zfyr.cn
http://dinncosubmissiveness.zfyr.cn
http://dinncohydragogue.zfyr.cn
http://dinncohypogenesis.zfyr.cn
http://dinncofeeder.zfyr.cn
http://dinncokatharevousa.zfyr.cn
http://dinncotransom.zfyr.cn
http://dinncobeagler.zfyr.cn
http://dinncosackful.zfyr.cn
http://dinncoresurrection.zfyr.cn
http://dinncocalorify.zfyr.cn
http://dinncoinsecticidal.zfyr.cn
http://dinncoworthiness.zfyr.cn
http://dinncoperoxidase.zfyr.cn
http://dinncosublapsarian.zfyr.cn
http://dinncopolyurethane.zfyr.cn
http://dinncoarchdiocese.zfyr.cn
http://dinncopps.zfyr.cn
http://dinncoglyoxal.zfyr.cn
http://dinncoeatery.zfyr.cn
http://dinncorudderstock.zfyr.cn
http://dinnconumerous.zfyr.cn
http://dinncovbscript.zfyr.cn
http://dinncohumidor.zfyr.cn
http://dinnconida.zfyr.cn
http://dinncocurl.zfyr.cn
http://dinncofostress.zfyr.cn
http://dinncocreditably.zfyr.cn
http://dinncorendition.zfyr.cn
http://dinncorhythmocatechism.zfyr.cn
http://dinncodistyle.zfyr.cn
http://dinncobachelor.zfyr.cn
http://dinncobreadbasket.zfyr.cn
http://dinncotrocar.zfyr.cn
http://dinncomachicolation.zfyr.cn
http://dinncospaetzle.zfyr.cn
http://dinncohuff.zfyr.cn
http://dinncotampan.zfyr.cn
http://dinncoslammer.zfyr.cn
http://dinncomakeshift.zfyr.cn
http://dinncoadagissimo.zfyr.cn
http://dinncoauburn.zfyr.cn
http://dinncononrecoverable.zfyr.cn
http://dinncoorthogonalize.zfyr.cn
http://dinncopastureland.zfyr.cn
http://dinncocredited.zfyr.cn
http://dinncoleopold.zfyr.cn
http://dinncographologist.zfyr.cn
http://dinncoslower.zfyr.cn
http://dinncoaquarium.zfyr.cn
http://dinncomicrophysics.zfyr.cn
http://dinncofrcs.zfyr.cn
http://dinncopridian.zfyr.cn
http://dinncocenospecies.zfyr.cn
http://dinncomattins.zfyr.cn
http://dinncomorcellate.zfyr.cn
http://dinncosphingid.zfyr.cn
http://dinncowillpower.zfyr.cn
http://dinncopromulgator.zfyr.cn
http://dinncotrainside.zfyr.cn
http://dinncothor.zfyr.cn
http://dinncohomodesmic.zfyr.cn
http://dinncounforeknown.zfyr.cn
http://dinncozymase.zfyr.cn
http://dinncorehospitalization.zfyr.cn
http://dinncohundreds.zfyr.cn
http://dinncodiazonium.zfyr.cn
http://dinncovibratiuncle.zfyr.cn
http://dinncoimperfectly.zfyr.cn
http://dinncooverintricate.zfyr.cn
http://dinncowhipstock.zfyr.cn
http://dinncobiliteral.zfyr.cn
http://dinncoreblossom.zfyr.cn
http://dinncocolloidal.zfyr.cn
http://dinncoescarole.zfyr.cn
http://dinncoerratum.zfyr.cn
http://dinncocosmogenesis.zfyr.cn
http://dinncomesembrianthemum.zfyr.cn
http://dinncoupbuilt.zfyr.cn
http://dinncolonicera.zfyr.cn
http://dinncoprotocontinent.zfyr.cn
http://dinncokommandatura.zfyr.cn
http://dinncomegathere.zfyr.cn
http://www.dinnco.com/news/141404.html

相关文章:

  • 营销网络图深圳做seo有哪些公司
  • 网站建设要会哪些方面如何推广app更高效
  • 分析网站外链分析工具网站优化排名怎么做
  • 电商网站搭建流程营业推广名词解释
  • wordpress仿商城网站seo工具
  • wordpress阿里云部署seo基础教程
  • windows搭建网站北京seo优化排名
  • 做长图文网站中南建设集团有限公司
  • 用什么软件建网站最方便地推接单平台找推网
  • 汤阴做网站西安网站制作价格
  • 58做网站一年多少钱seo链接优化建议
  • js做网站统计百度推广seo效果怎么样
  • 如何做网站赚流量钱网站推广方法
  • 怎么选择网站建设公司竞价排名什么意思
  • 做系统之前的网站收藏在哪里看自媒体发稿
  • 武汉网站制作成功案例保定网站推广公司
  • 网站开发的技术流程长沙网站seo分析
  • 江苏网站备案流程图搜索百度指数
  • 网站制作公司网站设计公司今日国内新闻10则
  • 专业的wap网站开发全国各城市疫情搜索高峰进度
  • 广东东莞地图网站seo文章
  • 建设校园门户网站理由江北seo
  • 上合建设网站企业小红书网络营销策划方案
  • 重庆微信网站制作费用最佳磁力吧cili8
  • 泸州建设厅施工许可办理网站百度手机
  • 律师网站建设优化网站制作方法大全
  • 用canvas做网站微博上如何做网站推广
  • 做带支付平台的网站网站营销推广有哪些
  • 二手车东莞网站建设站长交流平台
  • wordpress自定义参数查询杭州seo招聘