Thinking of C + + data access statement

Source: Internet
Author: User

First, we all know that for the base class of private data or interface, regardless of the inheritance, this data forever belongs to this base class, only this one belongs to the object defined by this base class, of course, for this base class object this data can be more than one copy, but for other classes outside the class is only a copy, You can never share to a derived class. Derived classes can only be accessed through the methods of the base class. (This is not a discussion of the friend function, temporarily ignored)

Second, for all data, if private inheritance is used, regardless of the access attribute of the data or interface in the base class, it becomes the private member of the base class after this private inheritance.

Down the public data for the base class if the public attribute is used or is a public property in the derived class, if the use of protected inheritance becomes a protected property in the derived class, only the public interface can be used in the derived class to access

The protection data for the base class is protected if the use of public inheritance is still in the derived class, if protection inheritance is used or remains unchanged

Down to say a variety of inheritance, for private inheritance is like telling a derived class my side of the thing (data or interface) only I this copy, you want to copy, do not give, but I can allow you to read or write the operation through the data interface I provide you to operate. For the protection of inheritance is to tell the derived class my side of this thing (data or interface) I can copy one to you, but you must cherish this I give you the thing, and my data access rights you can not change, must have protected him in the past. For public inheritance is all I have in public and I give it to you, you have to make these public in the past, but the original properties of my data cannot be changed.

Again, protect data and private data. In the design of the time has been taken into account, in order to maintain data security and sharing is the emergence of public data and private data between the protection of data, private data is only one of its own class, other classes want to use the first I agree to, must go my interface, and the protection of data is inherited classes can and accumulate common enjoyment of private data , no matter how many times I have been derived, my heart is still the same when you use me as your private data, ah Yo, said a little sensational!!!


Before we know that for public inheritance all of the base class public interfaces are also the public interfaces of derived classes, you can directly call the interfaces of the base class directly using the objects in the derived class, but the protection inherits all of the base class public interfaces into the protected types in the derived class. In this way, the derived class object can access the public interface of the base class through the public interface in the derived class, and for private inheritance, the public interface in the base class becomes private data in the derived class, and the public interface of the base class must be accessed through the public interface of the derived class (here I only say that the interface for the class is not said to Data must be found)

So what I'm saying here is that the access statement is a workaround for the public interface to access the base class in the above protection inheritance and private inheritance, which reduces the public interface in the derived class that constructs a public function to access the base class, and the following is the case of the private inheritance as a matter of fact, protecting inheritance is the same.

/********************************************************************** * * Copyright (c) 2015,wk Studios * * filename:a.h * * COMPILER:GCC VC 6.0 * * AUTHOR:WK * * TIME:2015 26 6 * * * * /#include <iostream>using namespace std; Class a{public:a (int x=int (), int x1=int (), int x2=int ()):p _data (x), Data (x1), m_data (x2) {}void Show (int x) const{cout <<data+m_data-x<< "\ n";} void Show () {cout<< "Data of Protected:" <<data<< "Data of Private:" <<m_data<< "\ n";} int P_data;protected:int Data;private:int m_data;};/ /want to access the public data and interfaces of the base class in private inheritance, you can use Access claims, only give the declared data or interface a backdoor access, protect the data security class B:p rivate a{public:b (int x=int (), int x1=int (), int X2=int ()): A (x,x1,x2) {}a::show;//This is just a match to the function with the function name as an overloaded function, note that this only requires the functions name a::d ata;//data and function what is the original property of the Access property using the access declaration in the derived class or the original attribute};  void Main () {B B1 (n/a);  B1.show (); B1.show (1);  cout<< b1.data<<endl;; }



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Thinking of C + + data access statement

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.