Preliminary study of inheritance relationships in C + +

Source: Internet
Author: User

 The inheritance mechanism is the most important means for object-oriented programming to reuse code, which allows programmers to extend and add functionality on the basis of preserving the original class characteristics . This produces a new class, called a derived class. Inheritance presents the hierarchical structure of object-oriented programming, which embodies the cognitive process from simple to complex.
#include <<span style= "FONT-SIZE:14PX;" >iostream</span>>using namespace Std;class base{public:base ()    {}~base () {}private:int _pri; Protected:int _pro;public:int _pub;}; Class Derive:public base{public:derive () {}~derive () {}private:int _dpri;protected:int _dpro;public:int _dPub;}; int main () {cout << sizeof (Base) << endl;//12=4+4+4cout << sizeof (Derive) << endl;//24, The size of the derived class contains the base class size and its own size of return 0;}

Inheritance relationships and Access qualifiers:

Class Base{public:base () {}~base () {}private:int _pri;protected:int _pro;public:int _pub;}; Class Derive:p ublic base{public:derive () {}~derive () {}void Display () {cout << _pri= << _pri << endl;/ Private members of the/error base class cannot be accessed in derived classes cout << _pri= << _pro << endl;cout << "_pri=" << _pub << Endl ; cout << "_dpri=" << _dpri << endl;cout << "_dpri=" << _dpri << endl;cout << "_d Kpri= "<< _dpri << Endl;} Private:int _dpri;protected:int _dpro;public:int _dpub;}; int main () {Derive D;d._pri = 10;//errord._pro = 20;//error

D._pub = 30;
D._dpri = 20;//errord._dpro = 20;//errord._dpub = 20;}
<span style= "FONT-SIZE:12PX;" > Summary:</span>
1. A private member of a base class cannot be accessed in a derived class, and is defined as protected if the base class member does not want to be accessed directly outside the class, but needs to be accessible in the derived class. You can see that the protection member qualifier occurs because of inheritance.
2.public inheritance is an interface inheritance that maintains the is-a principle, and the members that are available to each parent class are also available to the child class, because each subclass object is also a parent class object.
3.protetced/private inheritance is an implementation inheritance, some members of a base class are not completely part of the subclass interface, and are the principle of has-a relationships, so the two inheritance relationships are not used under special circumstances and are used in most scenarios as public inheritance.
4. Regardless of the inheritance method, the public and protected members of the base class can be accessed inside the derived class, and the private members of the base class exist but are not visible in the subclass (inaccessible).
5. The default inheritance method when using the keyword class is private, and the default inheritance method when using a struct is public, but it is best to show the inheritance in the way it is written.
6. In the actual application of the general use is the public inheritance, very few scenarios will use Protetced/private inheritance.

In a derived class, if the six member functions of the definition class are not displayed, the compiler system will default to the six default member functions

<span style= "FONT-SIZE:12PX;" >class base{public:base () {cout << "Base ()" << Endl;} ~base () {cout << "~base ()" << Endl;} Private:int _pri;protected:int _pro;public:int _pub;}; Class Derive:p ublic base{public:derive () {cout << Derive () << Endl;} ~derive () {cout << "~derive ()" << Endl;} Private:int _dpri;protected:int _dpro;public:int _dpub;}; int main () {{Derive D;} GetChar (); return 0;} </span>

Order of constructor calls in an inheritance relationship: base class constructor = "Object constructor in derived class =" derived class constructor body

Inheritance Relationship Order of the destructor calls in: derived class destructor = "derived class contains member object destructor =" base class destructor

Description: 1, the base class does not have a default constructor, the derived class must explicitly give the base class name and the parameter list in the initialization list.
2. If the base class does not have a constructor defined, the derived class can also use the default constructor without defining it.
3. The base class defines the constructor with the formal parameter list, and the derived class must define the constructor function.

default constructor : Refers to the constructor that does not need to provide arguments at the time of invocation, so: it can be no parameter, or all parameters have a default value, the two take one, otherwise there is two semantics.

The compiler synthesizes the default (default) constructor: 1. When a class has a member of a class type, and the class member has a default constructor;

2. When the base class has a default constructor, the derived class does not have a constructor function;

3. When there are virtual functions in a class or in a class derivation chain;

4. When a class takes a virtual inheritance method.


Call the copy constructor: 1. When initializing another object with one object, it is a homogeneous object; (object Build Object)

2. When a function parameter is an object, a copy of the parameter is called by the copy constructor when the function is called through the object;

3. When the return value of a function is an object, when the function is called, returning an object invokes the copy constructor. (Value return value)

The compiler synthesizes the copy constructor :1. When a class has a member of a class type, and the class member has a copy constructor;

2. When the base class has a copy constructor, the derived class does not have a copy constructor;

3. When there is a virtual function in the class;

4. When a class takes a virtual inheritance method.


Scopes in the inheritance system:

1. In the inheritance system, base classes and derived classes are two different scopes.
2. Subclasses and parent classes have members of the same name, and child class members will mask direct access to members of the parent class. (in subclass member functions, you can use the base class:: base class member access)--hide--redefine
3. Note that in practice it is best not to define a member with the same name in the inheritance system.

Inheritance and transformation--assignment compatibility rules--public inheritance : 1. Subclass objects can be assigned to parent objects (cut/slice)
                                       2. The parent class object cannot be assigned to a subclass object
                                       3. A pointer/reference to a parent class can point to a subclass object
                                       4. A pointer/reference to a subclass cannot point to the parent class object (can be done by forcing type conversion)

Class base{public:base (int data) {}~base () {}private:int _pri;protected:int _pro;public:int _pub;static int count;}; int base::count = 0;class derive:public base{public:derive (): Base () {}~derive () {}private:int _dpri;protected:int _ Dpro;public:int _dpub;int _pri;}; int main () {Derive D; Base B (0); b = D;d = B;//error, the parent object cannot be assigned a value to the subclass base* PBase = &d;derive *PD = (derive*) &b;//can be assigned a value of return 0 by forcing the type conversion;}


Friend and inheritance: A friend relationship cannot inherit, that is, the base class friend cannot access the subclass private and protected members.
inheritance and static members : The base class defines a static member, and there is only one such member in the entire inheritance system. No matter how many subclasses are derived, there is only one static member instance.

Single inheritance: A subclass has only one direct parent class.

Multiple inheritance: A subclass of at least two parent classes.

Diamond Inheritance: Two semantic and data redundancy issues are generated.

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:12PX;" >class person{public:string _name;   Name};class student:public person{protected:int _num;   Study number};class teacher:public person{protected:int _id;     Employee number};class Assistant:public Student, public teacher{protected:string _majorcourse;     Major course};void Test () {//Displays the member that specifies which parent class to access assistant A;a.student::_name = "XXX"; a.teacher::_name = "yyy";} </span>
<span style= "color: #ff0000;" > To solve the two semantic and data redundancy problems of diamond inheritance, use virtual inheritance </span>.
<pre class= "CPP" name= "code" >class b{public:int data1;}; Class B1:virtual public b{public:int data2;}; Class B2:virtual public b{public:int data3;}; Class D:p ublic B1, public b2{public:void funtest () {B1::d ata1 = 0x01;data2 = 0x02; B2::d ata1 = 0x03;data3 = 0X04;DATA4 = 0x05;cout << this << endl;cout << &b1::d ata1 << Endl;cou  T << &data2 << endl;cout << &b2::d ata1 << endl;cout << &data3 << endl;cout << &data4 << endl;cout << data1 << Endl;} int data4;}; int main () {cout << sizeof (B) << endl;cout << sizeof (B1) << endl;cout << sizeof (B2) << Endl;cout << sizeof (D) << endl;d d;d.funtest (); GetChar (); return 0;} 
</pre></p><p></p><pre>

Preliminary study of inheritance relationships in C + +

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.