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: public
inheritance, protected
inheritance, and private
inheritance. Each inheritance method has its own significance and usage scenarios.
public inheritance
public
Inheritance is the most common way of inheritance, and it is also the default way of inheritance. In public
inheritance, subclasses can access public
the members of the parent class, but cannot access private
the members of the parent class. The significance of public inheritance is that the subclass can inherit the interface ( public
member) of the parent class, so that the subclass can use the methods and properties of the parent class more conveniently.
protected inheritance
protected
Inheritance is a method of inheritance between public
inheritance and private
inheritance. In protected
inheritance, subclasses can access protected
the members and public
members of the parent class, but cannot access the members of the parent class private
. protected
The significance of inheritance is that it enables subclasses to access protected
the 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
private
Inheritance is the strictest form of inheritance. In private
inheritance, subclasses can access parent class protected
and public
members, but cannot access parent class private
members. private
The 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
, Cat
and 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
, Cat
and Bird
are all public
inherited from Animal
the class. This means they can access Animal
public 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
, Cat
and Bird
are all protected
inherited from Animal
the class. This means that they can access Animal
protected 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
, Cat
and .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
, Cat
and Bird
are all private
inherited from Animal
the class. This means they cannot access Animal
public 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
, Cat
and , but only through the interface exposed in , such as the call of the function.Bird
Animal
eat()
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 Character
class 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 protected
access 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 public
inheritance, which means that the child class inherits all the properties and methods of the parent class, and these properties and methods are unique public
and 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 protected
inheritance, which means that the child class inherits all the properties and methods of the parent class, but these properties and methods are protected
private and cannot be accessed by other classes.
private
Inheritance, you can implement it yourself according to the above explanation, and do it yourself, which will help you understand.