The significance of designing three different inheritance methods in C++

In C++, inheritance is one of the core concepts of object-oriented programming. Inheritance enables a class (subclass) to inherit properties and methods from another class (parent class). In C++, there are three different ways of inheritance: publicinheritance, protectedinheritance, and privateinheritance. Each inheritance method has its own significance and usage scenarios.

public inheritance

publicInheritance is the most common way of inheritance, and it is also the default way of inheritance. In publicinheritance, subclasses can access publicthe members of the parent class, but cannot access privatethe members of the parent class. The significance of public inheritance is that the subclass can inherit the interface ( publicmember) of the parent class, so that the subclass can use the methods and properties of the parent class more conveniently.

protected inheritance

protectedInheritance is a method of inheritance between publicinheritance and privateinheritance. In protectedinheritance, subclasses can access protectedthe members and publicmembers of the parent class, but cannot access the members of the parent class private. protectedThe significance of inheritance is that it enables subclasses to access protectedthe members of the parent class, so that subclasses can reuse the implementation of the parent class without exposing the implementation details of the parent class.

private inheritance

privateInheritance is the strictest form of inheritance. In privateinheritance, subclasses can access parent class protectedand publicmembers, but cannot access parent class privatemembers. privateThe significance of inheritance is that it can prevent subclasses from directly using the interface of the parent class, but need to implement it through their own methods. This method is generally used to hide implementation details, that is, you do not want subclasses to be able to access the implementation details of the parent class.

The inheritance method in C++ is designed to realize code reuse and inheritance, and at the same time, it can also realize the hiding and protection of code implementation details. Different inheritance methods can play a role in different scenarios.

Suppose we have an Animal class Animal, and three derived classes Dog, Catand Bird, inherit from respectively Animal. Here is a code example using different inheritance methods:

public inheritance

class Animal {
    
    
public:
    void eat() {
    
    
        cout << "Animal is eating" << endl;
    }
};

class Dog : public Animal {
    
    
public:
    void bark() {
    
    
        cout << "Dog is barking" << endl;
    }
};

class Cat : public Animal {
    
    
public:
    void meow() {
    
    
        cout << "Cat is meowing" << endl;
    }
};

class Bird : public Animal {
    
    
public:
    void fly() {
    
    
        cout << "Bird is flying" << endl;
    }
};

Here Dog, Catand Birdare all publicinherited from Animalthe class. This means they can access Animalpublic members of the class (such as eat()functions) as their own public members.

protected inheritance

class Animal {
    
    
protected:
    void eat() {
    
    
        cout << "Animal is eating" << endl;
    }
};

class Dog : protected Animal {
    
    
public:
    void bark() {
    
    
        cout << "Dog is barking" << endl;
        eat();
    }
};

class Cat : protected Animal {
    
    
public:
    void meow() {
    
    
        cout << "Cat is meowing" << endl;
        eat();
    }
};

class Bird : protected Animal {
    
    
public:
    void fly() {
    
    
        cout << "Bird is flying" << endl;
        eat();
    }
};

Here Dog, Catand Birdare all protectedinherited from Animalthe class. This means that they can access Animalprotected members (such as functions) in the class eat()as their own protected members. Since eat()the function is a protected member, it can be accessed directly in both Dog, Catand .Bird

private inheritance

class Animal {
    
    
private:
    void eat() {
    
    
        cout << "Animal is eating" << endl;
    }
};

class Dog : private Animal {
    
    
public:
    void bark() {
    
    
        cout << "Dog is barking" << endl;
        eat();
    }
};

class Cat : private Animal {
    
    
public:
    void meow() {
    
    
        cout << "Cat is meowing" << endl;
        eat();
    }
};

class Bird : private Animal {
    
    
public:
    void fly() {
    
    
        cout << "Bird is flying" << endl;
        eat();
    }
};

Here Dog, Catand Birdare all privateinherited from Animalthe class. This means they cannot access Animalpublic or protected members of the class, but only as their own private members. Since eat()the function is a private member, it cannot be accessed directly in Dog, Catand , but only through the interface exposed in , such as the call of the function.BirdAnimaleat()

In order to be able to explain more clearly, such as the following example:

Suppose we are writing a game, this game has multiple characters, each character has its own attributes (such as blood volume, attack power, etc.) and behavior (such as movement, attack, etc.), and there are different relationships between characters ( Such as team relations, hostile relations, etc.). We use C++ to implement this game.

First, we need a base class to represent the common attributes and behaviors of all characters. We can define a Characterclass called " ", which contains the basic properties and behaviors of the role, as follows:

class Character {
    
    
protected:
    int health;
    int attackPower;
public:
    virtual void move(int x, int y) = 0;
    virtual void attack(Character& target) = 0;
};

In this class, we have used protectedaccess modifiers, which means that subclasses can access these properties and methods, but other classes cannot access them. At the same time, we also use pure virtual functions, which are functions that are only declared but not implemented, which tells the compiler that these functions need to be overridden in subclasses.

Next, we can define some subclasses to represent different types of roles. For example, we can define a " Warrior" class to represent a fighter, as follows:

class Warrior : public Character {
    
    
public:
    Warrior() {
    
    
        health = 100;
        attackPower = 10;
    }
    void move(int x, int y) override {
    
    
        // 移动逻辑
    }
    void attack(Character& target) override {
    
    
        // 攻击逻辑
    }
};

In this class, we have used publicinheritance, which means that the child class inherits all the properties and methods of the parent class, and these properties and methods are unique publicand can be accessed by other classes. At the same time, we rewrite the pure virtual function in the base class to realize the specific behavior of the fighter.

In addition to fighters, we can also define other types of roles, such as mages, archers, and so on. These roles are all inherited from the base class " Character", and different inheritance methods can be used according to different requirements.

For example, we can define a " Wizard" class to represent a mage as follows:

class Wizard : protected Character {
    
    
public:
    Wizard() {
    
    
        health = 50;
        attackPower = 20;
    }
    void move(int x, int y) override {
    
    
        // 移动逻辑
    }
    void attack(Character& target) override {
    
    
        // 攻击逻辑
    }
};

In this class, we have used protectedinheritance, which means that the child class inherits all the properties and methods of the parent class, but these properties and methods are protectedprivate and cannot be accessed by other classes.

privateInheritance, you can implement it yourself according to the above explanation, and do it yourself, which will help you understand.

Guess you like

Origin blog.csdn.net/shanniuliqingming/article/details/130024734