Cpp 知识3

权限对继承的影响

当使用protected继承时,所使用的基类成员时为public会换成protected 再去看下面的protected权限 类内部是可以访问的 但是同一类的对象是不能访问的 同理

cpp 复制代码
#include <iostream>

using namespace std;

//基类,父类
class Vehicle{//交通工具,车,抽象的概念

public:
    string type;
    string contry;
    string color;
    double price;
    int numOfWheel;
    
protected:
    int protectedData;
private:
    int privateData;
    
public:
    void run();
    void stop();

};


//派生类, 子类
class Roadster : protected Vehicle { //跑车,也是抽象,比父类感觉上范围缩小点   子类:public 父类  (因为或默认private)

public:
    
    void openTopped(){
        type="wefrwr";  //在这里type是protected  
    }
    void pdrifting();

};
int main()
{
    Roadster ftype;
    ftype.type ="捷豹Ftype";     //这里type是protected 不能在同一类对象中访问
    return 0;
}

基类/派生类构造函数

复制代码
Roadster(string contry,double price,int state) :Vehicle(contry,price)
cpp 复制代码
#include <iostream>

using namespace std;

//基类,父类
class Vehicle{ //交通工具,车,抽象的概念
public:
    string contry;
    double price;

    Vehicle(string contry,double price){
        cout << "基类的构造函数被调用" << endl;
        this->contry =contry;
        this->price = price;

    }


};

//派生类,子类
class Roadster : public Vehicle{

public:
    int stateOfTop;
    Roadster(string contry,double price,int state) :Vehicle(contry,price){
        cout << "派生类的构造函数被调用" << endl;
        stateOfTop =state;

    }

};

int main()
{
    Roadster FTYPE("法国",70,0);
    return 0;
}

虚函数

在C++中,virtual和override 关键字用于支持多态,尤其是在涉及继承和方法重写下的情况下

Virtual关键字

使用场景:在基类中声明虚函数

virtual void func()

目的:允许派生类重写该函数,实现多态,

cpp 复制代码
class Vehicle{ //交通工具,车,抽象的概念
public:
    string contry;
    double price;
    Vehicle(){};
    Vehicle(string contry,double price){
        cout << "基类的构造函数被调用" << endl;
        this->contry =contry;
        this->price = price;

    };
    //基类中声明了一个虚函数
    virtual void run(){

        cout << "车跑起来了"  << endl;
    }


};

override关键字

使用场景:在基类中重写虚函数

目的:明确指示函数意图重写基类的虚函数

cpp 复制代码
class Bike : public Vehicle{

public:
    void run() override{
        cout << "脚踩自行车" << endl ;

    }


};
cpp 复制代码
#include <iostream>

using namespace std;

//基类,父类
class Vehicle{ //交通工具,车,抽象的概念
public:
    string contry;
    double price;
    Vehicle(){};
    Vehicle(string contry,double price){
        cout << "基类的构造函数被调用" << endl;
        this->contry =contry;
        this->price = price;

    };
    //基类中声明了一个虚函数
    virtual void run(){

        cout << "车跑起来了"  << endl;
    }


};

//派生类,子类
class Bike : public Vehicle{

public:
    void run() override{
        cout << "脚踩自行车" << endl ;

    }


};


int main()
{
    Bike b;
    b.run();
    return 0;
}

override仅应用于派生类中重写基类的虚函数

如果类中有虚函数,通常应该将析构函数也声明为虚的

一旦在基类中声明为虚函数,该函数在所有派生类中自动成为虚函数,无论是否使用virtual关键字

多重继承

cpp 复制代码
#include <iostream>

using namespace std;

class classA{
public:
    void displayA(){

        cout << "Displaying ClassA"  << endl;
    }
    void testFunc(){

        cout << "testFunc ClassA" <<endl;
    }

};

class classB{
public:
    void displayB(){
        cout << "Displaying classB" << endl;
    }
    void testFunc(){

        cout << "testFunc ClassB" <<endl;
    }
};

class Derived : public classA,public classB{
public:
    void display(){
        displayA();
        displayB();
        classA::testFunc(); //如果在两个类中有相同的函数 那么应该表明是哪一个类
    }


};

int main()
{
   Derived obj;
   obj.displayA();
   obj.displayB();
   obj.display();
    return 0;
}

菱形继承

中间的必须采用虚拟继承才可以

复制代码
class Derived1: virtual public Base
复制代码
class Derived2: virtual public Base
cpp 复制代码
#include <iostream>

using namespace std;

class Base{
public:
    int data;
    Base(int data){
        this->data=data;
    }
    void pritInfo(){
        cout << data << endl;
    }

};

class Derived1: virtual public Base {
  //继承自 Base
public:
    Derived1(int data) : Base(data){

    }

};
class Derived2: virtual public Base {
  //继承自 Base
public:
    Derived2(int data) : Base(data){

    }

};


class FinalDerived : public  Derived1,public Derived2{
    //继承自 Derived1 和 Derived2
public:
    FinalDerived(int data) : Base(data),Derived1(data),Derived2(data){
    }

};



int main()
{
    FinalDerived final(10);
    final.pritInfo();
    return 0;
}

注意:

复制代码
FinalDerived(int data) : Base(data),Derived1(data),Derived2(data)

多态

复制代码
RemoteCon *remoteCon =new TvRemoCon; //多态  向下兼容   
remoteCon->openUtils();
cpp 复制代码
#include <iostream>

using namespace std;

class RemoteCon{
public:
    virtual void openUtils(){

      cout <<"遥控器的开被按下" << endl;
    };

};


class TvRemoCon : public RemoteCon{

public:
    void openUtils() override{

        cout << "电视遥控器的开被按下" << endl;
    }

};


class RoundspeakerCon : public RemoteCon{

public:
    void openUtils() override{

        cout << "音响遥控器的开被按下" << endl;
    }

};


class lightCon: public RemoteCon{

public:
    void openUtils() override{
        cout << "灯光遥控器的开被按下" << endl;
    }
};


void test(RemoteCon& r)
{

    r.openUtils();

}
int main()
{
    RemoteCon *remoteCon =new TvRemoCon; //多态  向下兼容
    remoteCon->openUtils();

    RemoteCon *remoteCon2 =new RoundspeakerCon; //多态
    remoteCon2->openUtils();

    RemoteCon *remoteCon3 =new lightCon; //多态
    remoteCon3->openUtils();

    TvRemoCon tvRemote;
    test(tvRemote);
    return 0;
}
复制代码
结果:

抽象类:

抽象类,不支持被实例化 即派生类不把所有的基类中函数重新定义 那么就会报错

抽象类 也是支持多态的

cpp 复制代码
#include <iostream>

using namespace std;

class Teacher{

public:
    string name;
    string school;
    string major;

    virtual void goInClass()=0;
    virtual void startTeaching() =0;
    virtual void afterTeaching() =0;


};
class EnglishTeacher : public Teacher{

public:
    void goInClass() override{

        cout << "英语老师开始进入教室" << endl;
    }
    void startTeaching() override{
        cout << "英语老师开始教学" << endl ;
    }
    void afterTeaching() override{


    }

};


int main()
{
    //Teacher t;  //抽象类,不支持被实例化 即派生类不把所有的基类中函数重新定义 那么就会报错
    EnglishTeacher e;
    e.goInClass();

    //抽象类 也是支持多态的
    Teacher *s = new EnglishTeacher();
    s->startTeaching();
    return 0;
}

接口(针对于动作和行为) 都有基类 派生类 纯虚函数

相关推荐
枣伊吕波5 分钟前
第十三节:第七部分:Stream流的中间方法、Stream流的终结方法
java·开发语言
一点也不想取名18 分钟前
解决 Java 与 JavaScript 之间特殊字符传递问题的终极方案
java·开发语言·javascript
im_AMBER35 分钟前
java复习 11
java·开发语言
Cai junhao44 分钟前
【Qt】工具介绍和信号与槽机制
开发语言·c++·qt·qt6.3
黑牛先生1 小时前
【Qt】信号与槽
开发语言·qt
橙子199110162 小时前
Kotlin 中的 Object
android·开发语言·kotlin
callJJ2 小时前
从 0 开始理解 Spring 的核心思想 —— IoC 和 DI(1)
java·开发语言·spring boot·后端·spring·restful·ioc di
Python开发吖3 小时前
【已解决】python的kafka-python包连接kafka报认证失败
开发语言·python·kafka
@老蝴6 小时前
C语言 — 通讯录模拟实现
c语言·开发语言·算法
L-ololois6 小时前
【AI】模型vs算法(以自动驾驶为例)
人工智能·算法·自动驾驶