Cpp 知识3

发布于:2025-06-14 ⋅ 阅读:(21) ⋅ 点赞:(0)

权限对继承的影响

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

#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)
#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()

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

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关键字

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

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

class Bike : public Vehicle{

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

    }


};
#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关键字

多重继承

#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
#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();
#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;
}
结果:

抽象类:

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

抽象类 也是支持多态的

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

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