C + + inheritance and derivation (1)

Source: Internet
Author: User
Tags access properties

C + + inheritance and derivation (1)

Http://www.cnblogs.com/CaiNiaoZJ/archive/2011/08/08/2130817.html

Inheritance is an important feature in object-oriented programming and a form of software reuse. In fact, inheritance and derivation are different statements of the same concept, usually we say that the subclass inherits from the parent class, and the parent class derives the child class. To tell the truth, after learning and understanding C + + inheritance and derivation, I found that I have a deeper understanding of inheritance and derivation in object-oriented. Before I get into the knowledge of C + + on inheritance, I'd like to talk about the basic concepts of derivation and inheritance in C + +, which is the basis for the construction and destruction of derived classes, and for multiple inheritance.

1. Declaration of a derived class

Declares a generic format for a derived class:

Class derived classes Name: [Inheritance Method] base class name

{

New data members and member functions for derived classes

};

For the above format, I would like to talk about the role of inheritance, it is mainly to specify how to access the members inherited from the base class. It can have the keywords private, protected, and public to represent private inheritance, to protect inheritance, and to inherit publicly. If the inheritance method keyword is not displayed, the system defaults to private inheritance. inheritance specifies that class-derived class members and out-of-class objects have access to members that inherit from the base class.

2. For different inheritance methods, the access properties of base class members in derived classes are also different, and the following table lists the access properties of the base class members in the derived class (which I want to illustrate in a table, both obvious and straightforward):

3. Finally, the access rules of derived classes to base class members are only accessed in two ways: internal access and object access. In the same 3 inheritance, the access of derived classes to base class members is also divided into 3 classes. For the 3 access rules, I will use the instance and summary one by one to correspond:

(1). Private inherited access rules:

Example:

#include "stdafx.h"
#include <iostream>

Class A
{
Private
int A1;
Protected
int A2;
Public
int A3;
A (int a1);
void ShowA ();
};

A::A (int A1)
{
this->a1=a1;
}
void A::showa ()
{
std::cout<< "a1=" <<this->a1<<std::endl;
}

Class B:private A
{
Private
int b;
Public
B (int b,int a1,int a2,int A3);
void Showb ();
};

b::b (int b,int a1,int a2,int A3): A (A1)
{
this->a2=a2;//can be accessed internally
this->a3=a3;//can be accessed internally
this->b=b;
}

void B::showb ()
{
std::cout<< "a1=" <<this->a1<<std::endl; inaccessible
ShowA ();//indirect access
std::cout<< "a2=" <<this->a2<<std::endl;
std::cout<< "a3=" <<this->a3<<std::endl;
std::cout<< "b=" <<this->b<<std::endl;
}


int main ()
{
b b (4,1,2,3);
b.a1=5;//not accessible outside class
b.a3=5; not accessible outside class
b.a2=5; not accessible outside class
B.showa (); out-of-class inaccessible
B.SHOWB ();

Return0;
}

Results:

The above example can summarize:

Private inheritance is a member of a base class that inherits from a derived class and becomes a private member in the derived class, so that the object of the derived class accesses the members in any base class (data member A1,a2,a3 and member function Showa ()) are not allowed, and the derived class has no access to the Private member (A1) in the base class. Accessible protected members (A2) and public members (data member A3, member function Showa ());

(2). Protection of inherited access rules:

Change the private inheritance in the example code above to protected

#include <iostream>
3
4 Class A
5 {
6 Private:
7 int A1;
8 Protected:
9 int A2;
Public:
int A3;
A (int a1);
void ShowA ();
14};
15
A::A (int A1)
17 {
this->a1=a1;
19}
void A::showa ()
21 {
std::cout<< "a1=" <<this->a1<<std::endl;
23}
24
25
Class b:protected A//Protection inheritance
27 {
Private:
int b;
Public:
to B (int b,int a1,int a2,int A3);
+ void Showb ();
33};
34
b::b (int b,int a1,int a2,int A3): A (A1)
36 {
PNS this->a2=a2;//Internal accessible
this->a3=a3;//can be accessed internally
this->b=b;
40}
41
B::SHOWB void ()
43 {
//std::cout<< "a1=" <<this->a1<<std::endl; inaccessible
ShowA ();//indirect access
std::cout<< "a2=" <<this->a2<<std::endl;
std::cout<< "a3=" <<this->a3<<std::endl;
std::cout<< "b=" <<this->b<<std::endl;
49}
50
51
int main ()
53 {
Si b b (4,1,2,3);
Not accessible outside the//b.a1=5;//class
//b.a3=5; out-of-class inaccessible
//b.a2=5; out-of-class inaccessible
//b.showa (); out-of-class inaccessible
B.SHOWB ();
60
Return0;
62}

The above example can be summarized (the run result is the same as the previous example):

Protect the members of the base class inherited by the derived class, except that the private base class members are immutable, in the derived class, to protect the members, so that the private members of the base class (A1), accessible Protected members (A2), and public members (data member A3, member functions Showa ()) are not accessible within the derived class Whereas an object of a derived class accesses a member of any base class (data member A1,a2,a3 and member function Showa ()) is not allowed, A1 is a private member and cannot be accessed, but A2,a3 and Showa () are inherited from the protected member in the derived class. They cannot be accessed because members of the protected protection type can be accessed by member functions of this class, or they can be accessed by a derived class member function of the class, but any access other than the class is not allowed, that is, it is semi-concealed.

(3). Access rules for public inheritance:

Example:

#include <iostream>

Class A
{
Private
int A1;
Protected
int A2;
Public
int A3;
A (int a1);
void ShowA ();
};

A::A (int A1)
{
this->a1=a1;
}
void A::showa ()
{
std::cout<< "a1=" <<this->a1<<std::endl;
}

Class B:public A//Public inheritance
{
Private
int b;
Public
B (int b,int a1,int a2,int A3);
void Showb ();
};

b::b (int b,int a1,int a2,int A3): A (A1)
{
this->a2=a2;//can be accessed internally
this->a3=a3;//can be accessed internally
this->b=b;
}

void B::showb ()
{
std::cout<< "a1=" <<this->a1<<std::endl; inaccessible
ShowA ();//indirect access
std::cout<< "a2=" <<this->a2<<std::endl;
std::cout<< "a3=" <<this->a3<<std::endl;
std::cout<< "b=" <<this->b<<std::endl;
}


int main ()
{
b b (4,1,2,3);
b.a1=5;//not accessible outside class
b.a2=5; not accessible outside class
b.a3=5;
B.showa ();
B.SHOWB ();

Return0;
}

Results:

The above example can summarize:

Public inheritance the members of the base class inherited from the derived class, in addition to the private base class members and protected base class members, become public members in the derived class, so that the derived class cannot access the private members (A1) in the base class, accessible protected members (A2), and public members (data member A3, The member function Showa ()), whereas an object of a derived class can access the data member A3 and member functions in the base class Showa (), but does not allow access to private members A1 and protected members A2.

4. Finally, summing up, in fact, this section of the content is in the final analysis of the inheritance of the way of understanding, and from the way the inheritance of the access attributes and the rules of the different differences. Because it seems to be a bit confusing to express it directly in words, try to explain it in the form and sample summary. I hope this will reach the reader will not feel confused when reading this essay!

C + + inheritance and derivation (1)

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.