C ++ encapsulation, inheritance, and Polymorphism

Source: Internet
Author: User

Encapsulation):Encapsulation is to combine abstract data with behaviors or functions to form an organic whole, that is, to organically combine data with the source code of operational data to form a "class ", data and functions are members of the class. The purpose of encapsulation is to enhance security and simplify programming. users do not have to understand the specific implementation details, but use class members through external interfaces and specific access permissions. By encapsulating some members to act as interfaces between the class and the outside, and hiding other members, this achieves reasonable control over the access permissions of members, reduce the mutual impact between different classes to a minimum, so as to enhance data security and simplify programming.


Inheritance:Inheritance is a concept in object-oriented software technology. If A Class B inherits from another class A, B is called the subclass of A, and A is called the parent class of B. Inheritance allows the subclass to have various attributes and methods of the parent class without having to write the same code again. When a subclass inherits the parent class, it can redefine some attributes and rewrite some methods, that is, overwrite the original attributes and methods of the parent class to obtain different functions from the parent class.


Polymorphism Polymorphisn ):Polymorphism is a technique that allows you to set a parent object to be equal to one or more of its sub-objects, the parent object can operate in different ways based on the features of the sub-objects assigned to it. To put it simply, assign a pointer of the subclass type to the pointer of the parent class. Polymorphism is implemented by Virtual functions in C ++. A virtual function is a member function that can be redefined by its subclass. The sub-class re-defines the parent class virtual function, called "Overwrite" or "override ).



The following is an example:

Inheritance:

Class BaseClass {public: int a; void test1 (); virtual void printFunc () {cout <"This is BaseClass. "<endl;} protected: int B; void test2 (); private: int c;}; class DerivedClassA: public BaseClass {public: void printFunc () {cout <"This is DerivedClassA. "<endl;} void testA () {cout <a <endl <B <endl <c <endl; // error} private: int d ;};

Access Control and inheritance Permissions

Access control:

In a base class, the public and private labels have common meanings: User code can be a public member of the category class but cannot access a private member. A private member can only be accessed by members and friends of the base class. The permission of the derived class to access the public and private Members of the base class is the same as that of any other part of the program: it can access the public member but cannot access the private member. Sometimes a class as a base class has some Members, and it wants to allow the derived class to access but still disallow other users to access these members. Such members should use protected access labels. A protected member can be accessed by a derived class object but cannot be accessed by a common user of this type.

Inherited permissions:

Public inheritance: The 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.

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

Private inheritance: all the members of the base class are private members in the derived class.


In the preceding example, the data members a and B of the base class are normal in the derived class, but an error is returned when c is used.


Polymorphism:

Function calls in C ++ do not use dynamic binding by default. To trigger dynamic binding, two conditions are met: first, only the member functions specified as virtual functions can be dynamically bound. The member functions are non-virtual functions by default, and non-virtual functions are not dynamically bound; second, you must call the function through a reference or pointer of the base class type. To understand this requirement, you need to understand what will happen when using a reference or pointer to an object of a certain type in the inheritance level.

You do not need to add the virtual keyword to the definition of the virtual function in the derived class, as long as the base class has it.

Now define another derived class:

class DerivedClassB : public BaseClass{public :    void printFunc(){    cout<<"This is DerivedClassB."<<endl;    }};


Test example:

Void main () {BaseClass * b1, * b2, * b3; BaseClass bc; DerivedClassA dcA; DerivedClassB dcB; b1 = & bc; b2 = & dcA; b3 = & dcB; b1-> printFunc (); // call the base class method b2-> printFunc (); // call the method b3-> printFunc () of the derived class (); // call the method of the derived class B}


Youyuan relationship and inheritance:


You can use the private and protected data of the metadata class. 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.


Class Base {friend class Frnd; protected: int I ;}; class D1: public Base {protected: int j ;}; class Frnd {public: int mem (Base B) {return B. i;} int mem (D1 d) {return d. I ;}// error inherit}; class D2: public Frnd {public: int mem (Base B) {return B. I ;}// error inherit };


Shielding and overloading

If an overloaded member is redefined in a derived class, only the Members that are redefined in the derived class can be accessed through the derived type.

If a derived class wants to use an overloaded version of its own type, the derived class must either re-define all the overloaded versions or one of them.

Sometimes classes only need to redefine the behavior of some versions in a reload set and want to inherit the meaning of other versions. In this case, it may be annoying to have to redefine each base class version to redefine a specific version.

The derived class does not need to redefine each inherited base class version. It can provide the using

Statement. A using Declaration can only specify one name, but cannot specify the form parameter table. Therefore, the using Declaration for the base class member function name adds all the overloaded instances of the function to the scope of the derived class. After all the names are added to the scope, the derived class only needs to redefine the functions that must be defined for this type, and the inherited definitions can be used for other versions.

Class BaseClass {public: void test1 () {cout <"BaseTest1" <endl;} virtual void printFunc () {cout <"This is BaseClass. "<endl;} private: int a;}; class DerivedClassA: public BaseClass {public: void printFunc () {cout <" This is DerivedClassA. "<endl;} void test1 (int t) {cout <" DerivedClassATest1 "<endl;} private: int B;}; void main () {BaseClass bc; derivedClassA dcA; bc. test1 (); dcA. test1 (1); dcA. test1 (); // error reported, blocked dcA. baseClass: test1 ();}

Through polymorphism, the base class can call blocked virtual functions. The base class pointer is used to point to the object of the derived class.





This article is from my study notes blog, please be sure to keep this source http://6924918.blog.51cto.com/6914918/1279707

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.