From c to C + + (top)

Source: Internet
Author: User

From the C language to the C + + language, it is a process-oriented and object-orientated programming leap. Object-oriented programming is characterized by inheritance and dynamic binding. C + + implements inheritance through the derivation of classes, supports dynamic binding through virtual functions, and provides some methods for encapsulating the implementation details of a class system.

Key concepts of object-oriented programming

Abstraction (abstraction)-removes unnecessary details from objects, preserves keywords that describe the essential characteristics of an object, and abstraction is a design activity.

Class--is a user-defined type, just like a built-in type such as Int. The class mechanism must allow the programmer to prescribe the operations that the class that it defines can do. Something inside a class is called a member of a class. In summary, a class is a set of data structures and a set of operations defined on that structure.

Object--A specific variable of a class that is also known as an instance of a class

Encapsulation (encapsulation)--combines types, data, and functions together to form a class.

Inheritance (inheritance)--allows classes to accept data structures and functions from a simpler base class. Derived classes can obtain the data and operations of the base class, overwrite them as needed, or add new data types and functions in the derived class.

—————————————————————————————————————————————————————————————————————————————

Abstract

Abstraction-is to observe a group of things, and realize that they have some common themes, and only record key data items that can represent the characteristics of things. When you do this, you are abstracting, and the data types you store are abstract data types. abstraction itself is the simplification of things .

Abstraction creates an abstract data type that C + + uses to implement this feature of class. It provides a way to look at encapsulation from top to bottom, observing the properties of the data type: Combining the various data structures and methods in the new user-defined class. It also provides a bottom-up view of encapsulation: combining various data and methods to implement a Yonghe custom type.

The benefits of abstraction:

    • Hide irrelevant details and focus on essential features.
    • Provides a black box interface to the outside. The interface determines the set of valid operations applied to the object, but does not provide internal implementation details.
    • Decompose a complex system into several separate modules. This allows for a clear division of labor and avoids the interaction between components that do not conform to rules.
    • Reuse and share code.

—————————————————————————————————————————————————————————————————————————————

Packaging

When you bundle the abstract data types with their operations, they are encapsulated. Non-OOP languages do not have a complete mechanism to implement encapsulation.

The current state of program design art is object-oriented language, which implements data integrity by bundling user-defined data structures and user-defined functions that operate on these data structures, and other functions cannot access these internal data.

—————————————————————————————————————————————————————————————————————————————

Access control

Access control describes who can access the data or functions that are declared next. Access control in C + + is as follows

    • Public declarations that are public are visible outside the class and can be set, invoked, and manipulated as needed. The general principle is not to make the data of the class public, because it is one of the object-oriented theories to keep the data of the class private, only the class itself can change its own data, and the external function can only invoke the member functions of the class, which guarantees that the data of the class will only be updated in a regular manner.
    • The contents of a protected declaration can only be used by functions of the class itself and by functions of classes derived from the class.
    • Private claims can only be used by member functions of the class,private declarations are visible outside the class, but do not have access to the
    • Friend is a friend whose function does not belong to the member function of the class, but it can access the private and protected members of the class like a member function of a class. A friend can be a function or a class.

(the latter two can only be used for one declaration at a time)

————————————————————————————————————————————————————————————————————————————

Statement

The declaration of a C + + class is the normal amount C declaration, which includes functions, types, or data. Classes bind them together, and each function in the class requires an implementation, and it is common practice to place them outside the class.

Class fruit{Public:peel ();        Slice ();    Juice ();        Private:int weight; int height; };
When a member function is implemented outside the class, a special prefix must be added::. "::" is referred to as the "global scope decomposition", followed by him is the scope of the search. If:: Front without an identifier indicates the lookup scope is global.


Calling member functions

The member function of an object that invokes a class is equivalent to the term "send an information to an object" used by the object-oriented programming language. Each member function has a this pointer, which is implicitly assigned to the function, which allows the object to refer to the object itself within the member function.

—————————————————————————————————————————————————————————————————————————————

Constructors and destructors

When an object of a class is created, the constructor is implicitly called, which is responsible for initializing the object. Corresponding to this, the class exists a cleanup function called a destructor. The destructor is called automatically when the object is destroyed (out of scope or delelte, and the heap memory used by it is reclaimed). Destructors are less common than constructors, and the code in it is typically used to handle special termination requirements and garbage collection. Destructors not only erase objects, they also clean up locks held by objects.

Constructors and destructors are very much needed because any function outside the class cannot access the private data members of the class, so you need a privileged function inside the class to create and initialize an object.

From c to C + + (top)

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.