Indispensable Windows Native-C + +: Multiple inheritance, virtual base class

Source: Internet
Author: User

[SOURCE DOWNLOAD]


Indispensable Windows Native-C + +: Multiple inheritance, virtual base class



Webabcd


Introduced
Essential C + + of Windows Native

    • Multiple inheritance
    • Virtual base class



Example
1. Base Class 1
CppBase1.h

#pragmaOnce#include<string>#include"CppBaseVirtual.h"using namespacestd;namespacenativedll{//Virtual represents cppbasevirtual is the virtual base class of CppBase1 (the virtual base class is declared when the derived class is declared with the inheritance method specified)    classCPPBASE1:Virtual  Publiccppbasevirtual {protected:        stringName; floatSalary;  Public: CppBase1 (intNumberstringNamefloatsalary); };}

CppBase1.cpp

 /*   */  #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

#pragmaOnce#include<string>#include"CppBaseVirtual.h"using namespacestd;namespacenativedll{//Virtual represents cppbasevirtual is the virtual base class of CppBase2 (the virtual base class is declared when the derived class is declared with the inheritance method specified)    classCPPBASE2:Virtualcppbasevirtual {protected:        stringName; intAge ;  Public: CppBase2 (intNumberstringNameintAge ); };}

CppBase2.cpp

 /*   */  #include   " pch.h   "  #include   " cppbase2.h   " using  namespace   Nativedll; Cppbase2::cppbase2 ( int  number, string  name, int  Age): cppbasevirtual (number), name (  " cppbase2   + name", age {}  


3. Virtual base class
CppBaseVirtual.h

#pragma<string>usingnamespace  std; namespace nativedll{    class  cppbasevirtual    {    private:         int number ;      Public :        Cppbasevirtual (int number );}    ;}

CppBaseVirtual.cpp

/**/"pch.h""CppBaseVirtual.h  "usingnamespace  Nativedll; Cppbasevirtual::cppbasevirtual (int number ): Number (number) {}


4. Derived classes
CppDerived.h

#pragmaOnce#include<string>#include"CppBase1.h"#include"CppBase2.h"using namespacestd;namespacenativedll{classCppderived: PublicCPPBASE1, PublicCppBase2 { Public: cppderived (intNumberstringNamefloatSalaryintAge ); stringShow (); };}

CppDerived.cpp

/** Derived class * * * in case of multiple inheritance: * 1, if a is the base class of B C D E, F also inherits B C D E, then the instantiation of F will save 4 A member * 2, if a is the base class of B, a is the virtual base class of C D e (virtual  The base class makes it possible to retain only one member when inheriting the indirect common base class, and F inherits the B C D E, so that when you instantiate F, you save 2 A members (one copy from the path of C D E, and one copy from Path 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, instantiating cppderived will only retain a copy of the Cppbasevirtual member ( Because Cppbasevirtual is the virtual base class of CppBase1 and CppBase2) * 4, the C + + compiler will only perform the last derived class (cppderived) call to the virtual base class (Cppbasevirtual) constructor, Instead of ignoring the direct derived classes of the virtual base class (CppBase1 and CppBase2) calls to the constructor of the virtual base class, this guarantees 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 namespaceNativedll;//in the absence of a virtual base class, the constructor of a derived class is only responsible for initializing its direct base class//If there is a virtual base class, and a constructor with parameters is defined in the virtual base class, and no default constructor is defined, then the derived class is not only responsible for initializing its direct base class, but also for initializing the virtual base classCppderived::cppderived (intNumberstringNamefloatSalaryintAge ): cppbasevirtual (number), CppBase1 (number, name, salary), CppBase2 (number, name, age) {}stringcppderived::show () {//With respect to the two semantics of multiple inheritance (ambiguous), if multiple base classes that are inherited by a derived class have the same members, you must explicitly specify their base class when calling these members    returnCppbase1::name +" "+ float2string (Salary) +" "+int2string (age); //also: Of course, if a derived class has the same members as more than one base class, those members of the base class that are the same as 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<string>usingnamespace  std; namespace nativedll{    class  CppClass6    {    public:         string  Demo ();    };}

CppClass6.cpp

/** Multiple Inheritance (multiple inheritance), virtual base class*/#include"pch.h"#include"CppClass6.h"#include"CppDerived.h" using namespaceNativedll;stringCppClass6::D emo () {cppderived derived (0,"WEBABCD",100.0f, *); stringresult = derived. Show ();//cppbase1 WEBABCD 100.00    returnresult;}



Ok
[SOURCE DOWNLOAD]

Indispensable Windows Native-C + +: Multiple inheritance, virtual base class

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.