Virtual base class)

Source: Internet
Author: User

The general form of declaring a virtual base class is:

 
ClassDerived class name:VirtualInheritance Method Base Class Name

After such declaration, when the base class is inherited by a derived class through multiple derived paths, the derived class only inherits the derived class once, that is, the base class members are retained only once. To ensure that the virtual base class is inherited only once in the derived class, it should be declared as a virtual base class in all the direct Derived classes of the base class. Otherwise, multiple inheritance of the base class will still appear.

Note: In the final derived class, not only is it responsible for initializing its direct base class, but also for initializing the virtual base class

The C ++ compilation system only calls the constructor of the virtual base class by the final derived class, and ignores the call of the constructor of the virtual base class by other derived classes, this ensures that the data member of the virtual base class will not be initialized.

 1 # Include <iostream>
2 # Include < String >
3
4 Using Namespace STD;
5
6 // Declare a public base class person
7 Class Person
8 {
9 Public :
10 Person ( String Nam, Char Se, Int A)
11 {
12 Name = Nam;
13 Sex = Se;
14 Age =;
15 }
16 Protected :
17 String Name;
18 Char Sex;
19 Int Age;
20 };
21
22 // Declare the direct derived class teacher of person
23 Class TEACHER:Virtual Public Person // Declare person as a public-inherited virtual base class
24 {
25 Public :
26 Teacher ( String Nam, Char Se, Int A, String Ti): Person (Nam, Se,)
27 {
28 Title = Ti;
29 }
30 Protected :
31 String Title;
32 };
33
34 // Declare the direct derived class student of person
35 Class Student: Virtual Public Person // Declare person as a public-inherited virtual base class
36 {
37 Public :
38 Student ( String Nam, Char Se, Int A, Float SCO): Person (Nam, Se, A), score (SCO ){}
39 Protected :
40 Float Score;
41 };
42
43 // Declare a multi-inheritance derived class graduate
44 Class Graduate: Public Teacher, Public Student
45 {
46 Public :
47 Graduate ( String Nam, Char Se, Int A, String Ti,Float SCO, Float WA): Person (Nam, Se, A), teacher (Nam, Se, A, Ti), student (Nam, Se, A, SCO), wage (WA ){}
48 Void Show ()
49 {
50 Cout < " Name: " <Name <Endl;
51 Cout < " Sex: " <Sex <Endl;
52 Cout < " Age: " <Age <Endl;
53 Cout < " Title: " <Title <Endl;
54 Cout < " Score: " <Score <Endl;
55 Cout < " Wage: " <Wage <Endl;
56 }
57 Private :
58 Float Wage;
59 };
60
61 // Main Function
62 Int Main ( Void )
63 {
64 Graduate Grad ( " WangLi " , ' F ' , 24 , " Assistant " , 89.6 , 2000.6 );
65 Grad. Show ();
66 Return 0 ;
67 }

in the graduate class, only one member of the base class is retained, therefore, you can use the show function in the graduate class to reference the value of name, sex, and age of the data member of the public base class person in the graduate class object. You do not need to add the base class name and domain operator '':: '', does not produce ambiguity.

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.