C + + Manuscripts: encapsulation and inheritance

Source: Internet
Author: User

Level of visibility

The C + + class provides the encapsulation of data structures and algorithms, and the corresponding 3 levels of visibility. They define different visibility:

    • Public: visible in methods of the current class and subclass, externally visible.
    • Protected: The current class and the methods of the subclass are visible and outside is not visible.
    • Private: The method of the current class is visible and external is not visible.

In a member function of an object, you can call private methods of other homogeneous objects.

In most modern object-oriented languages, only private and public two visibility is provided, and the visible level of C + + is slightly more complex. However, three ways of inheritance and multiple inheritance mechanisms make the problem more complicated. For simplicity, only single inheritance of private and public methods is discussed here.

  • Public inheritance: The subclass can access the base class public , protected the member, and the child class is externally visible.
  • Private inheritance: A subclass can access a base class public , protected a member, and a subclass that is not visible outside the child class.
Inheritance of Classes

Public inheritance represents a "is a" relationship, and the object of a subclass is also an object of a base class. The behavior of subclasses should conform to the behavior of the base class, so the base class members are not usually overwritten in public inheritance.

For public inheritance, pointers, references, variables of subclasses can be directly assigned to pointers, references, and variables of the base class.

Private inheritance means "to ... Implementation, the subclass is implemented as a base class. For objects of a subclass, the behavior of the base class is not visible externally. Private inheritance is more like an object combination.

class CBase{};class CDerived: public CBase{public: CDerived(): CBase(){}};
Member Overrides

A member declared in a subclass overrides a member of the same name in a base class, but can be called by a base class name:

class CBase{ Public:    int I;};class cderived:  Public CBase{ Public:    int I;    void func(){        CBase::I = 1;    }};

Similar to member variables, the base class's member functions also need to be called through the base class name.

Closed class

A class with member objects is called an enclosing class , which is an implementation of the object combination. can be initialized in the constructor's initialization list.

class CPerson{    Chead Head;    Cbody Body; Public:    CPerson(Head_, Body_):Head(Head_), Body(_body){}};

Obviously, if the constructor parameter of an object member is not NULL, the current class is required to specify the constructor.

You can also assign a member object to a constructor without specifying an initialization list, which causes the member object to be constructed multiple times. However, because members of constant types, reference types do not accept assignments, they can only be initialized in the initialization list.

Construction and destruction process

Object, the constructor of the parent class is completed first, the member object is constructed, and the constructor of the current class is called:

    1. Constructs an object of the parent class. In this procedure, the dynamic type of the object is still the parent class.
    2. Constructs an object property. The order in which they are instantiated depends only on the order in which they are declared in the class, regardless of the order in the initialization list.
    3. Call the constructor. The construction process specified in the current class is completed here.

The object's destructor is reversed, first invoking the destructor of the current class, then destructors the object's properties, and finally the parent class object.

Friend Yuan

There is a syntactic feature in C + + that destroys encapsulation: friend. However, the extension and operator overloading of classes can be better implemented with friends. Specifically, it includes friend functions and friend classes, which can access private members.

The relationship between friends is not transitive and inherited.

The friend function adds a keyword to the class declaration friend , which can be manipulated by passing arguments to an instance of the class. A friend function can also be a member function of another class.

class CPerson{    int  Age; Public:    friend void Setage(CPerson&, int);    friend void Cgod::Createman();};void Setage(CPerson& P, int N){    P. Age = N;}

The logic of a friend class is relatively simple, giving the friend class in the declaration, giving it private permissions:

class  cperson  { public  :   Friend  class  god   };   

Unless noted, this blog article is original, reproduced please link to the form of this article address: http://harttle.com/2015/06/29/cpp-encapsulation-and-inheritance.html

Copyright NOTICE: This article is for bloggers original articles, reproduced please attach the original link.

C + + Manuscripts: encapsulation and inheritance

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.