Step-by-step learning C + + (Class) Inheritance and derivation

Source: Internet
Author: User

PaiShengAndDerive.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <string.h>class teacher{public:teacher (const char *s,int x,int N) {strcpy ( Mathteaname,s); Classroom = x; Teayear = n; printf ("base class constructor!\n");} void display ();p Rotected:char mathteaname[20];      Math teacher name int teayear;private:int Classroom; Classroom};void Teacher::d isplay () {printf ("%d\n", Classroom);p rintf ("%s\n", Mathteaname);p rintf ("%d\n", Teayear);} /* Derived class */class Student:public teacher{public:/* derived class constructor name (total parameter table column): base class constructor name (Parameter table column), child object name (parameter table column) *//*student (const char *s,int M,int N): Teacher ("Li", "P", Wang ("Wang", 45) the constructor of the derived class */student (const char *s,int m,int n,int x,int years): Teacher (S,x, Years), Wang (s,x,years) {strcpy (name,s); Math = m; Chinese = n;printf ("derived class constructor!\n");} void Std_display ();p Rivate:char name[20];int math;int Chinese; Teacher wang;//Sub-object};void student::std_display () {printf ("%d\n", teayear);//Derived classes access the base class protected members//printf ("%d\n", classroom) ;//"Teacher::classroom": unable to access private member (declared in "Teacher" Class)//derived class cannot access the secret member of the base class}int _tmain (int argc, _TChar* argv[]) {Student std1 ("Li", 100,100,1,50); Std1.std_display (); GetChar (); return 0;} 

The inheritance methods are: public (common), private (private), and protected (protected), which is optional and, if not written, the default is private.The so-called common, is that the defined variables or functions can be referenced outside the class, and private, then the definition of the variable or function can only be used within the class, the protected meaning is cannot be referenced by the outside world, but can be referenced by members of the derived class.

Student (const char *s,int m,int n,int x,int years): Teacher (S,x,years), Wang (S,x,years)

1) the order in which the derived class constructors are executed is:

①  calls the base class constructor for base class data member initialization;

②  called the sub-object constructor, the child object data member initialization

③  again executes the derived class constructor itself, to derived class data member initialization


2) The destructor of the derived class  

      When deriving, a derived class cannot inherit the destructor of the base class, and the destructor of the base class needs to be called through the destructor of the derived class. In a derived class, you can define your own destructor as needed to clean up the added members of the derived class. When you execute a destructor for a derived class, the system automatically calls the destructor of the base class and the destructor of the child object to clean up the base class and child objects. The order of the calls is the opposite of the constructor:   first perform the derived class's own destructor, Cleans up the newly added members of the derived class, then invokes the destructor of the child object, and finally calls the destructor of the base class.


3) virtual base class

From the above study, we found that if a derived class has more than one direct base class, and these direct base classes have a common base class, then in the final derived class, multiple copies of the same member of the indirect common base class data member will be retained. c++ provides virtual base class (virtual  base class), so that only one member is retained when inheriting the indirect common base class.

class a//declares base class A

 {...};

class b :virtual public a            //declares that Class B is a common derived class of Class A, and that a is the virtual base class of B

 {...};

class c :virtual public a            //declares that Class C is a common derived class of Class A, and that a is the virtual base class of C

 {...};

Note that the:  virtual base class is not declared when the base class is declared, but when the derived class is declared, when the inheritance method is specified. Because a base class can be used as a virtual base class when a derived class is generated, and not as a virtual base class when another derived class is generated.

Declares the general form of the virtual base class as

class  derived class name: virtual  inheritance   base class name

After such a declaration, when a base class is inherited by a derived class through multiple derived paths, the derived class inherits the base class only once. to ensure that the virtual base class inherits only once in the derived class, The virtual base class should be declared in all direct derived classes of the base class.

If a constructor with parameters is defined in the virtual base class and no default constructor is defined, in all of its derived classes, including derived classes that derive directly or indirectly, the virtual base class is initialized through the constructor's initialization table.

In the last derived class, it is not only the responsibility to initialize its immediate base class, but also the initialization of the virtual base class. The C + + compilation system only executes calls from the last derived class to the constructor of the virtual base class, while ignoring other derived classes of the virtual base class (such as Class B and Class C) calls to the virtual base class's constructor, which guarantees that the data members of the virtual base class are not initialized more than once.


To learn the inheritance and derivation of C + + (Class) step-by-step

Related Article

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.