C ++ - succession configured destructor, conflict between father and son

I. succession configured destructor

Q: how to initialize the parent class members? Parent and child class constructor class constructor by what relationship?
A. constructor class object
1. subclass can be defined constructor
2. subclass constructor - the process must be a member of inherited initialization (initialized directly by way of assignment or initialization list, call the parent class constructor initialized)
B. parent class constructor is called the subclass
1. default call - constructor is applied and no argument constructor with default parameters
2. shows the call - by the initialization call list, for all parent class constructor
code sample

#include <iostream>
#include <string>

using namespace std;

class Parent 
{
public:
    Parent()
    {
        cout << "Parent()" << endl;
    }
    Parent(string s)
    {
        cout << "Parent(string s) : " << s << endl;
    }
};

class Child : public Parent
{
public:
    Child()//对父类构造函数进行隐式调用
    {
        cout << "Child()" << endl;
    }
    Child(string s) : Parent(s)//对父类构造函数进行显示调用
    {
        cout << "Child(string s) : " << s << endl;
    }
};

int main()
{       
    Child c; 
    Child cc("cc");

    return 0;
}

The results shown in FIG operation
C ++ - succession configured destructor, conflict between father and son
may be a summary of the rules of the exemplary configuration and operation results: When creating a subclass object will first call the constructor of the parent class, perform the parent class constructor constructor executes subclass, the parent the class constructor can be invoked implicitly or displaying call
constructor class object: 1. call parent class constructor calls the constructor 2. 3. calling a class member variable itself constructor
subclass depth analysis of structure

#include <iostream>
#include <string>

using namespace std;

class Object
{
public:
    Object(string s)
    {
        cout << "Object(string s) : " << s << endl;
    }
};

class Parent : public Object
{
public:
    Parent() : Object("Default")
    {
        cout << "Parent()" << endl;
    }
    Parent(string s) : Object(s)
    {
        cout << "Parent(string s) : " << s << endl;
    }
};

class Child : public Parent
{
    Object mO1;
    Object mO2;
public:
    Child() : mO1("Default 1"), mO2("Default 2")
    {
        cout << "Child()" << endl;
    }
    Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2")
    {
        cout << "Child(string s) : " << s << endl;
    }
};

int main()
{       
    Child cc("cc");

    return 0;
}

Run Results
C ++ - succession configured destructor, conflict between father and son
Object mO1 Object mO2 a combination relation, to the parent of an explicit call to the construction order of the print result is consistent with the above mentioned
destructor C. subclass object
calling sequence constructor destructor Instead
1. perform its destructor
2. Run variable destructor member
3. Run the parent class destructor

The sample code

#include <iostream>
#include <string>

using namespace std;

class Object
{
    string ms;
public:
    Object(string s)
    {
        cout << "Object(string s) : " << s << endl;
        ms = s;
    }
    ~Object()
    {
        cout << "~Object() : " << ms << endl;
    }
};

class Parent : public Object
{
    string ms;
public:
    Parent() : Object("Default")
    {
        cout << "Parent()" << endl;
        ms = "Default";
    }
    Parent(string s) : Object(s)
    {
        cout << "Parent(string s) : " << s << endl;
        ms = s;
    }
    ~Parent()
    {
        cout << "~Parent() : " << ms << endl;
    }
};

class Child : public Parent
{
    Object mO1;
    Object mO2;
    string ms;
public:
    Child() : mO1("Default 1"), mO2("Default 2")
    {
        cout << "Child()" << endl;
        ms = "Default";
    }
    Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2")
    {
        cout << "Child(string s) : " << s << endl;
        ms = s;
    }
    ~Child()
    {
        cout << "~Child() " << ms << endl;
    }
};

int main()
{       
    Child cc("cc");

    cout << endl;

    return 0;
}

Operating results
C ++ - succession configured destructor, conflict between father and son
summary:
1. When you create a subclass object need to call the parent class constructor to initialize
the constructor 2. The first implementation of the parent class constructor then a member of the Executive
3. the parent class constructor calls the show needs to be initialized in list
4. subclass object destroyed when needed to call the parent class destructor cleanup
the constructor and destructor symmetrically opposite

II. The conflict between father and son

Q: subclass can define whether the parent class members with the same name? if it is possible? How to distinguish? If not, why?
The sample code

#include <iostream>
#include <string>

using namespace std;

class Parent
{
public:
    int mi;
};

class Child : public Parent
{
public:
    int mi;
};

int main()
{
    Child c;

    c.mi = 100;    // mi 究竟是子类自定义的,还是从父类继承得到的?
    cout<<"c.mi="<<c.mi<<endl; 
    return 0;
}

Operating results
C ++ - succession configured destructor, conflict between father and son
from this example and run the results we will draw some doubt, according to the concept of inheritance, the subclass has all the attributes and behavior of the parent class, in the program the father and son class defines mi, and in its initialization uncertainty is a subclass of a custom, or inherited from the parent class get. Creating a conflict between father and son.
The conflict between father and son A.
1. subclasses can define the members of the same name in the parent class
2 subclass members will hide members of the same name in the parent class
members 3. The parent of the same name still exists in the sub-category
4. by scope resolution operator (: :) to access members of the same name of the parent class
C ++ - succession configured destructor, conflict between father and son
member variable-depth analysis of the same name - code sample

#include <iostream>
#include <string>

using namespace std;

namespace A
{
    int g_i = 0;
}

namespace B
{
    int g_i = 1;
}

class Parent
{
public:
    int mi;

    Parent()
    {
        cout << "Parent() : " << "&mi = " << &mi << endl;
    }
};

class Child : public Parent
{
public:
    int mi;

    Child()
    {
        cout << "Child() : " << "&mi = " << &mi << endl;
    }
};

int main()
{
    Child c;

    c.mi = 100;    

    c.Parent::mi = 1000;

    cout << "&c.mi = " << &c.mi << endl;
    cout << "c.mi = " << c.mi << endl;

    cout << "&c.Parent::mi = " << &c.Parent::mi << endl;
    cout << "c.Parent::mi = " << c.Parent::mi << endl;

    return 0;
}

Operating results
C ++ - succession configured destructor, conflict between father and son
can be seen from the results of the operation code section, it can be clearly seen by the obtained address value, in the end the parent class and subclass which is called
B: More on overloaded
member function can be overloaded
1. nature overloaded function of a number of different functions
2. the domain name of the function parameter list is the unique identification
3. overloaded functions must occur in the same scope

Sons overloads between the experimental
sample code and operating results

#include <iostream>
#include <string>

using namespace std;

class Parent
{
public:
    int mi;

    void add(int v)
    {
        mi += v;
    }

    void add(int a, int b)
    {
        mi += (a + b);
    }
};

class Child : public Parent
{
public:
    int mi;

    void add(int v)
    {
        mi += v;
    }

    void add(int a, int b)
    {
        mi += (a + b);
    }

    void add(int x, int y, int z)
    {
        mi += (x + y + z);
    }
};

int main()
{
    Child c;

    c.mi = 100;    

    c.Parent::mi = 1000;

    cout << "c.mi = " << c.mi << endl;

    cout << "c.Parent::mi = " << c.Parent::mi << endl;

    c.add(1);
    c.add(2, 3);
    c.add(4, 5, 6);

    cout << "c.mi = " << c.mi << endl;

    cout << "c.Parent::mi = " << c.Parent::mi << endl;

    return 0;
}

C ++ - succession configured destructor, conflict between father and son
Conflicts between Sons
1. Function subclasses will hide the same name of the parent class
2. subclass can not overload the parent class member function
3. The functions of the same name resolution using the scope operator access to the parent class
4. subclasses exactly defined parent class member functions

Guess you like

Origin blog.51cto.com/13475106/2416010