My fucking in c + + book review – Understanding inheritance and composition!
|
Get your FREE Diabetes Recipes eBook! |
1, the derived class inherits the default base class member function, that is, without re-define the premise, yf (), and yX:: f () calls the same function
The combination of class members of class objects must be carried out through the function call, a class constructor calls the base class of the order is then combined class, the final will be the class constructor.
2 If you re-define the base class function, then the base class of the same name as the function of all the auto-hide. The so-called “all” because, in the base class may have more than one overloaded function the same name, they are all hidden
Health coverage is a class of functions assigned to cover the base class function
Function with the same name;
Parameters of the same;
Virtual base class function must have keywords;
Different ranges (derived class and base class).
Hide Health class is assigned the same name blocked function of the same base class
1, the function name the same, but the parameters are different, this time regardless of whether the Virtual keyword base class, base class function will be hidden.
2, the same function name, parameters are the same, but the base class of Virtual Keyword (there is the cover), the base class function will be hidden
The so-called “hidden” does not mean you can not call, but said that when the call yf () is called when the definition of Y in the new f (), to call the base class will have an explicit description of f yX:: f (), were type description only.
3 All constructor, destructor, operator = is not automatically inherited, but the compiler will automatically generate a default constructor, copy constructor, operator =, and correctly call the corresponding base class function, they work well
If you want to define parameters for the sub-class constructor, you must also define the default constructor for subclasses. If the custom of the copy constructor, you must also customize the default constructor
In the sub-class copy constructor, if you want to call the base class copy constructor, you must explicitly call the initialization list, or the compiler automatically calls the default constructor
Operator = in the child class, if you want to call the base class’s operator =, you must explicitly call the function body, or the compiler to do nothing.
# Include
using namespace std;
/ / Inheritance!
class A
{
public:
A (int i = 0) {cout <<“A is doing” <
{
cout <<“A cout” <
};
class B
{
public:
B (int i = 0) {cout <<“B is doing” <
class C: public A / / derived class
{
B b; / / composite classes
public:
/ / Initialization syntax: For the base class constructor is called using the class name; for members of the class, call the constructor using the object name
C (int i = 0): b (i), A (i) {cout <<“C is doing” <
/ / Call this place B if you do not specify a copy constructor, the compiler will call B, the default constructor with no arguments, it is obvious!
void print (void)
{
cout <<“C cout” <
};
int main ()
{
C c (2); / / construct the order of ABC
/ / C.A:: print ();
/ / C.C:: print ();
C d = c;
/ / D.A:: print ();
return 0;
}
This program has several points:
1 First of all, no matter how the constructor of category C write, initialize the list or how to call, how an order, the order of the structure of each class were unchanged at ABC (the base class, the combination of class, subclass)
C (int i = 0): b (i), A (i) {cout <<“C is doing” < / / C (int i = 0): A (i), b (i) {cout <<“C is doing” < / / C (int i = 0): b (i) {cout <<“C is doing” < / / C (int i = 0): A (i) {cout <<“C is doing” < / / C (int i = 0) {cout <<“C is doing” < 2, if not explicitly call b the copy constructor, the compiler will call b the default constructor is very good understanding, if not explicitly call A copy constructor, the compiler will call the default constructor for A ! 3, if the definition of A or B do not own a copy constructor, C explicitly call them if the copy constructor is invoked the compiler synthesized copy constructor. If C is not defined copy constructor, the compiler of the synthesized copy constructor will call B’s copy constructor, because the compiler synthesized copy constructor is to order copies of internal members. So in this case will be called A copy of the structure, because the synthesized copy constructor is explicitly called it.
Sorry, the comment form is closed at this time.


Comments
No comments yet.