Object-oriented design _c++1

Source: Internet
Author: User

Proficiency in a language is not for me, I like to quickly master a language of 50%.


The three main features of object-oriented programming are encapsulation, inheritance and polymorphism. Relative and process-oriented, encapsulation implements the separation of the interface and implementation of the class, where the interface is a function that can be used by the user, the implementation includes the private function, the private data, and so on, inherits, implements the code reuse, the polymorphism is the core of object-oriented, and realizes the reuse of interface through virtual function, which increases the convenience and


The members of a class consist primarily of data members and member functions, or they may have type members, such as: Typdef int num, the size of a class object, the size of a non-static data member, and static members stored in the global data area. member functions, like procedure-oriented functions, are also stored in the code area. Only each member function has a hidden this parameter, and the type of this parameter is a * const, which is a constant pointer to the class type. When a member function is called through a class object, the address of the object is assigned to the This parameter, and the operation of the member function for non-static data members in the class is implemented through the this pointer.

In the const function, the const modifier is the This parameter.


There are several special functions in a class that the compiler defines for us if the user does not customize these functions. These include: constructors, copy constructors, assignment operators, and destructors.

Functions: Parameters of a generic type can be passed through a register, the argument is assigned to some particular register, then the code that invokes the function is transferred, and the Register is assigned to the parameter of the calling function. The return value is also the same, after the function is executed, the return value is assigned to a register, and then back to the function call location, continue to execute, if you want to use the return value, the value of a particular register to assign a variable, if not, the register can be used by the program, the value of the inside does not know when will be overwritten If you pass a class, struct, and so on, the parameters and return values are passed through some space in memory.

The reference is converted into a pointer operation.

Constructor: The same as the class name, no return value, implementation of the class member initialization. It is best to initialize the initial value list, and if the function body is initialized, the data member has completed the default initialization, and the function body is assigned to the data member.

Copy constructor: The first argument is the constructor of a reference to its own class type. Used to initialize the object through =, and the parameters and return values are non-reference, the parameter is passed and the return value is returned. When a class object is initialized by (), the compiler chooses the most appropriate constructor, or it may choose to copy the constructor.

Assignment Operator: Overloads the = operator. The overloaded operator is essentially a function, and the assignment operator is a function named operator=. The general form is:a& operator= (const a&);

destructor: The function name consists of a wavy line plus a class name, with no return value and no parameters. Includes the function body and the destructor part (hidden), executes the function body first, then executes the destruction part, and the opposite of the constructor function. When a member of a class type is destroyed, a destructor is called, the built-in type does not have a destructor, and nothing is done when it is destroyed.

Object-oriented design _c++1

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.