C + + 1

Source: Internet
Author: User
Tags shallow copy

1 Starting C + +

1.1 Header files for C + +

A traditional C header file.

C + + header file

HPP File Parts

1.2 Namespaces

C + + introduces new concepts, namespaces can effectively avoid various name conflicts in large projects

class keyword

Class is the core of C + + and the core content of object-oriented programming.

1.3 More stringent types of conversions

In C + +, different types of pointers are not directly assignable and must be strongly

1.4 New and delete

New and delete are C + + built-in operators, no header files are required, memory allocated with new must be freed with delete, not free.

New method for creating arrays new[];

1.5 inline functions

The inline keyword means that the inline function is not called as a function, but instead embeds the code of the inline function directly into the calling statement

Inline functions are suitable for small numbers of function code and frequent calls.

1.6 References

A reference is an alias for a variable, not an address

Default parameters for functions

C + + allows a function to provide default parameters at the time of definition, and if a function is called without a formal parameter, the value of the formal parameter is the default value

Reference as a function of parameters, not out of the stack, into the stack operation, so more efficient

If the value of the reference parameter cannot be modified inside the function, it is defined as constant reference const &

1.7 Overloading of functions

The name of the function is the same, but different parameters can be overloaded

function parameters are the same, but the return values are different and cannot be overloaded

Class 2 and Object 2.1 protection for C + + class members

If a class function returns a pointer to a member variable, the function returns a constant pointer in order to avoid modification of the member variable outside the class.

If a class member variable has the same name as a global variable, the member variable of the class is accessed by default in the class member function.

Accessing the global identity within the class, keyword::

2.2 The nature of C + + classes

The class is actually the data member of the structure plus executable code, which provides encapsulation, inheritance, polymorphism.

Inside the class, there is no permission qualifier, and the default is private

Inside the structure, there is no permission qualifier, the default is public

Scope of Class 2.3

The scope of a class member variable is confined to the inside of the class, and the outside of the class is invisible.

Constructor and destructor for class 2.4

The constructor name matches the name of the class, and there is no return value

Called automatically when a class is instantiated as an object.

A destructor is automatically called when an object is destroyed.

2.5 Initialize member list of constructors

The Initialize member list can only be used in constructors

A const member must be assigned a value with an initialized member list

Reference data members must be assigned with a list of initialized members

2.5.1 principle:

Since there is only one destructor, it is important to unify new or new[when assigning memory to the function's member pointers in different constructors.

2.6 Copy constructor 2.6.1 shallow copy

A simple assignment of member variables between two objects.

2.6.2 Deep Copy

Different object pointer members point to different memory addresses, while copying constructs is not a simple pointer assignment, but rather a copy of the memory.

2.6.3 principle:

If a class member has pointers, you need to implement the copy constructor yourself, or there is a risk of a shallow copy.

2.7 Constant class member, constant object.

A class member followed by the keyword const means to tell the compiler that no changes are made to the class member variables inside the function.

The argument to a function is a class, so a reference to the class is used. If you do not want the parameter to be called inside the function modification, then use const&

2.8 Explicit

Tell the C + + compiler to explicitly call this constructor, rather than think of it as the = operator to invoke the construct.

2.9 this pointer

This is a pointer to your own instance

Static member variables for class 2.10

The static variable is placed in the quiescent memory area, the program load exists, until the program exits to clean up.

The static member of a class is not directly related to the object of the class, and the statically member of the class is placed in the static memory area, and the program begins execution, until the end of the program is cleared.

A static member variable of a class has only one copy of the member variable, regardless of how many instances of the class are.

C + + 1

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.