Basic knowledge of C + +

Source: Internet
Author: User
Tags access properties

One, C + + occupies memory allocation

1), stack (stack)-Automatically allocated by the compiler to release, store the function's parameter value, local variable value and so on. Its

Operations are similar to stacks in data structures.

2), heap area (heap)-generally released by the programmer, if the programmer does not release, the program ends may be the OS back

Inbox Note that it is not the same as the heap in the data structure, the distribution is similar to the list, hehe.

3), Global Zone (Static)--the storage of global variables and static variables is placed in a block, initialized

Global variables and static variables in an area, uninitialized global variables and uninitialized static variables in adjacent

An area. -released by the system after the program is finished.

4), literal constant area-the constant string is put here. Released by the system after the program is finished

5), program code area-the binary code that holds the function body.

Two, friend function and friend class

2.1 Why use friend functions

Reduce system overhead and increase efficiency when data sharing between classes is implemented.

In fact, there are about two situations in which you need to use a friend function:

(1) A friend is required for some situations where operator overloading is used.

(2) Two classes to share data.

2.2 Parameters

The friend function does not have this pointer, there are three conditions for the parameter:

(1) When you want to access a non-static member, you need the object to make arguments

(2) When you want to access a static member or global variable, you do not need the object to do the argument

(3) If the object that makes the argument is a global object, then the object is not required to do the argument

2.3 Calls

You can directly adjust the cell function, do not need to pass through the object or pointer

2.4 Classification

(1) Common friend function : Enables the normal function to access the class.

Syntax: declaration: Friend + common function declaration; Implementation location: Can be outside the class or class;

Implementation code: Same as normal function; call: similar to normal function, call directly

(2) All member functions of Class Y are class X friend functions-friend classes: Use a single declaration to make all functions of the Y Class A friend of Class X, which provides a way to collaborate between classes so that objects of class Y can have the functions of Class X and Class Y

Syntax: Declaration location: Public Private, usually private (class as a variable)

Declaration: Friend + class name (not object OH)

(3) A member function of Class Y is a friend function of Class X: Make a member function of Class y a friend of Class X, specifically: In this member function of Class Y, with the help of parameter x, you can directly take the private variable of x

Syntax: Declaration location: Declared in public (itself as a function); Declaration: Friend + Declaration of member function

Call: Define Y's Object y---use y to call your own member function---Use the friend mechanism in your own member function

2.5 The difference between a friend function and a class member function

(1) The member function has this pointer, and the friend function does not have the this pointer

(2) Friend function can not be inherited, just like the father's friend may not be the son of friends

Iii. Constructors and destructors

3.1 Features

Both the user-defined constructor and the default constructor have the following characteristics: ①. Automatically executes when an object is created; The function name of the ② constructor is the same as the class name; ③. No return value type, no return value; ④. Constructors cannot be called explicitly.

3.2 Initializing an expression

Initialization with the initialization table is done before the constructor is called. Each member can appear only once in the initialization table, and the order of initialization is not determined by the order in which the data members appear in the initialization table, but by the order in which they are declared in the class.

3.3 Copy Constructors

When the parameters of a function are passed by value, the editor calls the copy constructor of the class to copy the actual arguments to the called function.

The copy constructor, like other constructors of a class, takes the name of a class as a function, but its parameters are only one, that is, the constant reference type of the class.

3.4 Destructors

Destructors are called automatically when an object is revoked.

The destructor function name is the same as the class name, and it is closely preceded by the name with the tilde ~ and the constructor, for example: ~point (); Destructors have no return type and cannot specify parameters, so destructors can only have one and cannot be overloaded; unlike constructors, destructors may be explicitly called to free memory that is dynamically requested in an object.

3.5

When the user does not explicitly define a destructor, the compiler also generates a default destructor for the object, but the default generated destructor frees only the space occupied by ordinary data members of the class and cannot release the space requested by new or malloc. So sometimes we need to explicitly define destructors to release the space for these applications and avoid memory leaks.

About Exceptions:

(1) It is not recommended to throw an exception in the constructor.

(2) When the constructor throws an exception, the destructor does not execute automatically and requires manual release to free up memory.

(3) The destructor should not throw an exception.

Iv. copy Constructors and overloaded assignment operator functions

4.1 Copy Constructors

Call Time:

(1) When another object of the class is initialized with one object of the class

(2) If the function's formal parameter is the object of the class, when the function is called, the combination of the formal parameter and the argument

(3) If the return value of the function is an object of the class, when the function execution finishes returning the caller

(4) When a temporary class object needs to be generated

4.2 Distinguishing between copy constructors and overloaded assignment operator functions

Myclass Obi1;

Myclass obj3=obj1; Call copy constructor

Myclass obj3;

Obj3=obj1; Calling overloaded assignment operator functions

Five, this pointer

4.1 Concepts

The this pointer is a special pointer that is implied in each member function. It points to the object that is being manipulated by the member function.

4.2 Features

(1) This can only be used in member functions. Neither the global function nor the static function can use this.

In fact, the member function defaults to the first parameter is T * const this.

(2) This is constructed before the start of the member function, cleared after the end of the member function

In C + +, classes and structs are only one difference: Members of a class are private by default, and structs are public. This is a pointer to a class, and if it is a struct, this is the pointer to the struct.

4.3 The following scenario is required to display the call of the This pointer

(1) For the realization of the chain-type reference of the object;

(2) To avoid assigning values to the same object;

(3) When implementing some data structures, such as list;

V. Inheritance and derivation

5.1 Inheritance

Class, it is the new class that obtains existing attributes from existing classes. Or the process of generating a new class from an existing class is the derivation of the class. The original class is called a base class or parent class, and the resulting new class is called a derived class or subclass.

5.2 Derived classes

1) A derived class can have multiple base classes at the same time, which is called multiple inheritance, and the derived class has only one base class, called single inheritance. Direct derivation, indirect derivation.

2) The Inheritance method specifies how to access the members inherited by the base class. The way of inheritance is public, private, protected. If the inheritance method is not shown, the default is private inheritance.

3) Derived classes inherit all members of the base class except constructs and destructors.

base class members access properties in derived classes

 

public

private

protected

public

public

private

protected

private

not directly accessible

not directly accessible

not directly accessible

Protected

Protected

Private

Protected

5.3 Constructors and destructors for derived classes

The initialization of a member inherited from a base class in a derived class is done by the constructor of the base class, and new members in the derived class are initialized in the constructor of the derived class.

Constructor syntax

Derived class Name:: Derived class name (general table of parameters): base class name 1 (parameter table 1), base class name (parameter Name 2) .... base class name N (parameter name N), inline sub-object 1 (Parameter table 1), inline sub-object 2 (Parameter Table 2) .... Inline sub-object N (parameter table N)

Note:

The initialization order of constructors is not performed in the order above, but is initialized according to the order of the declarations.

The execution order of destructors is opposite to the constructor function

5.4 Virtual base class

To solve the problem of multiple copies, you can set the common base class to a virtual base class

In general, derived classes only pass arguments to the constructors of their direct base classes, but in a virtual base class, all derived classes of the virtual base class, whether directly or indirectly, must list the initialization of the virtual base class in the member initialization list of the constructor.

5.5 Assignment Compatibility principle

1), assignment-compatible rules are substituted for objects of a public-derived class that can be used anywhere a base class object is needed.

2), the substitution referred to in the Assignment compatibility rule includes:

The object of a derived class can be assigned to a base class object;

An object of a derived class can initialize a reference to a base class;

The address of a derived class object can be assigned to a pointer to a base class.

After the override, the derived class object can be used as an object of the base class, but only members that inherit from the base class are used.

5.6 Methods for accessing private member variables of a class

1) Set/get Interface

2) Friend class

3) friend function

Vii. Basic principles of object-oriented design

(1) Single duty principle (Single-resposibility Principle): A class, it is best to do only one thing, only a change that causes it. The principle of single responsibility can be regarded as the extension of low coupling, cohesion in object-oriented principle, defining responsibility as the cause of change, in order to improve cohesion to reduce the cause of change. (2) Open closure principle (open-closed principle): Software entities should be extensible and non-modifiable. That is, to open the extension (taste new requirements or changes, you can extend the existing code to adapt to the new situation), to modify the closed (meaning the class once the design is complete, you can work independently, and do not make any changes to the class).

(3) The Liskov substitution principle (liskov-substituion Principle) is the Richter substitution principle: Subclasses must be able to replace their base classes. This idea is embodied in the constraints of inheritance mechanism, only the subclass can replace the base class, it can ensure that the system in the runtime to identify the subclass, which is the basis of ensuring inheritance reuse.

(4) Dependency inversion principle (dependecy-inversion Principle): dependent on abstraction. In particular, high-level modules do not rely on the underlying modules, both of which are dependent on abstraction;

(5) interface Isolation principle (Interface-segregation Principle): Use multiple small dedicated interfaces instead of a large total interface

(6) The Dimitri rule is also called the least knowledge principle (Least knowledge Principle LKP): That is, one object should have as little knowledge of other objects as possible,

Don't talk to strangers.

(7) Combination/aggregation Reuse principle: Use composition/aggregation as much as possible, rather than using inheritance.

Basic knowledge of C + +

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.