Learning C ++ inheritance from scratch (2): inheritance and constructor, conversion from a derived class to a base class

Source: Internet
Author: User
Tags class manager

1. member functions that cannot be automatically inherited

Constructor (including copy constructor)

Destructor
= Operator


Ii. Inheritance and constructor

The constructor of the base class is not inherited. You must declare your own constructor In the derived class.
When declaring constructors, you only need to initialize new members in this class, the base class constructor is called for the initialization of the inherited base class members (if not given, the default constructor is called by default ).
The constructor of the derived class must pass parameters to the constructor of the base class.


C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Include <iostream>
Using namespace STD;

Class objectb
{
Public:
Objectb (INT objb): objb _ (objb)
{
Cout <"objectb..." <Endl;
}
~ Objectb ()
{
Cout <"~ Objectb... "<Endl;
}
Int objb _;
};

Class objectd
{
Public:
Objectd (INT objd): objd _ (objd)
{
Cout <"objectd..." <Endl;
}
~ Objectd ()
{
Cout <"~ Objectd... "<Endl;
}
Int objd _;
};

Class base
{
Public:
Base (int B): B _ (B), objb _ (111)
{
Cout <"base..." <Endl;
}
Base (const base & other): objb _ (other. objb _), B _ (other. B _)
{

}
~ Base ()
{
Cout <"~ Base... "<Endl;
}
Int B _;
Objectb objb _;
};

Class derived: public Base
{
Public:
Derived (int B, int D): D _ (D), base (B), objd _ (222)
{
Cout <"derived..." <Endl;
}
Derived (const derived & other): D _ (other. D _), objd _ (other. objd _), base (other)
{

}
~ Derived ()
{
Cout <"~ Derived... "<Endl;
}
Int D _;
Objectd objd _;
};

Int main (void)
{
Derived D (100,200 );
Cout <D. B _ <"" <D. D _ <Endl;

Base B1 (100 );
Base B2 (B1 );
Cout <b2. B _ <Endl;

Derived D2 (d );
Return 0;
}


The output shows that:

Structure order of the derived class object:

First, call the constructor of the base class Object member, then the constructor of the base class, then the constructor of the object Member of the derived class, and finally the constructor of the derived class itself.

You can also see that the execution sequence of the constructor is to execute the initialization list first, and then the function body. When multiple parameters in the initialization list are called and a base class constructor is called, the base class constructor is executed first (from the farthest start, if multiple inheritance is performed, the base class constructor is inherited ); if there are more than one other object member, it is constructed in the defined order and has nothing to do with the initialization list order. For the initialization list, refer to here

The structure order is the opposite to the structure order.

Iii. Friendship, static members and inheritance

The relationship between friends and friends cannot be inherited.

Do not inherit static members

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Include <iostream>
Using namespace STD;

Class base
{
Public:
Static int B _;
};

Int base: B _ = 100;
Class derived: public Base
{

};

Int main (void)
{
Base B;
Derived D;
Cout <base: B _ <Endl;
Cout <B. B _ <Endl;

Cout <derived: B _ <Endl;
Cout <D. B _ <Endl;

Return 0;
}

Can be accessed, with an output of 100, but the recommended class is: XX access. For example, B. B _ access is ambiguous. In fact, static members do not belong to any object.


Iv. Conversion from a derived class to a base class

When a derived class inherits the base class in the public mode, the compiler can automatically perform the conversion (up-to-transition upcasting secure conversion)

The object pointer of the derived class is automatically converted to the object pointer of the base class.

The reference of a derived class object is automatically converted to a base class object reference.

The derived class object is automatically converted to the Base Class Object (the unique member disappears)

When the derived class inherits the base class in private/protected mode

To convert a derived class Object Pointer (reference) to a base class Object Pointer (reference), a forced type conversion is required. But static_cast cannot be used. reinterpret_cast must be used.

You cannot forcibly convert a derived class object to a base class object.


C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Include <iostream>
# Include <string>
Using namespace STD;

Class employee
{
Public:
Employee (const string & name, const int age, const int deptno): Name _ (name ),
Age _ (AGE), deptno _ (deptno)
{

}
PRIVATE:
String name _;
Int age _;
Int deptno _;
};

Class MANAGER: Public Employee
{
Public:
Manager (const string & name, const int age, const int deptno, int level)
: Employee (name, age, deptno), Level _ (level)
{

}
PRIVATE:
Int level _;
};

Class manager2: private employee
{
Public:
Manager2 (const string & name, const int age, const int deptno, int level)
: Employee (name, age, deptno), Level _ (level)
{

}
PRIVATE:
Int level _;
};

Int main (void)
{
Employee E1 ("zhangsan", 25, 20 );
Manager M1 ("Lisi", 38, 20, 10 );
Manager 2 m2 ("wangwu", 40, 15, 8 );
Employee * PE;
Manager * PM;
Manager2 * PM2;

Pe = & E1;
PM = & M1;
PM2 = & m2;

Pe = & M1; // The Object Pointer of the derived class can be converted to the Base Class Object Pointer. Converts a derived class object to a base class object.
// PM = & E1; // The Base Class Object Pointer cannot be converted to the class Class Object Pointer. You cannot regard a base class object as a derived class object.

E1 = m1; // a derived class object can be converted to a base class object. Converts a derived class object to a base class object.
// It will generate object cutting (the Special Member of the derived class disappears ). Object Slicing

// Pe = PM2; // Private or protected inheritance, the object pointer of the derived class cannot be automatically converted to the Base Class Object Pointer
Pe = reinterpret_cast <employee *> (PM2 );

// E1 = m2; // Private or protected inheritance, the derived class object cannot be converted to the base class object.
// E1 = reinterpret_cast <employee> (m2); // when private or protected inheritance, the derived class object cannot be forcibly converted to the base class object.

PM = static_cast <manager *> (PE); // The base class pointer can be forcibly converted to a derived class pointer, but it is not safe.

// M1 = reinterpret_cast <Manager> E1; // The base class object cannot be forcibly converted to a derived class object.

Return 0;
}


5. Conversion from base class to derived class

Base Class Object Pointer (reference) can be forcibly converted to a derived class Object Pointer (reference), while base class objects cannot perform this type of conversion.
Downward transformation is not secure and there is no automatic conversion mechanism

// Demonstrate from the syntax that the base class object can be converted to a derived class object, but it does not make sense

1. Conversion constructor:
Manager (const employee & other): employee (other), Level _ (-1)
{

}

2. type conversion operators:

Employee: Operator Manager ()
{

Return Manager (name _, age _, deptno _,-1 );

}


Refer:

C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications

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.