C ++ derivative type

Source: Internet
Author: User

In public inheritance, the members of the public, private, and protected types of the derived classes can access the public members and protected members of the base class. The objects of the derived classes can access the public members of the base class. when private is inherited, the public, private, and protected member functions of the derived class can access the public and protected members of the base class, however, the object of the derived class cannot access any member of the base class. the Inheritance Method of protected is the same as that of private.

The base class public inherits protected inherits private inheritance.
Public member public protected private
Protected member Protected private
Private member ---------------

// Inherittest. cpp: defines the entry point of the console application.
//

# Include "stdafx. H"
Using namespace STD;

Class
{
Public:
Const static int A = 1; // if it is initialized during definition, it must be declared as const static
Protected:
Const static int B = 2;
PRIVATE:
Const static int c = 3;
};

Class B: public
{
Public:
B ()
{
D = B; // The subclass can internally access the protected member inherited from the parent class.
};
Public:
Int D;
};

Int _ tmain (INT argc, _ tchar * argv [])
{
 
A * obj1 = new ();
B * obj2 = new B ();

Cout <obj1-> A <Endl;
// Cout <obj1-> B <Endl; // The object cannot directly access the protected member from the outside.

Cout <obj2-> A <Endl;
Cout <obj2-> d <Endl;
// Cout <obj-> C <Endl;

// Getchar ();
Int I;
Cin> I;
Return 0;
}
 

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.