Inheritance of C + +

Source: Internet
Author: User

The subclass object space is always not less than the base class object.

class father{

int A, b;

Public:

member functions

};

class Son:public father{

int C;

Public:

member functions

};

Default constructs:
If the child class does not have a constructor, the default constructor is called, and the default constructor instead calls the default parent class constructor to complete the construction of the parent object part.
If there is also a parent class above the parent class, recursion is recursive in turn.

Custom constructs:
To specify how the parent constructor is called instead of the default call, you need to customize the subclass constructor and describe the invocation form of the parent class constructor in the initialization list of the constructor definition body. The descriptive form is consistent with the description of the object member construct.

Overwrite (overlap):
The subclass defines the ancestor class (parent class, or parent class of the parent class ...). Members with the same name.

Class student{
Public
void display ();
// ...
};
Class Grastudent:public student{
Public
void display ();//overlap
// ...
};
VOID Fn () {
Grastudent GS;
Gs.display (); Callgrastudent::d isplay ()
}
The bundle subclass object accesses the member function, then the child class is first matched, then the parent class, then the parent class, and so on.

Copy construction:
Subclass if the copy constructor is not defined, the subclass object invokes the copy constructor of the parent class when the copy is created, and then completes its own bit-to-bit copy.
If the parent class does not have a copy constructor defined, the subclass object invokes the default copy constructor of the parent class in copy creation.
How to Inherit:

Inheritance can be publicly inherited, protected from inheritance and private inheritance.

Public inheritance is a common inheritance, and the base class can be used for most application services, or it can be inherited repeatedly.

The protection inheritance is "only Son" inheritance, inherits only to own descendants, the application is the public member function of the descendants to expand the service to the outside.

Private Inheritance is an "out-of-print" inheritance, which inherits only the immediate subclasses, regardless of whether the subclass is inherited anymore.

In the inheritance system, subclasses can adjust their access control properties in the scope visible to the ancestor class members.

class base{int B1;protected:int B2; void Fb2 () {b1=1;}  Public:int B3; void Fb3 () {b1=1;}};/    /-----------------------------------class Pri:private Base{public:void Test () {b1=1;    Error b2=1;    OK b3=1;   OK FB2 ();   OK Fb3 ();     OK}};//-----------------------------------class frompri:public pri{public:void Test () {b1=1;     Error b2=1;     Error b3=1;    Error FB2 ();    Error FB3 ();     Error}};//-----------------------------------class pro:protected base{public:void Test () {b1=1;     Error b2=1;     OK b3=1;    OK FB2 ();    OK Fb3 ();     OK}};//-----------------------------------class frompro:public base{public:void Test () {b1=1;     Error b2=1;     OK b3=1;    OK FB2 ();    OK Fb3 ();      OK}};//-----------------------------------class pub:public base{public:void Test () {b1=1;      Error b2=1;      OK b3=1; //OK FB2 ();     OK Fb3 ();      OK}};//-----------------------------------class frompub:public base{public:void Test () {b1=1;      Error b2=1;      OK b3=1;     OK FB2 ();     OK Fb3 ();  OK}};//-----------------------------------int main () {Pri priobj;   Priobj.b1=1;   Error priobj.b2=1;   Error priobj.b3=1;  Error Pro Proobj;   Proobj.b1=1;   Error proobj.b2=1;   Error proobj.b3=1;  Error Pub Pubobj;   Pubobj.b1=1;   Error pubobj.b2=1;   Error pubobj.b3=1;  ok}//====================================


Inheritance of C + +

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.