The private inheritance and the public inheritance _c language in C + + programming

Source: Internet
Author: User
Tags access properties function definition inheritance

Private inheritance for C + + classes
When declaring a derived class, the inheritance of the base class is specified as private, and a derived class established in private inheritance is called a private derived class (the private derived class), and its base class is called the private base class.

The public members of a private base class and the protected member's Access property in the derived class are equivalent to private members in the derived class, that is, the member functions of the derived class can access them, and they cannot be accessed outside the derived class. Private members of a private base class become inaccessible members in a derived class, and only the member functions of the base class can reference them. The access properties of a base class member in the base class may be different from the Access property in the derived class. The access properties of the members of a private base class in a private derived class are shown in the following table.

The table does not need to be rote, just understand: Since the declaration of private inheritance, it means that the original can be outside the reference to hide the members, do not let outside reference, so the public members of the private base class and protection members are naturally private members in the derived class.

Private members of a private base class can only be referenced by a member function of the base class, but they are not accessible outside the base class, so they are hidden and inaccessible in the derived class.

The functionality of a class that does not need to be further inherited can be hidden by private inheritance, so that the next layer of derived classes cannot access any of its members. As you can see, the access attributes of a member in a different derivation hierarchy may be different, and it is related to the inheritance method.

Cases

Class Student1:private student//declares a derived class in private inheritance Student1
{public
:
  void Display_1 ()//output values of two data members
  {
   cout<< "Age:" <<age<<endl; Reference a private member of a derived class, correctly
   cout<< "address:" <<addr<<endl;
  } Reference a private member of a derived class, correct
private:
  int age;
  String addr;
};

Please analyze the following main function:

int main ()
{
  Student1 stud1;//defines an object for a Student1 class STUD1
  stud1.display ();//error, public member function of private base class is a private function
  in a derived class Stud1.display_1 ()//correct, display_1 function is the common function of Student1 class
  stud1.age=18;//error, the outside cannot refer to the private member of the derived class return
  0;
}

You can see:
You cannot refer to any member inherited from a private base class (such as Stud1.display () or stud1.num) through a derived class object such as STUD1.
A member function of a derived class cannot access a private member of a private base class, but can access a public member of a private base class (such as the Stud1.display_1 function can call the public member function display of the base class, but cannot refer to the private member num of the base class).

Many readers have asked such a question: data members such as Private member Mun of a private base class can only be referenced by a member function of a base class, while a public member function of a private base class cannot be invoked outside of a derived class, then is there a way to invoke a private base class's public member function to refer to private members of the private base? Yes.

It should be noted that although it is not possible to invoke a public member function of a private base class outside of a derived class object, a public member function of a private base class can be called by a member function of a derived class (it is a private member function in a derived class that can be called by any member function of the derived class).

You can override the member function definition of the private derived class above to:

void Display_1 ()//output value of 5 data members
{
  display ()://Call the common member function of the base class, outputting the value of 3 data members
  cout<< "Age:" <<age< <endl; Outputs private data members of derived classes
  cout<< "Address:" <<addr<<endl;
} To output a private data member of a derived class

The main function can be rewritten as:

int main ()
{
  Student1 stud1;
  Stud1.display_1 ();//display_1 function is the common function of the derived class Student1 class return
  0;
}

This allows you to correctly reference private members of a private base class. As you can see, this example uses the following methods:
Call the public member function stud1.display_1 in the derived class in the main function;
The public member function display of the base class is called through the public member function (it is a private function in a derived class and can be called by any member function in the derived class);
The data members in the base class are referenced through the public member function display of the base class.

According to the above requirements, to supplement and improve the above procedures to make it a complete, correct program, the program should include the function of input data.

Because private derived classes are too restrictive and inconvenient to use, they are not commonly used.

Common inheritance for C + + classes
When defining a derived class, the inheritance of the base class is specified as public, called a common inheritance, and a derived class established with public inheritance is called a common derived class (public derived class), and its base class is called the common base class (public).

When public inheritance is used, the public and protected members of the base class retain the properties of their public members and protected members in the derived class. While a private member of a base class is not a private member of a derived class in a derived class, it is still a private member of the base class, and only a member function of the base class can reference it. Instead of being referenced by a member function of a derived class, it becomes an inaccessible member in a derived class. The access properties of the members of the common base class in the derived class are shown in the table.

Some people ask, since it is a common inheritance, why not allow access to the private members of the base class? You know, this is an important software engineering perspective in C + +. Because private members embody the encapsulation of the data, hiding private members helps test, debug, and modify the system. If the access rights of all members of the base class are inherited intact to the derived class, so that the private members of the base class remain private in the derived class, and the derived class members can access the private members of the base class, does the base class and the derived class have no bounds? This destroys the encapsulation of the base class. If a derived class continues to derive a new derived class and access the private members of the base class, then the private members of the base class can be accessed at the level of all derived classes of the base class, which completely discards the benefits of encapsulation. Protecting private members is an important principle.

[Example] access to members of the public base class. The declarations section of the class is written below:

Class student//declares base class
{
public://base class common member
  void Get_value ()
  {
   cin>>num>>name>> sex;
  }
  void display ()
  {
   cout<< "num:" <<num<<endl;
   cout<< "Name:" <<name<<endl;
   cout<< "Sex:" <<sex<<endl;
  }
Private://The base class is privately member
  int num;
  string name;
  char sex;
};
Class Student1:public Student//To declare the derived class Student1
{public
:
  void Display_1 ()
  {
   cout << "num:" <<num<<endl; Attempt to reference a private member of the base class, error
   cout<< "Name:" <<name<<endl; Attempt to reference a private member of the base class, error
   cout<< "Sex:" <<sex<<endl; Attempt to reference a private member of the base class, error
   cout<< "Age:" <<age<<endl; Reference a private member of a derived class, correctly
   cout<< "address:" <<addr<<endl;
  } Reference a private member of a derived class, correct
private:
  int age;
  String addr;
};

Because private members of the base class are inaccessible to derived classes, it is not permissible to refer directly to the private data members num,name and sex of the base class in the Display_1 function in the derived class. A private data member of a base class can only be referenced through a common member function of the base class. You can change the declaration of a derived class Student1 to

Class Student1:public Student//To declare the derived class Student1
{public
:
  void Display_1 ()
  {
   cout< < "Age:" <<age<<endl; Reference a private member of a derived class, correctly
   cout<< "address:" <<addr<<endl; Reference a private member of a derived class, correctly
  }
private:
  int age; string addr;

The display function of the base class and the Display_1 function in the derived class are then called separately in the main function, which outputs 5 data.

You can write the main function (assuming there is already data in the object stud):

the int main ()
{
  Student1 stud;//defines the object Stud Stud.display () of the derived class Student1
  , and//calls the common member function of the base class, outputting the values of 3 data members in the base class
  stud.display_1 ();//Call a derived class public member function, output the value of two data members in a derived class return
  0;
}

According to the above analysis, write a complete program, the program should include the function of input data.

In fact, the program can also improve by calling the display function of the base class in the Display_1 function of the derived class, and writing one line in the main function:

  Stud.display_1 ();


You can output 5 data.

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.