Concept of C ++ encapsulation, inheritance and Polymorphism

Source: Internet
Author: User

 

From: http://www.cppblog.com/Lee/articles/56247.html

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, form a "class", where 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.

For example, on the basis of abstraction, We can encapsulate clock data and functions to form a clock class.
According to the C ++ syntax, the clock class declaration is as follows:

Class
Clock
{
Public:
// Shared members, external excuses
Void settime
(Int
Newh,
Int newm
, Int news
);
Void showtime
();
PRIVATE:
// Private member, which cannot be accessed from outside
Int hour
, Minute
, Second;
}

We can see that some members act as class and external interfaces through encapsulation, while other members are hidden, thus achieving reasonable control over Member access permissions, reduce the mutual impact between different classes to a minimum, so as to enhance data security and simplify programming.

What is polymorphisn )? Literally, it means "multiple shapes ". Reference Charlie Calverts's description of polymorphism-polymorphism is a technology 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 assigned to its sub-objects (from "delphi4 Programming Technology insider "). To put it simply, you can assign a pointer of the subclass type to a pointer of the parent class. Polymorphism is implemented by virtual functions in Object Pascal and C ++.

Okay, followed by "virtual functions" (or "Virtual Methods "). A virtual function is a member function that can be redefined by its subclass. The sub-class redefinition of the parent class virtual function is called override or rewrite ".

Inheritance is a concept in object-oriented software technology. If a Class A inherits from another class B, it is called "B's subclass" and B is called "A's parent class ". 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.

Inheritance means that an object directly uses the attributes and methods of another object. In fact, many entities we encounter have the meaning of inheritance. For example, if a car is regarded as an entity, it can be divided into multiple entities, such as trucks and buses. These fruit bodies all have the characteristics of automobiles. Therefore, automobiles are their "Fathers" and these fruit bodies are the "children" of automobiles ".

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.