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;
}

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

相关推荐
sharemylove5 小时前
Java集合框架全面详解
java·开发语言·经验分享·笔记·学习
IvanCodes8 小时前
模型如何自我改进?拆解 RSI 的模型权重与 Harness 两条路线
人工智能·算法·ai·论文·agent·rsi
罗西的思考8 小时前
【轻量化复刻大模型 / KIMI K3】MXFP4 / MXFP8 量化感知训练(QAT)& 演示
人工智能·算法·机器学习
郝学胜-神的一滴9 小时前
Effective Python 条款43:自定义容器类
开发语言·windows·python·pandas·scikit-learn·pyqt
好焊孙辉10 小时前
一条产线省下 30 个电机位:Z 轴和 R 轴,该合成一个了
开发语言·r语言
布莱克60510 小时前
Swift 中的逃逸闭包与非逃逸闭包:@escaping 到底在做什么
开发语言·ios·swift·闭包·逃逸
郝学胜-神的一滴10 小时前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
旖旎夜光11 小时前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法
吃我一窝jio11 小时前
Java IO流核心分类与应用
java·开发语言·笔记
阿enna11 小时前
检测指标入门:IoU、Precision、Recall与mAP
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·cnn