C ++ Inheritance

Inheritance: born dragon, phoenix raw chicken, mouse son can make hole.


sizeof can verify that inherits all the member variables of the base class

public's inheritance, combined with subclass object to call the base class member function, you can get the members of the base class subclasses (all resources are inherited. public members of the base class in a subclass authority is public.)

 

#include<iostream>
using namespace std;

class Base {    //父类
public:
void Set(int pri,int pro,int pub) {
_pri = in;
_pro = pro;
_pub = pub;
}

void Print() {
cout << _pri << " " << _pro << " " << _pub << endl;
}
int _pub;
protected:
int _pro;
private:
int _pri;
};

class SON1: Base     // subclass // no inheritance 
{
};

class Son2: public Base     // subclass // public inheritance 
{
 public :
 void the Set ( int PRI, int Pro, int Pub) {

_pri = PRI; // sentence error: access private member variables in a subclass can not access. 

_pro = Pro; // protection can be accessed 
_pub = Pub;
}

};



int main () {
cout << sizeof (SON1) << endl; // size is 12; the member variables can inherit

Son1 s;
s.Set();
s.Print ();     // This error, because the need for inheritance 

System ( " PAUSE " );
 return  0 ;
}

 


Inheritance:


public、private、protected

1.public: All resources are inherited. Base class public member variable in the subclass rights are also public ; the base class protection of member variables in a subclass to access their access to direct and protection , but not outside the class; the base class private member variable in a subclass Yes in the presence of but I can not see. (Descendants want to know, do not want to let others know so protection can be accessed, can not access private.)
2.protected: base class public member variables change permissions in a subclass, is Protect (access classes, class outside You can not access); the base class protection of member variables in a subclass of access is protected , but not outside the class; the base class private member variable in a subclass Yes there is, but can not see .
3.private: base class public member variables occurring in the authority changing subclass is Private (access class, the class can not be accessed outside); protected base class member variable in the subclass access isProtect , but not outside the class; the base class private member variable in a subclass Yes Yes present but not visible

 

 

 

 

The practical application of almost all public. (FIG permission member is the base class)

Assignment-compatible with the rules:

Subclass object can be used as a parent class object to use;

Subclass object can be assigned directly to the parent object;

Subclass object can initialize direct parent class object;

Parent pointer may point directly to subclass object;

Parent class directly references subclass object reference;

After the substitution, the derived class object can be used as an object using the base class, but can only use the inherited from the base class members.

Subclass is a special parent class. Member variables and member functions of the same name are distinguished by the scope resolution operator.

Determining a sentence, can be accessed:

1. Look at the call statement, these words written on the inside subclass, external

2. How to Look subclass inherits from the parent class (public, private, protected)     

3. See the access level (public, private, protected) in the parent class
how to properly use public, protected and private access level for the members of the statement:

1. The need to be a member of the outside world directly accessible to public
2. only be set to private access to the members of the current class of
3 members can only be set in the current class and subclass access to access protected, protected members of the media between public and private.


With the public in general.


 

 

Inherited constructor and destructor:

How to initialize the parent class members? Constructors parent class and subclass What is the relationship?

1. In a subclass object construction, need to call the parent class constructor initializes its members come inherited
member 2. In a subclass of object destruction, need to call its parent class destructor inherited come clean


Destructor call configuration principle of inheritance
1. When creating a subclass object will first call the constructor of the superclass
2. After performing the parent class constructor constructor executes subclass
3. When there is the parent class constructor parameters , the need to display the list of call initialization subclass
opposite 4. destructor calls the constructor and the order


Multiple inheritance:
a plurality of class inheritance relationship immediate base class is called a multiple inheritance.

Format: class derived class name: the name of a base class access control, access control, the base class name 2, ..., n access control base class name

 

Multiple inheritance and access the derived class constructor:

1. The derived class constructor plurality of base classes may be the initial call the base class constructor initializes the data members of the
order of execution of the single inheritance constructor 2. Similar circumstances. The order of succession of the base class specified when a plurality of base class constructor direct execution sequence depends on derived classes.
3. A derived class object has a plurality of directly or indirectly, a member of the base class. Different members access ambiguity does not occur. If different members of the same name as the base class, the derived class object should be identified for access.


Virtual inheritance:
If a derived class is derived from multiple base classes, and the names of the base classes have a common base class is declared in the base class to access, it may produce ambiguous.

 

Friend functions:
1. The friend function is not a class member functions, can not be inherited;
2. friend relationship is unidirectional, not commutative; if the B class is a friend class A, class A is B is not necessarily class friend
3. friend relationship can not be transferred, if class B is a friend of class a, class C is a friend class B, class C necessarily meta class is a friend a


Friend function Note:

1. The members of the private member functions can access the class friend function, but not class; 
2. const friend function can not be modified; 
3. friend function can be defined anywhere in the class declaration, the class without access restrictions qualifier ;
4 may be a function of a plurality of classes friend function; 
the same principles calls and ordinary function call 5. friend function; 
6. friendship is not inherited, the base class is not a friend of the members of the derived class special access. If the base class is awarded a friend relationship, only the base class with special access rights. The base class of the derived class can not access grant friendship to the class.

class Base
{
friend void fun();
public:
Base(int a = 0, int b = 0, int c = 0)
: _pub(a)
, _pro(b)
, _Pri (c)
{}
int _pub = 1;
protected:
int _pro = 2;
private:
int _pri = 3;
};
class Derive :public Base
{};
int main ()
{
Derive d1;
d1.fun();
return 0;
}


The result is given: function fun function is not a member of the derived class, explained friend function can not be inherited.

 

Guess you like

Origin www.cnblogs.com/lonelyprince7/p/11668322.html