C + + improvements (2)

Source: Internet
Author: User

This article address: http://www.cnblogs.com/archimedes/p/cpp-change2.html, reprint please indicate source address

Difference One: The difference of the prototype declaration

Concept of the prototype declaration:
The function is defined and reused, and if you use it first, you must use the prototype declaration

#include <iostream>using namespacestd;intMain () {floatAddfloatXfloaty);//Prototype Declaration   floatA,b,c; cout<<"Please enter a, B:"; CIN>> a >>b; C=Add (A, b); cout<<"sum= "<< c << Endl;   return 0;}floatAddfloatXfloaty) {   floatZ; Z= x+y; returnZ;}

Attention:

① declaration statement must be semicolon-delimited!

② position arbitrary, only different scopes

The reason for the ③ declaration is that it tells the compilation environment the number, type, and order of the function arguments

In ④c and C + +, any type of function that uses a post-definition requires a prototype declaration!

Difference: The meaning of the prototype is different from empty

void Fun   (); void  Fun (void);

C + +: The two forms are considered to be non-parametric

C: Think that the first one may have multiple parameters the second one without a parameter

Difference two: location of local variable definition

Difference Three: Domain parsing:: Widening the visible range of global variables
#include <iostream>#include<string>#include<iomanip>using namespacestd;intsum =5050;intMain () {intarr[3], I; cout<<"input 3 num:"<<Endl;  for(i=0; i<3; i++) Cin>>Arr[i]; intsum =0;  for(i=0; i<3; i++) Sum+=Arr[i];  for(i=0; i<3; i++) cout&LT;&LT;SETW (4) <<arr[i]<<Endl; cout<<"Local sum="<< sum <<Endl; :: Sum += sum; cout<<"Global sum="; cout<<:: Sum <<Endl; System ("PAUSE"); return 0;}
Difference Four: A function with default parameters

Meaning: A function of the initial value of a formal parameter

#include <iostream>#include<string>#include<iomanip>using namespacestd; void fun (int i, int j = 5, int k = ten); intMain () {Fun ( -); Fun ( -, -); Fun ( -, -, +); System ("PAUSE"); return 0;}voidFunintIintJintk) {cout<<"i="<<I<<"j="<<J<<"k="<<k<<Endl;}

Attention:

1. Parameters with default parameter values must be at the right end of the parameter table;
int f (int a,int b=0,int c); X

2, the default value must be notified to the compilation system before the function call;

3, Declaration and definition at the same time give the default value, some compiler error, some do not. It is best to give the default value only when the function is declared;

4, the parameter with the default value, the argument is given when the argument, the actual parameter value takes precedence

#include <iostream>using namespacestd;intMaxintAintBintC=6);intMain () {intresult =0; Result= Max (3,4,5); cout<<result<<Endl; System ("PAUSE"); return 0;}intMaxintAintBintc) {    intT; T= a > B?a:b; C= c > t?c:t; returnC;}
Difference five: inline functions

1. Call mode

2. Define the method: add inline on the left end of the function

#include <iostream>using namespaceStd;inlineintMaxintAintBintc);intMain () {intI=Ten, j= -, k= -, M; M=Max (i,j,k); cout<<"max="<<m<<Endl; System ("PAUSE"); return 0;}intMaxintAintBintc) {    intT; T= a > B?a:b; C= c > t?c:t; returnC;}

Attention:

You can write inline at the same time as you define and declare functions, or you can write inline in one place

Only small and frequently used functions are defined as inline functions

Cannot contain complex control statements in an inline function

Inline declarations on functions are recommended, not as inline functions are handled

In short, small, frequently used functions suitable as inline

The member functions defined within the class will be interpreted as inline, without the prior inline

class declaration, a function defined outside the class is not the default inline

Difference Five: function overloading

Overloading premise: The overload occurs in the same scope
Because a locally declared name in a function in C + + will mask the name declared within the global scope!

#include <iostream>using namespacestd;intSquareintx) {    returnx*x;}floatSquarefloatx) {    returnx*x;}DoubleSquareDoublex=1.5){    returnx*x;}intMain () {cout<<"Square ()"<<square () <<Endl; cout<<"Square (Ten)"<<square (Ten) <<Endl; cout<<"Square (2.5f)"<<square (2.5f) <<Endl; cout<<"Square (1.1)"<<square (1.1) <<Endl; System ("PAUSE"); return 0;}

Attention:

① overload function parameter number, parameter type, parameter order 3 must have at least one difference, the return value is not used as overloading basis

② overloaded functions should be similar in function

The ③main () function cannot be overloaded

④ parameter types are best guaranteed to be consistent, inconsistencies are automatically converted but unsuccessful conversions can be an error

⑤ overloads are used with functions with default values,may produce ambiguity
#include <iostream>using namespacestd;intFunintXinty=Ten){    returnx*y;}intFunintx) {    returnx;}intMain () {Fun (5);//error:An ambiguous call to an overloaded functionSystem ("PAUSE"); return 0;}

C + + improvements (2)

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.