When a class inherits from another class.The inherited class is called derived class and the super class is called base class
-
First, let's talk about the type of function method:
public, protected and private:
when a class function is declared as public, it can be used outside the class. A private function can only be called inside the class. A protected method can be called in the inherited class. -
Then we will discuss the public inheritance
public inheritance models an "is - a" relationship
if the function method is the base class is declared as virtual, it means this function should be redefine in the derived class.
In inheritance, a virtual destructor is important.
pure virtual function
when a class has a pure virtual function, this class need not be defined. But if you like, you can also define the function in the class.
functions that cannot be inherited:
1.constructor
2.destructor
3.friend function
4.assignment operator
5.private elements cannot be directly called in the child class
-
private inheritance models "has - a" relationship
-
protected inheritance has some different with private inheritance:
When the parent class use protected inheritance to inherit grandparent class, and then the child class use protected inheritance to inherit parent class, the child class can call the protected function in the grandparent class.
characters | public inheritance | protect inheritance | private inheritance |
---|---|---|---|
public members | public members | protected members | private members |
protected members | protected members | protected members | private members |
private members | use base class interfaces | use base class interfaces | use base class interfaces |
- multiple inheritance
网友评论