What are the differences between C + +? A lot of people don't know the comparison method

Source: Internet
Author: User
C + + should compare the number of keywords, source file, variable definition or declaration location, function, default parameters several aspects of comparison, if you are always confused, read this article will help you.





C + + is compared from the following aspects:


    1. number of keywords:
      C Language: C99 version, 32 keywords
      C++:C98 version, 63 keywords

    2. source file:
      C source file suffix. c,c++ source file suffix. cpp, if you do not give anything when you create the source file, the default is. cpp

    3. variable definition or declaration location:
      C language must be defined in the first line; C + + does not require

    4. function:
      (1) Return value
      In C, if a function does not specify a return value type, it returns the int type by default;
      In C + +, the detection of function return values is more stringent, and if a function does not return a value, it must be specified as void.
      (2) Parameter list
      In C, if a function does not specify a parameter list, it can accept any number of arguments by default, but in C + +, because of strict parameter type detection, there is no parameter list function, which defaults to void and does not accept any parameters.

    5. Default parameters:
      The default parameter is to specify a default value for the function's arguments when declaring or defining the function. When the function is called, the default value is used if no argument is specified, otherwise the specified argument is used.

//1. Implement the default parameter void Test(int a = 50){

     Cout << a << endl;

}

Int main(){ Test(); // output 50

     Test(100); // output 100}



(1) Full default parameter: All parameters are given the default values//code


// Implement the full default parameter void Test(int a = 1, int b = 2, int c = 3)

{ cout << a << "" <<" "<< b << "" <<" "<< c << endl;

}int main()

{

     Test();//1 2 3

     Test(100);//100 2 3

     Test(100, 200);//100 200 3

     Test (100, 200, 300); / / 100 200 300}


(2) Semi-default parameters: Specify that the default value can only be transferred from right to left//code


// Implement semi-default parameters Note: The default value can only be passed from right to left void Test1(int a = 1, int b = 2, int c = 3)

{ cout << a << "" << " " << b << "" << " " << c << endl;

}void Test2(int a , int b = 2, int c = 3)

{ cout << a << "" << " " << b << "" << " " << c << endl;

}void Test3(int a , int b , int c = 3)

{ cout << a << "" << " " << b << "" << " " << c << endl;

}void Test4(int a = 1, int b , int c = 3)// cannot be compiled, because it violates the default value and can only be given from right to left { cout << a << " " << " " << b << "" << " " << c << endl;

}void Test5(int a = 1, int b = 2, int c )// cannot be compiled because it violates the default value and can only be given from right to left { cout << a << " " << " " << b << "" << " " << c << endl;

}void Test6(int a = 1, int b , int c )// cannot be compiled, because it violates the default value and can only be given from right to left { cout << a << "" < < " " << b << "" << " " << c << endl;

}void Test7(int a , int b = 2, int c )// cannot be compiled because it violates the default value and can only be given from right to left { cout << a << "" < < " " << b << "" << " " << c << endl;

}int main()

{

    Test1();//1 2 3}


Attention:


a. Parameters with default values must be placed at the end of the parameter list.

b. Default parameters cannot appear in both function declarations and definitions, only one of them, preferably in a function declaration.

c. The default value must be a constant or a global variable.


The C language does not support default parameters



function overloading


    • function overloading is the declaration of functions of the same name in the same scope that have similar functions, which must have different parameter lists (the number, type, and type Order) of the functions with the same name.

/ / Function overload void Add (); void Add (int a); / / row parameters are not the same void Add (char b); 
/ / row parameter type different void Add (int a, char b); void Add (char a, int b); / / the order of the row type is different

    • Only the type of the return value is different and cannot be a function overload.

/ / Only the type of the return value is different, is a void add (int a, int b) that can not constitute a function overload

{}int Add(int a, int b)

{ return a + b;

}int main()

{

     Add(1, 2);//Because this will make the call ambiguous, both functions can be called

     Return 0;

}
    • C + + supports function overloading because the VS editor compiles the type of function arguments into the name of the function at the bottom, so the original function name is replaced with another unique name.

int Add (int a, int b);    // ? add@ @YAHHH @zchar Add (int a, int b);   // ? add@ @YADHH @zchar Add (char A, char b); // ? add@ @YADDD @z
    • The C language does not support the reason for function overloading: The new function name generated is still the same. Just add _ in front of the function name

    • C + + will compile the function as a language, just add the extern "C" to the function

extern "C" int Add (char A, int b);


Reference



There are two kinds of functions in C language: The value of the transfer and the method of transmitting



Pass value : During a function call, a temporary variable is generated with a formal parameter instead, and the value of the argument is eventually passed to the newly assigned temporary variable, the parameter.
value-Passing advantages : The side effects of a function do not affect the external arguments.
Pass -through disadvantage: you cannot change an external argument by modifying the parameter.



Pass -through: During a function call, a temporary variable is generated with a formal parameter instead, and the address of the argument is eventually passed to the newly allocated temporary variable.
Advantages of transmission : saving space, high efficiency, changing parameters can change external arguments.
Reference Disadvantage : pointers are unsafe, and side effects of functions affect external arguments.



in C + + :



References:
(1) Concept: A reference is not a new definition of a variable, but an alias for an existing variable, the compiler does not open a memory space for the reference variable, and its reference variables share the same block of memory space.
(2) Form: type & Reference variable name = reference entity


/ / Reference int main ()

{ int a = 10; int& ra = a; printf("%p\n", a); printf("%p\n", ra);//ra and a have the same address, indicating that ra and a are the same An entity that shares the same memory space

 

     Ra = 3; printf("%d\n", a);//3

     Return 0;

}


Note:


a. References must be initialized when they are defined.

b. A variable can be referenced multiple times.

c. References Once an entity is referenced, it cannot be referenced by other entities.

d. The life cycle of a reference variable is shorter than the life cycle of an entity.


(3) frequently cited


Often quote int main()

{ const int a = 1; //int& ra = a;//Compile will fail because entity a is a constant

     Const int& ra = a; double b = 12.34; //int& rb = b;//Compile will fail because of different types

     Const int& rb = b; printf("rb=%d\n", rb);//rb=12

     b = 5.0; printf("b=%f\n", b);//b=5.0

     Printf("rb=%d\n", rb);//rb=12

     // The value of b changes, but the value of rb does not change, indicating that rb and b are two different entities}


(4) Array reference


Array reference int a[10];//array A is of type Int[10]int (&ra) [ten] = A;


(5) referencing the scene:
A. Use a reference as a parameter to the function to change the argument.


Void Swap(int* pLeft, int* pRight)

{ int temp = *pLeft; *pLeft = *pRight; *pRight = temp;

}

 

Void Swap(int& left, int& right)

{ int temp = left;

     Left = right;

     Right = temp;

}

/ / If you do not want to change the value of the actual parameter when using the reference, then add const before the reference

Void Swap(const int& left, const int& right);int main()

{ int a = 10; int b = 20;

     Swap(&a, &b);//Change the argument by passing the address printf(" a=%d ", a); printf(" b=%d\n", b);

 

     Swap(a, b);//Change the argument by reference printf(" a=%d ", a); printf(" b=%d\n", b);

}


B. Using a reference variable as the return value of a function//code


Case 1: int& FunTest()

{ int a = 10; return a;

}int main()

{ int b = FunTest();// Assign the return value of the function to b

     Printf("b=%d\n", b);//b=10

     Printf("b=%d\n", b);//b=10

     Printf("b=%d\n", b);//b=10

     Return 0;

}

 

Case 2: int& FunTest2()

{ int a = 10; return a;

}int main()

{ int& b=FunTest2();//The return value of the function as an entity,

     Printf("b=%d\n", b);//b=10

     Printf("b=%d\n", b);//random value

     Printf("b=%d\n", b);//random value

     Return 0;

}

 

Case 3: int& FunTest3(int& a)

{

     a = 10; return a;

}int main()

{ int b; int& rb = FunTest3(b); printf("b=%d\n", b);//b=10

     Printf("rb=%d\n", rb);//rb=10

     Printf("rb=%d\n", rb);//rb=10

     Printf("rb=%d\n", rb);//rb=10

     Return 0;

}

Note: you cannot return a reference on the stack space


Comparison of the efficiency of transmitting, transmitting and quoting


/ / Compare struct BigType

{ int array[10000];

};void FunTest(BigType bt)//pass or address{}void FunTest(BigType& bt)//reference{}void TestFunTestRumTime()

{

     BigType bt;

     Size_t Start = GetTickCount(); for (i = 0; i < 1000000; i++)

     {

         FunTest(bt);//pass value or pass reference

         FunTest(&bt);//Address

     }

     Size_t End = GetTickCount(); printf("%d\n", End - Start);

}//This code detects the slowest pass value, and the address and reference speed are fast and the time is almost the same


What is the difference between a reference and a pointer?



Same point:


    • List Contents

    • The underlying process is the same, and is implemented in the same way as pointers.

    • The type of pointer corresponding to the underlying reference variable:

    • Type of reference variable entity * Const


Different points:


    • References must be initialized; pointers are not required.

    • A pointer of a normal type can point to any object of the same type at any time, and a reference to another entity cannot be referenced once it is referenced.

    • Pointer + +: Point to the next address; reference + +: Give value + +.

    • In sizeof, the meaning is different: The reference result is the size of the reference type, and the pointer is always the number of bytes in the address * space.

    • Pointers need to be addressed manually, whereas references are addressed by the compiler.

    • References are relatively safe to use than pointers.


Name space



In C + +, variables, functions, and classes exist in large numbers, and the names of these variables, functions, and classes will all exist in the global namespace, causing many conflicts, using namespaces to localize the names of identifiers to avoid naming conflicts or name contamination.


    • Definition of namespaces

/ / Namespace namespace N1

{ int a = 30; void FunTest()

     { printf("N1::FunTest()\n");

     }

}//N1 namespace int a = 20; void FunTest()

{ printf("::FunTest()\n");

}//In the main scope int main()

{ int a = 10; printf("%d\n", a); printf("%d\n", ::a);

     ::FunTest(); printf("%d\n", N1::a);

     N1::FunTest(); return 0;

}//The namespace's nested namespace N2

{ int a = 40; void FunTest()

     { printf("N2::FunTest()\n");

     } namespace N3

     { int a = 50; void FunTest()

         { printf("N2::N3::FunTest()\n");

         }

     }

}int main()

{

     N2::FunTest();

     N2::N3::FunTest(); return 0;

}// In the same project, multiple namespaces with the same name are allowed, and the compiler will eventually synthesize into the same namespace namespace N1.

{ int b = 70; void Test()

     { printf("N1::Test()\n");

     }

}
    • Description
      A. A namespace defines a new scope, and all content in the namespace is confined to that namespace.
      B. Namespaces that do not have a name can only be used in the current file, and the variables defined in it are equivalent to the global variables within the project.

    • Use of namespaces

/ / Namespace use namespace N1

{ int a = 1; int b = 2; int c = 3; /*void FunTest1()

     {}

     Void FunTest2()

     {}*/}//法二: using N1::b;//法三:using namespace N1;int main()

{ int a = 4; //法一:

     Printf("a=%d\n", N1::a);//a=1

 

     Printf("b=%d\n", b);//b=2

     Printf("c=%d\n", c);//c=3}


C + + input and output:



Code


//C++ input and output #include <iostream>using namespace std;//std standard namespace int main()

{ int a = 10; double b = 3.14; char c = 'c'; cout << a ; cout << b << '\n'; cout << c << endl; cout << a << " " << b << " " << c << endl; cin >> a ; cin >> b >> c; return 0;

}// cout: standard namespace re-export stream object <<output operator // cin: standard namespace re-input stream object >> input operator 
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.