c++--class Inheritance

Source: Internet
Author: User

    //Inheritance//Inheritance principle: The base class is private, which is inheritable, inaccessible//The rest is who's grade confidential and who's taking//protected is not accessible outside of the subclass.//Subclass & Parent class derived classes & base classes//memory Problem: The derived class is understood to contain a nameless base class memory in the derived class's memory, preceded by a derived class memory range in the base class; so is multiple inheritance.//Construction Order//the construction of each class is divided into three steps://1. Check if there is a base class, and if so, construct the base class//2. Check if there are sub-objects, if any, the sub-object is one of the other classes in this class to declare the definition of an object, that is, go in and give him constructs//3. Construct itself//If you do not have to write the structure of the parameter, then you do not have to initialize, but once you have their own constructor, then the constructor in the subsequent derived class will also have parameters to satisfy the construction of the base class, initialize//Destruction Order:: How to construct, how to reverse the destruction#pragmaOnceclasscnpc{intHP; Public: CNpc (); CNPC (intHintm); ~CNpc ();}; #include"stdafx.h"#include"Npc.h"CNPC::CNPC () {printf ("cnpc-1\n"); } CNPC::CNPC (intHintm) {printf ("cnpc-2\n"); HP=h; }
//*****************************************************#include"Npc.h" classCSHOPNPC: PublicCNpc { Public: CSHOPNPC (inth); ~CSHOPNPC (); }; #include"stdafx.h"#include"ShopNpc.h"CSHOPNPC::CSHOPNPC (inth): CNpc (H,2)//, M (3){printf ("cshopnpc\n"); }
//******************************************************#pragmaOnce#include"ShopNpc.h"classcm{ Public: CM (intm) {printf ("cm\n"); } intM//There is no memory for a in this, only the memory is allocated when the object is defined. };classCX: Publiccshopnpc{ Public: CX (inth); ~CX (); Public: CM m;//child object, the data member of the class is a class object, and this class object is called a child object};
//**************************************************************Cx::cx (inth): CSHOPNPC (h), M (4)//location, base class and subclass{printf ("cx\n");}//******************************************************** classBook {//constructor FunctionBook (Const Char*the_title,intPages,Const Char*the_writer): num_pages (pages) {title=New Char[Strlen (The_title) +1]; strcpy (Title,the_title); Writer=New Char[Strlen (The_writer) +1]; strcpy (Writer,the_writer); } ~book () {Delete[] title,writer; }//writing Form } classTeachingmaterial: PublicBook {Char*course; Public://constructors for derived classes must initialize the base class by using the member initialization list methodTeachingmaterial (Const Char*the_title,intPages,Const Char*the_writer,Const Char*the_course): Book (the_title, pages, The_writer) {course=New Char[Strlen (The_course) +1]; strcpy (Course,the_course); }} //AB Two classes, B is a derived class, show () is a global function, the main function has a A; b b; Show (a); show (b), show parameter is type A, that is, the parameters of the base class type

        // a derived class object is necessarily a base class object, and the base class object is not a derived class object         Cfoodbox FB;        CBox B (12);         = FB;        Cfood f (rice);         = FB;                
// Multiple inheritance is related to the construction order of multiple base classes and the declaration of inheritance, regardless        of the invocation of the member initialization list // The order in which the child objects are constructed is related to the order in which the child objects are declared in the class, regardless of the member initialization list.         class Cfoodbox: public public cfoodbox        {         Public :            Cfoodbox ();             ~Cfoodbox ();         Private :            CY y;            CX x;        };         // Construct Order: base class sub-object itself: CBox  Cfoodbox CY CX Cfoodbox;; The destruction sequence is completely opposite
//variable assignment with the same name in a derived classCE test; TEST.A= -;//derived class with the same name variable assignment itselfTest. Cd::a =123;//assignment of A to a CD base class in a derived classTest. Cb::a =456;//derived Class A assigns a value of one of the CB base classesTest. Cd::ca::a = $;//derived classes one of the CD base class CA base Class A assignmentTest. Cb::ca::a = -;//A derived class where a CB base class CA base Class A assigns a value//can be accessed to implement

c++--class Inheritance

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.