Indispensable Windows Native (22), native22

Source: Internet
Author: User

Indispensable Windows Native (22), native22

[Download source code]


Indispensable Windows Native (22)-C ++: Multi-inheritance, virtual base class



Author: webabcd


Introduction
Windows Native C ++ is indispensable

  • Multi-Inheritance
  • Virtual base class



Example
1. Base Class 1
CppBase1.h

# Pragma once # include <string> # include "CppBaseVirtual. h "using namespace std; namespace NativeDll {// virtual indicates that CppBaseVirtual is the virtual base class of CppBase1 (the virtual base class is declared when the inheritance method is specified when the derived class is declared) class CppBase1: virtual public CppBaseVirtual {protected: string Name; float Salary; public: CppBase1 (int number, string name, float salary );};}

CppBase1.cpp

/** Base class 1 */# include "pch. h "# include" CppBase1.h "using namespace NativeDll; CppBase1: CppBase1 (int number, string name, float salary): CppBaseVirtual (number), Name (" cppbase1 "+ name ), salary (salary ){}


2. Base Class 2
CppBase2.h

# Pragma once # include <string> # include "CppBaseVirtual. h "using namespace std; namespace NativeDll {// virtual indicates that CppBaseVirtual is the virtual base class of CppBase2 (the virtual base class is declared when the inheritance method is specified when the derived class is declared) class CppBase2: virtual CppBaseVirtual {protected: string Name; int Age; public: CppBase2 (int number, string name, int age );};}

CppBase2.cpp

/** Base class 2 */# include "pch. h "# include" CppBase2.h "using namespace NativeDll; CppBase2: CppBase2 (int number, string name, int age): CppBaseVirtual (number), Name (" cppbase2 "+ name ), age (age ){}


3. virtual base class
CppBaseVirtual. h

#pragma once #include <string>using namespace std;namespace NativeDll{    class CppBaseVirtual    {    private:        int Number;    public:        CppBaseVirtual(int number);    };}

CppBaseVirtual. cpp

/** Used to demonstrate the virtual base class ** Note: * 1. The virtual base class is not declared when the base class is declared, but when the derived class is declared, * 2. A base class declared during inheritance can be used as a virtual base class when a derived class is generated, you can also generate a derived class instead of a virtual base class */# include "pch. h "# include" CppBaseVirtual. h "using namespace NativeDll; CppBaseVirtual: CppBaseVirtual (int number): Number (number ){}


4. Derived classes
CppDerived. h

#pragma once #include <string>#include "CppBase1.h"#include "CppBase2.h"using namespace std;namespace NativeDll{    class CppDerived : public CppBase1, public CppBase2    {    public:        CppDerived(int number, string name, float salary, int age);        string Show();    };}

CppDerived. cpp

/** Derived class **: * 1. If A is the base class of B c d e, F inherits B C D E at the same time, when F is instantiated, four copies of A member * 2 will be saved. If A is the base class of B, A is the virtual base class of c d e (the virtual base class will retain only one member when inheriting the indirect common base class), F inherits B C D E at the same time, when F is instantiated, two copies of A member will be saved (one copy from the path of c d e and one copy from the path of B). *** in this example: * 1. CppBaseVirtual is the virtual base class of CppBase1 and CppBase2 * 2. CppDerived inherits CppBase1 and CppBase2 (multiple inheritance) * 3. In this case, when CppDerived is instantiated, only one CppBaseVirtual member is retained (because CppBaseVirtual is the virtual base class of CppBase1 and CppBase2). * 4. The C ++ compiler only executes the final derived class (CppDerived) call the constructor of the virtual base class (CppBaseVirtual), and ignore the call of the constructor of the virtual base class (CppBase1 and CppBase2) to the constructor of the virtual base class, this ensures that the data members of the virtual base class will not be initialized multiple times */# include "pch. h "# include" CppDerived. h "# include" cppHelper. h "using namespace NativeDll; // In the absence of a virtual base class, the constructor of the derived class only needs to initialize the base class directly. // if there is a virtual base class, A constructor with parameters is defined in the virtual base class, and no default constructor is defined. The derived class is not only responsible for initializing its direct base class, cppDerived: CppDerived (int number, string name, float salary, int age): CppBaseVirtual (number), CppBase1 (number, name, salary ), cppBase2 (number, name, age) {} string CppDerived: Show () {// ambiguity of Multi-inheritance (ambiguous ), if multiple base classes inherited by a derived class have the same members, you must explicitly specify their base classes return CppBase1: Name + "" + float2string (Salary) when calling these members) + "" + int2string (Age); // In addition, if the derived class has the same member as multiple base classes, all the Members in the base class and the derived class will be hidden (that is, the Members in the derived class will overwrite the members in the base class )}


5. Example
CppClass6.h

#pragma once #include <string>using namespace std;namespace NativeDll{    class CppClass6    {    public:        string Demo();    };}

CppClass6.cpp

/** Multiple inheritance and virtual base class */# include "pch. h "# include" CppClass6.h "# include" CppDerived. h "using namespace NativeDll; string CppClass6: Demo () {CppDerived derived (0," webabcd ", 100366f, 35); string result = derived. show (); // cppbase1 webabcd 100.00 35 return result ;}



OK
[Download source code]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.