Basic records of c ++ classes

Source: Internet
Author: User

 

1 virtual is used to enable dynamic binding. In addition to the number of constructors, any non-static member function can be a virtual function. Virtual only appears in the member function declaration of the class and cannot be used in the function definition that appears outside the lamp definition body.

2. public members are allowed to access and rivate is not allowed. They can only be accessed by members and friends of this class.

Like a private member, protected members cannot be accessed by class users.

Like a public member, a protected member can be accessed by a derived class of the class.

The base class is specified in the class 3 derived list. The base class can be one or more classes.

4. Generally, a derived class must redefine the inherited virtual function.

5. The derived class object contains the base class object as the sub-object.

The (non-static) member defined by the derived class and the child object of the base class (non-static) member.

A function in a derived class can use a member of the base class.

The derived class can access the public and protrcted members of the base class, as if they were their own members.

The derived class can be used as the base class.

The declaration of a derived class contains the class name but does not include the list of derived classes.

For example:

// Error d forward declaration must not include the derivation list

Class Bulk_item: public Item_base;

// Forwaed declarations of bath derived and nonderied class.

Class Bulk_item;

Class Item_base;

 

6. Transition from a derived class to a base class. You can use the address of a derived type object or reference to assign values or initialize pointers or references of the base class type. But strictly speaking, the derived type object is not converted to the base class type object.

7. The Inheritance level base class itself specifies the minimum access control for its own members. If a member is private in this class, the member can be accessed by the friends of the base class and base class. A derived class cannot access the private Members of the base class, nor can it use its own users to access those members. If the base class member is public or protected, the access label used in the derivation determines the access level of the member in the derived class.

The common inheritance base class members maintain their own access level. The public Member of the base class is the public Member of the derived class, and the protected member of the base class is the protected member of the derived class.

The public and protected members of the protected inheritance base class are protected members in the derived class.

All the members of the private inheritance base class are private members in the simplified tutorial class.

 

8 removing a derived class from an individual member can restore the access level of the inherited member, but it cannot make the access level more strict or loose than the original one specified in the base class.

For example:

Class Derived: private Base {

Public:

// Maintain access levels for members related to the size of the objct

UsingBase: size; // size is public in Base, but private in Derived.

Protected:

Using Base: n;

}

 

9 The derived class defined by class has private inheritance by default. The classes defined by struct are inherited by public by default. This is also true for the inheritance relationship.

 

10 friends can use the private and protected data of the category.

The relationship between friends and friends cannot be inherited. The base class's friends do not have special access permissions to the members of the derived class. If the base class is granted a friend relationship, only the base class has special access permissions. The derived class of the base class cannot access the class that grants the friend relationship.

If the derived class wants to grant access to its members to the friends of its base class. The derived class must explicitly do this: the Friends of the base class do not have special access permissions to the base class. Similarly, if both the base class and the derived class need to access another class, the class must grant the access permission to the base class and each derived class.

11 If a base class defines a static member, there is only one such member in the entire inheritance hierarchy. No matter how many derived classes are derived from the base class, each static member has only one instance.

Static members follow regular access control: if the member is private in the base class, the derived class cannot access it.

 

12 conversion from a derived class to a base class if there is an object of the derived type, you can use its address to assign values or initialize pointers of the base class type. (The pointer defining the base class can point to the derived type)

You can also use references of the derived type or references of the object base class type.

13 The conversion referenced is different from the conversion object.

 

-- C ++ Primer

 

 

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.