Object-Oriented Design _ C ++ 1 and object-oriented design _ c

Source: Internet
Author: User

Object-Oriented Design _ C ++ 1 and object-oriented design _ c

Mastering a language is not suitable for me. I like to quickly master 50% of a language.


Three main features of object-oriented programming are encapsulation, inheritance, and polymorphism. Relative to the process-oriented model, it encapsulates and implements the separation of class interfaces and implementations. interfaces are functions that can be used by users, including private functions and private data. Inheritance, code reuse is realized. polymorphism is the core of object-oriented, and interface reuse is realized through virtual functions, increasing the convenience and diversity of programming.


The class members mainly include data members and member functions, and may also have type members, such as typdef int num. The class object size is the size of non-static data members, static members are stored in the global data zone. Like process-oriented functions, member functions are stored in the code area. However, each member function has A hidden this parameter. 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. Operations on non-static data members in the class in the member function are implemented through the this pointer.

In the const function, const modifies this parameter.


There are several special functions in a class. If you do not customize these functions, the compiler will define them for us. It includes constructor, copy constructor, value assignment operator, and destructor.

Function: Generally, parameters of the type can be passed through registers. Real parameters are assigned to some specific registers first, and then jump to the code that calls the function. Then, the registers are assigned to the form parameters that call the function. The return value is also the same. After the function is executed, the return value is assigned to a register, and then return to the function call position to continue the execution. If the return value is required, assign the value of a specific register to a variable. If it is not used, the register can be used by the program, and the value in it will not be overwritten at any time. If a class or struct is passed, parameters and return values are passed through some space in the memory.

All references are converted to pointer operations.

Constructor: it has the same class name and has no return value. It initializes class members. The initialization method is best to select the initial value list. If the function is initialized, the data member has completed the default initialization, and the function body only assigns values to the data member.

Copy constructor: the first parameter is the constructor referenced by its own class type. It is used to pass parameters and return values when = is used to initialize objects and when parameters and return values are not referenced. When class objects are initialized through (), the compiler selects the most appropriate constructor, or may choose to copy the constructor.

Assign OPERATOR: overload the = Operator. The overload operator is essentially a function, and the value assignment operator is a function named operator =. The general form is: A & operator = (const &);

Destructor: The function name is composed of a wavy line and a class name, with no return value or parameters. Including the function body and the Destructor (hidden). Execute the function body first, and then execute the destructor, which is opposite to the constructor. When a member of the class type is destroyed, the Destructor will be called. The built-in type has no destructor, and nothing will be done during destruction.

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.