Simple inheritance
Inheritance is a feature of C ++. It can create a class that inherits a known class. The derived class automatically has a member of the base class. The inheritance form is as follows:
Class derived_class_name: public base_class_name
{/*...*/};
Public indicates the inheritance method, which can be replaced by protected and private. If the inheritance method is omitted, private inheritance is used for 'class' and public inheritance is used for 'struct.
The sample code is as follows:
// derived classes
#include <iostream>
using namespace std;
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};
class CRectangle: public CPolygon {
public:
int area ()
{ return (width * height); }
};
class CTriangle: public CPolygon {
public:
int area ()
{ return (width * height / 2); }
};
int main () {
CRectangle rect;
CTriangle trgl;
rect.set_values (4,5);
trgl.set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}
The protected modifier is similar to private. The only difference is that when a class inherits from another class, the Members in the derived class can access the protected member inherited from the base class, but not its private member.
Based on the access method, we can summarize the following access types:
| Access |
Public |
Protected |
Private |
| Members of the same class |
Yes |
Yes |
Yes |
| Members of derived classes |
Yes |
Yes |
No |
| Not members |
Yes |
No |
No |
Three inheritance Methods
The inheritance methods mentioned earlier include public, private, and protected ).
1. public inheritance)
Public inheritance is characterized by the original state of both the public members of the base class and the protected members as the members of the derived class, while the private members of the base class are still private, it cannot be accessed by the subclass of this derived class.
2. private)
Private inheritance features that the public and protected members of the base class are both private members of the derived class and cannot be accessed by the subclass of the derived class.
3. protected)
The protection inheritance feature is that all the public and protected members of the base class become the protected members of the derived class and can only be accessed by the member functions or friends of the derived class, the Private Members of the base class are still private.
Inherited Member
In principle, the derived class does not inherit the following members of the base class:
1. Construction and destructor of the base class
2. = () member functions of the base class
3. Friends of the base class
Although the constructor and destructor of the base class are not inherited, the default constructor (that is, it does not contain the constructor parameters) and its destructor.
If the base class does not have a default constructor, when an overloaded constructor is called to create a new derived object, you can define it in the constructor of each derived class:
Derived_constructor_name (parameters): base_constructor_name (parameters ){...}
For example:
// constructors and derived classes
#include <iostream>
using namespace std;
class mother {
public:
mother ()
{ cout << "mother: no parameters\n"; }
mother (int a)
{ cout << "mother: int parameter\n"; }
};
class daughter : public mother {
public:
daughter (int a)
{ cout << "daughter: int parameter\n\n"; }
};
class son : public mother {
public:
son (int a) : mother (a)
{ cout << "son: int parameter\n\n"; }
};
int main () {
daughter cynthia (0);
son daniel(0);
return 0;
}
Multi-Inheritance
C ++ supports one class to inherit multiple classes, such
class CRectangle: public CPolygon, public COutput;
class CTriangle: public CPolygon, public COutput;
The complete example is as follows:
// multiple inheritance
#include <iostream>
using namespace std;
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};
class COutput {
public:
void output (int i);
};
void COutput::output (int i) {
cout << i << endl;
}
class CRectangle: public CPolygon, public COutput {
public:
int area ()
{ return (width * height); }
};
class CTriangle: public CPolygon, public COutput {
public:
int area ()
{ return (width * height / 2); }
};
int main () {
CRectangle rect;
CTriangle trgl;
rect.set_values (4,5);
trgl.set_values (4,5);
rect.output (rect.area());
trgl.output (trgl.area());
return 0;
}