C + + Primer notes 10-Inheritance

Source: Internet
Author: User


Package. inheritance, polymorphism C + + three basic concepts, here we focus on the inheritance of something


1 class derived List
The class derivation list specifies a derived class that inherits from the base class, from the list to one or more base classes such as:
Class B:public a1,protected a2,private A3
But the single inheritance is the most common, many inheritance is rare


2 definition of derived classes
When a derived class inherits, it includes all members of the parent class. Even private members cannot be interviewed. Virtual functions in a parent class are generally defined in a derived class, such as


If the fruit is undefined. The derived class inherits the virtual function of the base class


3 The base class must be already defined
A class that is declared only can not be derived from the class derivation list of today's derived classes. So the derived class is very likely to visit the list of base classes, but suppose the base class has not yet


is defined, so it is wrong to do so.








4 declaration of a derived class
The list of derived classes cannot be present in the list of derived classes, such as:
Class B:public A; //error
The correct statement should be:
Class B;
Class A;


5 Inheritance modifier
There are three ways to inherit from C + +. Public Protected,private Inheritance (abbreviated as 3p).



Regardless of the inheritance method, private members in the base class


cannot be visited by derived user code and member definition code. Let's talk about the following three ways of inheriting:
<1>private inheritance, all members of the base class are private members in the derived class, but the private members of the base class still cannot be interviewed
<2>protected inherits, the public,protected member of the base class becomes the protected member of the subclass, and the private member of the base class still cannot be interviewed
<3>public inheritance, the public of the base class becomes the public of the derived class. Protected becomes the protected of the derived class. The private of the base class cannot be interviewed


6 protected modifier
When protected modifies a class member, the class member can be used by the class's own member definition. However, it cannot be used by user code, which is similar to private
Second, the members of the protected decoration can be interviewed by the quilt category. This is similar to public, but not for private.






7 cannot be inherited member
Constructor, copy control member (copy constructor, assignment operator, destructor) This is a function that cannot be inherited


8 derived class constructor
derived class constructor general format:
Deriveclass (XXX): BaseClass (XXX), X1 (x), X2 (x) .... The constructor of the
<1> derived class initializes the members of the base class in addition to the members to which they are newly defined. The order is to first call the constructor of the base class to initialize the members of the base class. The new members are then initialized (in the order in which they are declared)
<2> assuming there are no custom constructors. The compiler synthesizes a default constructor, invokes the default constructor of the base class, and then initializes the other new members.
<3> note here: Suppose that the base class has no default constructor. The compiler would not be able to synthesize the default constructor, assuming that the default constructor for the derived class would not be defined at this time, then an error would occur. such as-"error C2512: Derive": No appropriate default constructor is available. "
<4> assumes that the constructor of the base class is not specified in the initialization list of the derived class constructor, and the default constructor for the base class is called
<5> the default argument derived class constructor can set all the parameters of the constructor to the default parameters, and can use the 0-n default arguments

9 replication control in a derived class
The copy control contains the copy constructor, assignment operator. destructor
<1> copy constructor not defined. The compiler will synthesize a copy constructor itself, calling the copy constructor of the base class
<2> defining the copy constructor itself, typically calling the copy constructor of the base class, or unexpected
<3> undefined assignment operator. The compiler will itself synthesize an assignment operator, which invokes the copy operator of the base class
<4> itself defines the assignment operator to beware of its own assignment. That's what this is all about.


Derive& Derived::operator =(Derived& rh)
{
if(this != &rh)
{
Base::operator=(rh);
//do whatever needed to clean up the old value
//assign the members from the derived
}
}

The reason for this is to prevent yourself from assigning values. When you assign a value to yourself. We usually have to clear out the dynamically opened memory first. Suppose not to add if (this! = &AMP;RH). Then when you make your own assignment, you will waste yourself (clear off yourself).
<5> derived class destructor
This is different from the first two, the destructor of the derived class is only responsible for the destruction of the newly defined object, and is not responsible for the destruction of the base class object. When a derived class object is refactored, the compiler invokes the derived class's own destructor and then calls the destructor of the base class.





Each class, whether a base class or a derived class, is only responsible for clearing its own members.
The above Copy Control Demo sample code looks like the following:


#pragma once
#include <iostream>
using namespace std;
class Base
{
public:
Base(){ m = 10;cout<<"Base default constructor"<<endl;}
Base(int x):m(x){}
Base(Base& rh){cout<<"the base copy constructor"<<endl;};
~Base(void){cout<<"Base destructor"<<endl;}
Base& operator = (Base& base){ this->m = base.m;cout<<"the base operator ="<<endl;return 


*this;} 
private:
int m;
};


class Derive :
public Base
{
public:
~Derive(void){cout<<"Derived destructor"<<endl;}
private:
int mx;
};


void main()
{
Derive X;
cout<<"this is the separtor-----------------"<<endl;
Derive Y = X;
Y = X;
}

Operation Result:
Base default constructor
This is the Separtor-----------------
The base copy constructor
The Base operator =
Derived destructor
Base destructor
Derived destructor
Base destructor


10 calls to derived class functions
In the case of inheritance. The scope of the derived class is nested within the scope of the base class, assuming that the name cannot be determined in the derived class, and the name is defined in the outer base class.
The <1> derived class calls the function as a principle: First look for the name in the derived class, and stop when you find it. What you can't find is found in the base class.
<2> static type, dynamic type
Static type: Refers to an expression type that is determined only by parsing the program text without regard to the execution period semantics of an expression. A static type depends only on the form of the program text that includes the expression, and does not change when the program executes
Dynamic type: The type of the finally derived object that is referenced by the Lvalue represented by an lvalue expression. The dynamic type of an rvalue expression. is the static type of it.
<3> the static type of the object, reference, or pointer determines the behavior that the object can run
Therefore a reference or pointer cannot run a newly defined member name in a derived class.
The <4> base class conflicts with the name of the derived class
When a base class has the same name as a derived class. A derived class masks a member of the same name in the base class. If you do not want to call a member of the base class, you must explicitly call it. such as: Base::func ();
<5> masking of base class member functions
The base class and the derived class have the same name function, but the prototype is different (the reference), then the derived class will not be able to call directly the function of the base class with the same name.





According to the previous principle, find the same name is no longer looking for the base class, so the following program is wrong.





class A
{
public:
void fun(){};
}
class B : public A
{
public:
void fun(int){};
private int x;
}
B b;
b.fun(11); //OK
b.func();  //error

One using
The ability to use using in a derived class to alter the level of the member inherited from the base class. The prerequisite is that the derived class has access to the members of the base class.
//Base.h
#pragma once
#include <iostream>
using namespace std;
class Base
{
public:
Base(void){ n = 100; };
~Base(void){};
size_t size()const{return n;}
protected:
//private:
size_t n;
int fn(int x){return x;};
int fn(){return 11;}
};

//Derived.h
#pragma once
#include "base.h"
class Derived :
private Base
{
public:
Derived(void){};
~Derived(void){};
using Base::size;
using Base::fn;
};

//main.cpp
#include "Base.h"
#include "Derived.h"
#include <iostream>
using namespace std;

void main()
{
Derived XX;
Base YY;
cout<<XX.size()<<endl;
cout<<XX.fn()<<endl;
cout<<XX.fn(2)<<endl;
system("pause");
}


Here we can also see that assuming that the function of the base class is overloaded, for the same using base::xxx, it is possible to make the base class function with the name XXX declared in the derived class.





Can really be described as a statement, multiple use.


The relationship between friend and inheritance
Friends are not related to inheritance. A derived class of a base class cannot be interviewed by a derived class of the base class.



The derived class of the friend class does not have access to the base class.


13 reference conversions, converting objects
Reference conversions: derived class objects are converted to references of base class types
Transform object: An object that is converted to a base class with a derived class object. This time the shape of the participation is fixed. Both compile and execute objects are base class type objects. The base class portion of the derived class is copied into the form.
Reference transformation: A reference to a derived class object that is converted to a base class. However, assuming the assignment is also to assign the base class part of the derived class to the base class object. This is a reason for the slicing effect of pointers.






14 Conversion of base classes and derived classes
A derived class-to-object assignment to a base class is also conditional talent, and the condition is that derived classes are inherited through public. This is either in the member code or in the user code. All can be achieved


Derived D; Base B = D;


Assuming that it is inherited through protected, it is only possible to use this code in the member definition.









15 Default Inheritance Criteria
Classes and structs are reversed by default inheritance guidelines.
<1> when a class inherits, assuming there is no qualifier, the default is private inheritance. If there is no structure at the qualifying time, it is the default public inheritance.
<2> also strut the default public modification defined between the first qualifier from the beginning to the inside. But the class default value is private modification.



C + + Primer notes 10-Inheritance


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.