C + + class inheritance one (Access control)

Source: Internet
Author: User
Tags access properties

//Inheritance of Classes#include <iostream>using namespacestd;/*Parent-child relationship between inheriting classes in object-oriented 1. Subclasses have all member properties and member functions (including private member variables) of the parent Class 2. Subclasses are a special kind of parent Class 3. Subclass objects can be used as a parent class object 4. Subclasses can have methods and properties that the parent class does not have C + + Class member Access level (public,protected, Private) class member access level setting Principle 1. The member that needs to be accessed by the outside world is set directly to Public2. Members that can only be accessed in the current class are set to Private3. Members that are only accessible in the current class and subclass are set to protected,protected member access rights are between public and To judge the access rights of a class member variable from private to the third step 1. See if the call statement is written inside or outside of the subclass 2. See how subclasses inherit from parent class (Public,protected,private) 3. Look at the access level in the parent class (Public,protected,private) */classa{ Public:    intA;protected:    intb;Private:    intc;};classB: Publica{ Public:    voidTest1 () {a=Ten;//the public member of the parent class can be accessed inside the child classb = -;//the protection members of the parent class can be accessed inside the subclass//C = 20; Error: The private member of the parent class cannot be accessed inside the child class    }};classBt: Publicb{ Public:    voidTest12 () {a=Ten;//a property in Class B can be accessed--stating that the access level of a property is at least public or protectedb = -;//You can access the B attribute in Class B--stating that the access level of a property is at least public or protected//C = 20; Error: The private member of the parent class cannot be accessed inside the child class    }};voidProtect1 () {B B1; b1.a= One;//Public members of the parent class can be accessed outside the subclass//Proof: Public inherits the parent class, and the member of the parent class becomes the public member of the Child class//b1.b = 21; Error Description: Public inherits the parent class, and the protected member of the parent class becomes the protected member of the child class}classE |protecteda{voidTest1 () {a=Ten;//the public member of the parent class can be accessed inside the child classb = -;//the protection members of the parent class can be accessed inside the subclass//C = 20; Error: The private member of the parent class cannot be accessed inside the child class    }};classCt: Publicc{voidTest12 () {a=Ten;//You can access the A property in Class C--stating that the access level of a property is at least public or protectedb = -;//You can access the B property in Class C--stating that the access level of a property is at least public or protected//C = 20; Error: The private member of the parent class cannot be accessed inside the child class    }};voidProtect2 () {C C1; //c1.a = 10; Error: Error C2247: "A::a" is not accessible because "C" inherits from "A" using "protected"//Description protected inherits the parent class, and the public member of the parent class becomes the protected member of the child class//c1.b = 21; Error C2248: "A::b": Unable to access protected member (declared in class "A")//Description protected inherits the parent class, and the protected member of the parent class becomes the protected member of the child class}classB |Privatea{voidTest1 () {a=Ten;//the public member of the parent class can be accessed inside the child classb = -;//the protection members of the parent class can be accessed inside the subclass//C = 20; Error: The private member of the parent class cannot be accessed inside the child class    }};classDt: Publicd{voidTest12 () {//a = 10; Error C2247: "A::a" is inaccessible because "D" inherits from "A" using "Private"//member variables in D cannot be accessed by their subclasses, stating that a member property is a private member property of D//B = 20; Error C2248: "A::b": unable to access inaccessible members (declared in class "A")//the member variable in D cannot be accessed by its subclasses, stating that the B member property is a private member property of B//Description: Private inherits the parent class, the public member variable in the parent class becomes the private member variable of the child class, and the protected member variable of the parent class becomes the private member variable of the child class    }};voidMain () {System ("Pause");}/*the inheritance of C + + (public,private,protected) affects the external access property of the subclass public inheritance: The parent class member maintains the original access level in the subclass- that is, the public member of the parent class becomes a member of the child class. The protected member of the parent class becomes a protected member of the child class, and the private member of the parent class inherits from the quilt class, but is not a private member of the subclass that inherits: The parent class becomes a private member in the subclass- that is, the public member of the parent class becomes the private member of the child class. The protected member of the parent class becomes a private member of the child class, and the private member of the parent class inherits from the quilt class, but is not a private member of the subclass protected inherits: The public member of the parent class becomes protected-- , and the parent class becomes a protected member of the child class. The protected member of the parent class becomes a protected member of the child class, and the private member of the parent class inherits from the quilt class, but is not a private member of the subclass summary: A different inheritance may change the access properties of an inherited member */

C + + class inheritance one (Access control)

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.