C++_ the difference between basic _c and C + + 2

Source: Internet
Author: User
Tags aliases

Content:
(1) Functions in C + +
(2) Dynamic memory
(3) References
(4) Type conversion
(5) C + + Community's advice

Functions in the 1.c++
1.1 Overloading of functions
(1) The concept of overloading
In the same scope, the function name is the same, the argument list of the function is overloaded, and the identifier hiding principle is followed in different scopes.

(2) How the function overloads
A. Function names are the same, parameter types are different
B. Function name is the same, the number of parameters is different
C. function name is the same, parameter order is different
D. Function names are the same, const-modified constant functions and ordinary member functions make up Overloads (hereafter)

(3) More special heavy-duty method
(4) The principle of overload matching
Exact Match > Constant conversion > Upgrade conversion
> Standard conversions > Custom type Conversions (later) > Omit matches

1.2 Principle of function overloading
(1) The C + + compiler implements overloading by changing the name of the function to reflect the parameter table information in the new function name.
Such as:
void Show (int i,int j) {}
= _z4showii; New name after change of name
void Show (int j,int i)
= _z4showii;

(2) C programs can also call functions in C + + modules through a new function name after the C + + compiler has been replaced with a name
(3) through the extern "C" keyword can explicitly require the C + + compiler does not change the function of the operation to meet the C program can directly invoke the functions in the C + + module

1.3 Default parameters
The parameter of the function has a default value, which is also the default value
Such as:
void foo (int i,char c = ' A ', char* p = NULL) {...}

Foo (the ' B ', ' hello ');
Foo (in all, ' B '); p = NULL;
Foo (66); c = ' A ', p = NULL

Attention:
(1) The default parameter must be on the right. If a parameter has a default value, all parameters to the right of the parameter must have a default value
Such as:
void foo (int i,char c = ' A ', char* p) {}//Error
Foo (the "Hello");

(2) If the Declaration and definition of a function are separate, then the value of the default parameter can only be written in the declaration part of the function
Such as:
void foo (int i,char c = ' A ', char* p = NULL);

void foo (int i,char c,char* p) {}

(3) Take care to prevent overloading conflicts caused by default parameters

Extended:
(1) void fn () {}; In the C language, it means that you can accept any number of arguments of any type, and in C + + it means that no arguments are accepted.
(2) In C language can not declare the function, will automatically do the implicit declaration, the call function must be declared in C + +, or the definition of the called function is placed before the calling function

1.4 Dummy Yuan
Only data types that have no name are called dummy elements
Such as:
VOID fn (int) {}
FN (); Error
FN (66); Ok

Use:
(1) In order to be compatible with previous code
(2) for operator overloading, mainly used to distinguish the prefix from the increment and decrement operator (hereafter)

1.5 Inline
Functions decorated with the inline keyword are called inline functions
Similar to the C language of the macro function, you can replace the instruction, relative macro function has some advantages, you can check the data type, you can calculate the value of the expression and so on

Attention:
The inline keyword modifier, which simply means that this is a recommendation, not a requirement, that all functions decorated with the inline keyword do not necessarily do inline processing; Conversely, functions that are not decorated with the inline keyword may be inline with the compiler's optimization strategy

Practice:
Customizes a function that computes the square of an int type parameter, and returns the result of the calculation by the return value, using inline and unmodified to see if the inline

Extended:
(1) Small and simple functions that are called multiple times are suitable for inline
(2) Very few calls and large and complex functions are not suitable for inline
(3) Recursive function cannot be inline

2. Dynamic memory
C Language: Malloc/calloc/realloc/free function

In C + +:
In addition to providing compatibility with C, the new/delete operator is provided to manage dynamic memory

2.1 Requesting memory of the specified data type variable size
int* pi = new int;
Delete pi; PI = NULL;

int* pi = new int (66);
Delete pi; PI = NULL;

2.2 Requesting memory of the specified array size
int* pi = new int[10];
delete[] PI; PI = NULL;

C++11/C++0X Standard Support
int* pi = new int[10]{...};
delete[] PI; PI = NULL;

Questions:
int* pi = new int; Ok
int* pi = new int[5]; Ok
int* pi = new Int[3][4];//error
int** pi = new int[3][4]; Error

2.3 Returns an array pointer to the N-1 dimension when assigning an n-dimensional array using the new operator
Such as:
int (*PI) [4] = new INT[3][4];
delete[] PI; PI = NULL;

int (*PI) [3][4]
= new Int[2][3][4];
delete[] PI; PI = NULL;

Initializing with {} after [] requires c++11/c++0x standard support

2.4 Positioning assignments
New (pointer) data type (initial value);
|
The pointer here indicates the starting position of the allocated memory

Function:
Used primarily to allocate memory again in an already allocated memory space.

3. References

3.1 Concepts
A reference is not a separate data type, similar to a pointer in C, which is actually an alias of a variable

Such as:
int a = 10;
int& B = A;
= = to a an individual named B,b = 10;
int& C = b;
= = int& C = A; c = 10;

3.2 The difference between a reference and a pointer
(1) The reference must be initialized, the pointer can not be initialized
int& A; Error
int* PA; Wild Hands

(2) The reference cannot be null, the pointer can be empty
int& a = NULL; Error
Const int& A = NULL; Frequently cited
Const int& A = 11; Ok
int* pi = NULL; Null pointer

(3) Reference can not be changed target, the pointer can be
int a = 10;
int& B = A;
int m = 20;
b = m; = a = m; Assign value

int* pi = &a;
PI = &b;

(4) You cannot declare a reference array, you can declare a pointer-type array
int& ARR[10]; Error
int* ARR[10]; Ok
int arr[10]; Ok
int (*arr) [10]; Ok

Suggestions:
Use references as much as possible in C + + programs, rather than pointers, because pointers are prone to field pointers and error prone segments

Homework:
Query the signal function and combine the prototype of the signal function

Forecast for Tomorrow:
(1) References
(2) Type conversion
(3) The C + + community's advice to programmers
(4) The concept of object-oriented programming
(5) Classes and objects
(6) Constructors
(7) List of initialization and its necessity

Review:
Introduction to 1.c++ and programming changes
Introduction to 1.1 C language
Bell Labs Thompson B language, modified and expanded on the basis of B language, New B language, later renamed C, was born in 1972

1.2 C + + Introduction
On the basis of C language, BS extended C, New C, renamed C with Classes (Class C), + + operator, C + +

1.3 Historical events
Standard c++98 for C + + appeared in 1998
2003 C++98 Revision c++03
2011 C++11 Standard (c++0x)

1.4 Comparison of C + + and C languages
(1) is a compiled language
(2) A strongly typed language
(3) C + + compatible with more features
A. More concise language style
B. More rigorous type checking (void* int*)
C. Support for object-oriented programming
D. Support exception handling
E. Support for operator overloading
F. Support for generic programming

1.5 Use
(1) For game development
(2) System development and drive development

1.6 Programming changes
(1) File extension
SOURCE file:. cc/. C/.cxx/.cpp
Header file:. hpp. h
(2) header file
#include <iostream>
#include <cstdio>
(3) Input and output
cout <</CIN >>
(4) Compiler changes
G++/c++ Xxx.cpp
GCC/CC xxx.cpp-lstdc++
(5) namespaces
using namespace Std;

2. Namespaces
2.1 Custom namespaces
Namespace namespace name
{
variables, functions, etc.
}

2.2 How namespaces are used
(1) How to use namespace directives
using namespace Std;
(2) Using scope qualifiers
Std::cout << "Good people are really good" << Std::endl;
(3) How to use namespace declarations
Using Std::cout;
Using Std::endl;
cout << "Good people are really good" << Endl;

2.3 Nameless namespaces
If an identifier is not placed in any namespace, it is placed by default in the Nameless/anonymous namespace, using the following method:
:: identifier

2.4 Extensions
(1) The contents of the namespace can be written separately
(2) Separation of function declarations and definitions in namespaces
(3) namespaces can be nested

3. Struct, union, enumeration different
3.1 Differences in structural bodies
(1) struct keyword can be omitted when defining struct variables
(2) A function can be defined in a struct

3.2 Different of the Union
(1) The Union keyword can be omitted when defining a union variable
(2) Support anonymous syndication

3.3 Enumeration of different
(1) enum keyword can be omitted when defining enum type variables
(2) enumeration is a separate data type and cannot be assigned using an integral type

4.bool type and operator aliases
4.1 BOOL Type
BOOL is a basic data type in C + + and has two values: True and False, which is 1 and 0,
is essentially a single-byte integer that can be implicitly converted to the bool type for any of the base data types, using Boolalpha to specify the format of the output: True and False

A bool type can define a variable as a function's return value and parameter, or it can define a pointer

4.2 Aliases
&& & Bitand
|| or | Bitor ...
----------------------------------
Today's content:
(1) Functions in C + +
(2) Dynamic memory
(3) References
(4) Type conversion
(5) C + + Community's advice

Functions in the 1.c++
1.1 Overloading of functions
(1) The concept of overloading
In the same scope, the function name is the same, the argument list of the function is overloaded, and the identifier hiding principle is followed in different scopes.

(2) How the function overloads
A. Function names are the same, parameter types are different
B. Function name is the same, the number of parameters is different
C. function name is the same, parameter order is different
D. Function names are the same, const-modified constant functions and ordinary member functions make up Overloads (hereafter)

(3) More special heavy-duty method
(4) The principle of overload matching
Exact Match > Constant conversion > Upgrade conversion
> Standard conversions > Custom type Conversions (later) > Omit matches

1.2 Principle of function overloading
(1) The C + + compiler implements overloading by changing the name of the function to reflect the parameter table information in the new function name.
Such as:
void Show (int i,int j) {}
= _z4showii; New name after change of name
void Show (int j,int i)
= _z4showii;

(2) C programs can also call functions in C + + modules through a new function name after the C + + compiler has been replaced with a name
(3) through the extern "C" keyword can explicitly require the C + + compiler does not change the function of the operation to meet the C program can directly invoke the functions in the C + + module

1.3 Default parameters
The parameter of the function has a default value, which is also the default value
Such as:
void foo (int i,char c = ' A ', char* p = NULL) {...}

Foo (the ' B ', ' hello ');
Foo (in all, ' B '); p = NULL;
Foo (66); c = ' A ', p = NULL

Attention:
(1) The default parameter must be on the right. If a parameter has a default value, all parameters to the right of the parameter must have a default value
Such as:
void foo (int i,char c = ' A ', char* p) {}//Error
Foo (the "Hello");

(2) If the Declaration and definition of a function are separate, then the value of the default parameter can only be written in the declaration part of the function
Such as:
void foo (int i,char c = ' A ', char* p = NULL);

void foo (int i,char c,char* p) {}

(3) Take care to prevent overloading conflicts caused by default parameters

Extended:
(1) void fn () {}; In the C language, it means that you can accept any number of arguments of any type, and in C + + it means that no arguments are accepted.
(2) In C language can not declare the function, will automatically do the implicit declaration, the call function must be declared in C + +, or the definition of the called function is placed before the calling function

1.4 Dummy Yuan
Only data types that have no name are called dummy elements
Such as:
VOID fn (int) {}
FN (); Error
FN (66); Ok

Use:
(1) In order to be compatible with previous code
(2) for operator overloading, mainly used to distinguish the prefix from the increment and decrement operator (hereafter)

1.5 Inline
Functions decorated with the inline keyword are called inline functions
Similar to the C language of the macro function, you can replace the instruction, relative macro function has some advantages, you can check the data type, you can calculate the value of the expression and so on

Attention:
The inline keyword modifier, which simply means that this is a recommendation, not a requirement, that all functions decorated with the inline keyword do not necessarily do inline processing; Conversely, functions that are not decorated with the inline keyword may be inline with the compiler's optimization strategy

Practice:
Customizes a function that computes the square of an int type parameter, and returns the result of the calculation by the return value, using inline and unmodified to see if the inline

Extended:
(1) Small and simple functions that are called multiple times are suitable for inline
(2) Very few calls and large and complex functions are not suitable for inline
(3) Recursive function cannot be inline

2. Dynamic memory
C Language: Malloc/calloc/realloc/free function

In C + +:
In addition to providing compatibility with C, the new/delete operator is provided to manage dynamic memory

2.1 Requesting memory of the specified data type variable size
int* pi = new int;
Delete pi; PI = NULL;

int* pi = new int (66);
Delete pi; PI = NULL;

2.2 Requesting memory of the specified array size
int* pi = new int[10];
delete[] PI; PI = NULL;

C++11/C++0X Standard Support
int* pi = new int[10]{...};
delete[] PI; PI = NULL;

Questions:
int* pi = new int; Ok
int* pi = new int[5]; Ok
int* pi = new Int[3][4];//error
int** pi = new int[3][4]; Error

2.3 Returns an array pointer to the N-1 dimension when assigning an n-dimensional array using the new operator
Such as:
int (*PI) [4] = new INT[3][4];
delete[] PI; PI = NULL;

int (*PI) [3][4]
= new Int[2][3][4];
delete[] PI; PI = NULL;

Initializing with {} after [] requires c++11/c++0x standard support

2.4 Positioning assignments
New (pointer) data type (initial value);
|
The pointer here indicates the starting position of the allocated memory

Function:
Used primarily to allocate memory again in an already allocated memory space.

3. References

3.1 Concepts
A reference is not a separate data type, similar to a pointer in C, which is actually an alias of a variable

Such as:
int a = 10;
int& B = A;
= = to a an individual named B,b = 10;
int& C = b;
= = int& C = A; c = 10;

3.2 The difference between a reference and a pointer
(1) The reference must be initialized, the pointer can not be initialized
int& A; Error
int* PA; Wild Hands

(2) The reference cannot be null, the pointer can be empty
int& a = NULL; Error
Const int& A = NULL; Frequently cited
Const int& A = 11; Ok
int* pi = NULL; Null pointer

(3) Reference can not be changed target, the pointer can be
int a = 10;
int& B = A;
int m = 20;
b = m; = a = m; Assign value

int* pi = &a;
PI = &b;

(4) You cannot declare a reference array, you can declare a pointer-type array
int& ARR[10]; Error
int* ARR[10]; Ok
int arr[10]; Ok
int (*arr) [10]; Ok

Suggestions:
Use references as much as possible in C + + programs, rather than pointers, because pointers are prone to field pointers and error prone segments

Homework:
Query the signal function and combine the prototype of the signal function



C++_ the difference between basic _c and C + + 2

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.