C + + Small Summary

Source: Internet
Author: User
Tags shallow copy

The simple difference between 1.C and C + +

1. The created file type is different, C is the. c,c++ is. cpp

2. The introduced header file is not the same

3.c++ has a name space

4. Input and output statements are not the same

5.C language does not allow overloading, C + + can overload

6. Custom type, C language use struct,c++ use class

7.C language oriented process, C + + object oriented

2. Primary knowledge classes and objects

Four features of C + +: Abstraction, inheritance, encapsulation, polymorphism

Abstraction: The process of defining a specific transaction is called an abstract process

Encapsulation: The process of tying data and data to the way they are manipulated

Polymorphism: The same transaction has multiple forms of representation

Languages that support inheritance and polymorphism are called object-oriented languages

A class is a description of a computer's abstract class of real-world entities, and the instance created is an object

3. Definition and creation of classes

Class is a data type in C + +

C + + objects: variables defined by the class

Instantiation: The process of defining an object

Access qualifier: Public,protected,private

If there is no access qualifier at the beginning of the class, the system defaults to private

Access qualifiers protected and private embody the encapsulation of classes

Class has no space, so first instantiate the object and allocate space for the object

4.this pointer

Size of the class: corresponding data member size (related to byte alignment), excluding methods

The this pointer represents the current object

The this pointer hides the insertion of this pointer only at the function location when the object transfer method is used.

C + + class recognition Order: 1. Class name 2. Identify data members (whether public or private) 3. Identification method (function) so the location of the data member can be arbitrary

Rewrite function: Add a parameter, class name *const this//with const block pointer, this point cannot be changed

Object is also overwritten when calling this method, the first argument is actually the address of the object.

5. Constructors and destructors

Private data members can only be accessed through public methods

Constructors: Data members are more private, and to initialize them, they must be executed with a public function that executes only once when the object is defined.

The constructor function name is the same as the class name, no return type description, and when the program runs, when the new object is built, the constructor of the class to which the object belongs is automatically called, and this lifetime is called only this time. Constructors can be overloaded. If the constructor is not given in the class description, the C + + compiler automatically gives a default constructor. However, the default constructor is not generated by the system whenever a constructor is defined.

Distinction: Test T1; Instantiating an Object

Test T1 (); function declaration

Destructor: Unregisters the object and works on the aftermath.

First constructs the post-destructor (constructs the object to generate in the stack area, therefore conforms to the stack advanced after the characteristic)

Destructors have no return value, no arguments, and one class has only one destructor.

Destructors can default, and the system automatically calls destructors when the object is logged off.

6. Three functions of a constructor

1. Constructing objects

2. Initializing objects

3. Type conversions (related to the ability to generate intermediate variables)

You can never assign a value as long as the type is different.

Keyword explicit: Do not allow implicit conversions (display conversion keywords) before the constructor declaration

7. References (aliases)

The reference avoids the side effects of the function and avoids a significant reduction in the efficiency of program execution caused by the reallocation of memory to the argument object.

Several cases of reference:

1. references to variables:

Int a=10;

Int &b=a;

2. reference to the pointer:

Int *p=&a;

Int *&q=p;

3. references to arrays:

Int ar[10]={1,2,3,4,5,6,7,8,9,10};

Int (&BR) [10]=ar;  Note Cannot write int &br[10]=ar; Assigning an array to a reference to an array element.

You cannot reference a const member variable with a non-const member variable:

Const int x=100;

Int &y=x; Example of error correct procedure: const int &y=x; But you can refer to a non-const member variable with a const member variable

4. another situation

Const double d=12.34;

Const int &f=d; Can compile normally, but the behavior is very strange. caused the address of F and D not one. The reason is that the address of F does not refer to the address of D but refers to the address of the temporary variable, which is the int type. This causes the program to get an error.

5.const double d=12.34;

int& F=d; Program compilation error, because the temporary variable has the constancy, the temporary variable to f assignment is equivalent to the Const member variable assignment to the non-const member variable, which is not allowed

8. Copy Constructors

Copies only need to copy data members, and function members are public

Initializes another object with an existing object (no transfer constructor, transfer copy constructor)

Take a class name as a function and use a reference to a parameter of its own type.

If you pass a real class object as a parameter to the copy constructor, it will cause infinite recursion, so use a reference. Parsing: When using a value pass without reference passing, you need to construct a temporary variable for the argument copy, copy the constructed argument and copy the construct, so there is an infinite recursive scenario, which causes the stack to overflow and cause the program to crash.

Easy Error Point:

Test T;

Test t2=t; Note that the copy constructor is called

Test T3;

t3=t; Note that the assignment function is called

Three functions of a copy constructor:

1. Object Initialization Object

2. When a function's formal parameter is a class object, it is used when the function is called in conjunction with a formal parameter, when a new local object is created in memory, returning the caller

function return mechanism: We think the returned value is a local variable, out of scope will be extinct. The C + + and C languages return are actually temporary objects. That is, create an intermediate temporary variable that copies the value you want to return to a temporary variable. Once returned, the variable to be returned is extinct (the lifetime of the temporary variable is only at the expression at the function call). Once the variable is assigned a value, the returned temporary variable is refactored.

The temporary variable is local and returns the temporary variable created.

9. Assignment function

Benefits of using References: 1. The copy constructor is no longer called, increasing the efficiency by 2. No more space for it saves memory

Frequently cited benefits: Protect parameters so that the values of the parameters cannot be changed

A problem that is often ignored by referencing: parse the following program: Return by reference, that is, no longer create a temporary nameless object, return is a real reference to the TMP, but because TMP is a temporary object, once the function execution is finished, the TMP will be destroyed when the main program executes a test T2; T2=fun (t1), the T2 data becomes an incorrect value because T1 has been refactored. Therefore, be aware of the use of references.

test& Fun (Test x)

{

int value;

Value=x.getdata (); A member function

Test tmp (value);

return TMP;

}

When the return value is not affected by the scope (such as a pointer), consider using a reference

10. Call Optimization for functions

1. Will test TMP (value);

return TMP;

Switch

return tmp (value); A temporary nameless object was created

The process of reducing the temporary variables constructed by the copy constructor and the destructor copy construction (reduced one copy and destruction of the return unnamed temporary object, optimized by the compiler)

2. The parameter is passed by reference, no longer the copy construction of the temporary object for the parameter, but the direct manipulation of the original object, and does not open up space for the parameter.

3. Return values are returned as a reference, but be aware that the return value must be not subject to scope restrictions

4. Place Test T1 (100);

Test T2;

T2=fun (t1);

Switch

Test T1 (100);

Test t2=fun (t1);

T2 is not an assignment (to initialize), minus a call to an assignment function, or a copy construct (the compiler thinks that the temporary object returned by the function is T2, so the copy construction is omitted and the compiler does the optimization)

11. Depth Copy

Six default functions: constructors, copy constructors, assignment functions, destructors, overloads of the fetch address operator for general objects, overloads of the regular object take address operator

If a data member in a class contains pointers, it is common to re-write copy constructors and assignment functions, and it is not possible to solve a deep copy problem by relying on the default copy constructor.

The same space cannot be freed multiple times

Shallow copy: Just copy the pointer without making a real copy of the content

Deep copy: Not just a pointer to a simple copy, but a copy of the same space, and pass the value to the space

12. Operator overloading

The operators that are not overloaded in C + + are: three-mesh conditional operators, members. Operator, scope operator, sizeof

13. Friends

A regular member function declaration describes the three things that are logically different:

1. The function can access the private part of the class declaration

2. The function is in the scope of the class

3. The function must be activated via an object (with a this pointer)

By declaring the function as static, you can let him have only the first two properties. By building a function declaration as a friend you can just let him have the first property (access the private part of the class declaration).

A friend can be implemented inside a class, or it can be implemented outside of a class, and his transfer does not require an object to be activated. Friends have broken the encapsulation of classes to some extent.

Friend function Note points:

1. A friend function is not a function, but a friend function can access all members of a class, and a general function can only access a common member in a class.

2. The friend function is not restricted by the Access keyword in the class, it can be placed in the public, private, protected part of the class, as a result.

  

C + + Small Summary

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.