Processing methods of inheriting members with the same name in c + + learning notes

Posted by Spartan 117 on Sat, 25 Dec 2021 02:48:26 +0100

catalogue

Object model in inheritance

Construction and deconstruction order in inheritance

Verification Code:

Inherit static member handling with the same name

Verification Code:

Object model in inheritance


Question: which members inherit from the parent class belong to the subclass object?
    
1) all non static member properties in the parent class will be inherited by the child class
2) the private member attribute in the parent class is hidden by the compiler, so it cannot be accessed, but it is inherited

Construction and deconstruction order in inheritance


After a subclass inherits from the parent class, when a subclass object is created, the constructor of the parent class will also be called
Question: which is the construction and deconstruction order of parent and child classes?

The order of construction and destruction in inheritance is as follows:
Construct the parent class first. When constructing the child class, the destruct order is opposite to the construction order (that is, destruct the child class first and destruct the parent class)

48. Handling method of inheriting members with the same name
Question: when a member with the same name appears in a subclass and parent class, how can you access the data with the same name in the subclass or parent class through the subclass object?

1) access the member with the same name of the subclass directly
2) a scope is required to access a member with the same name as the parent class
3) to access members with different names in the parent and child classes, you can call them directly
4) if a member function with the same name as the parent class appears in the subclass, the member with the same name in the subclass will hide all member functions with the same name in the parent class, and overloading is not easy!!
If you want to access a member function with the same name hidden in the parent class, you need to add a scope


Verification Code:

        #include <iostream>
        using namespace std;

        class Base {
        public:
            Base()
            {
                m_Name = "from Base";
                m_A = 10;
            }

            //
            void func(int a)
            {
                cout << "this func from Base" << endl;
            }

            string m_Name;
            int m_A;
        };

        class Son : public Base {
        public:
            Son()
            {
                m_Name = "from Son";
            }

            void func()
            {
                cout << "this func from Son" << endl;
            }

            string m_Name;
        };

        void test01()
        {
            Son s1;

            //Members with the same name of the subclass can be accessed directly
            cout << "this m_Name " << s1.m_Name << endl;

            //Scope is required to access the member with the same name as the parent class
            cout << "this m_Name " << s1.Base::m_Name << endl;

            //To access members with different names in the parent and child classes, you can call them directly
            cout << "m_A =  " << s1.m_A << endl;

            //If a member function with the same name as the parent class appears in the subclass, the member with the same name in the subclass will hide all member functions with the same name in the parent class
            //Overloading is not easy!!
            //s1.func(100); / / an error will be reported

            //If you want to access a member function with the same name hidden in the parent class, you need to add a scope
            s1.Base::func(100);    

            //Direct access is in a subclass, and it needs to be written according to the rules of the subclass (if a subclass has several formal parameters, it will wear several formal parameters)
            s1.func();
        }

        int main()
        {
            test01();

            system("pause");

            return 0;
        } 

Inherit static member handling with the same name


Question: how do static members with the same name in inheritance access on subclass objects
Static members and non static members have the same name and are handled in the same way
1) access the member with the same name of the subclass directly
2) a scope is required to access a member with the same name as the parent class

Verification Code:

        #include <iostream>
        using namespace std;

        class Base {
        public:
            static string m_Name;
            static void func()
            {
                cout << "this func from Base" << endl;
            }
        };
        string Base::m_Name = "My name is Base";

        class Son : public Base {
        public:
            static string m_Name;
            static void func()
            {
                cout << "this func from Son" << endl;
            }
        };
        string Son::m_Name = "My name is Son";

        //Static member property with the same name
        void test01()
        {
            //Access by object
            cout << "Access by object" << endl;
            Son s1;
            cout << s1.m_Name << endl;
            cout << s1.Base::m_Name << endl;

            //Access by class name
            cout << "Access by class name" << endl;
            cout << Son::m_Name << endl;
            //The first:: represents access through the class name, and the second:: represents access under the scope of the parent class
            cout << Son::Base::m_Name << endl;
        }


        //Static member property with the same name
        void test02()
        {
            //Access by object
            cout << "Access by object" << endl;
            Son s1;
            s1.func();
            s1.Base::func();

            //Access by class name
            cout << "Access by class name" << endl;
            Son::func();
            Son::Base::func();
        }


        int main()
        {
            //test01();
            test02();

            system("pause");

            return 0;
        }

Topics: C++ inheritance