(Basic) C + + syntax _1

Source: Internet
Author: User

I think the comments are detailed, haha.

<pre name= "code" class= "CPP" >//namespaces use example. cpp #include <iostream>using namespace std; Space STD covers all standard C + + definitions and declarations namespace one//defines a namespace one with 1 constants m and a variable inf {const int m = 200;int inf = 10;}//followed by no semicolon  names Pace two//defines a namespace two with two variables {int x;int inf =-100;}  



The domain resolver really has global scope for global variables. cpp #include <iostream> #include <iomanip>//? Using namespace Std;intsum = 5000;//defines the global variable Sumint main () {int arr[3] = {10, 20, 30};//a small program block starts with {int i, sum = 0;for (int i = 0 ; I < 3; i++) {sum + = Arr[i];} cout<< "Local sum:" <<sum<<endl;//output local sum:: Sum + = sum;//through the domain resolver to global sum access in the scope of the local variable with the same name cout<< " Global sum: "<<::sum<<endl;} Sum *= 2;//This is the global sumcout<< "total sum:" <<::sum<<endl;int sum = 200;//define another sumcout<< "Local sum:" <<sum<<endl;::sum + = sum;//10120 + cout<< "Global sum:" <<::sum<<endl;return 0;  

Formal parameters function definitions with default parameter values and invocation examples. cpp #include <iostream>using namespace std;void fun (int i, int j = 5, int k = 10);//prototype declaration of parameter J and k Specify the default parameters 5 and 10int respectively main () {fun (20); Fun (14, 30); Fun (0); }  void Fun (int i, int j, int k)//is defined in the prototype, can no longer be specified in the defined header {cout<<i<< "" <<j<< "" <<k<< Endl;}



Two-number multiplication (wrong) is achieved by using a macro definition. cpp #include <iostream>using namespace std; #define MULTIPLY (x, y) x*y//Note: There are no parentheses on either side of X and Y, there is a hidden danger int Main () {int a = Multiply (3 + 4, 2 + 3);//expanded to: int a = 3 + 4*2 + 3cout<< "a =" <<a<<endl;//output 14, original plan is 7*5 = return 0;  


//two-number multiplication with inline functions. cpp #include <iostream>using namespace Std;inline int Multiply (int x, int y)//In Object-oriented programming, the member functions defined in the class are automatically interpreted as inline functions, re Turn x * y;//no need to add the inline keyword}int main () {int a = Multiply (3 + 4, 2 + 3), cout << "a =" << a << endl;//used After the inline function, it really avoids the macro definition of that error, the return 0;//output value is ' {} ' 
Examples of overloaded functions. CPP//In the C + + language, for the function is exactly the same or similar, just the number of formal parameters,//type, the order of different functions with the same function name to name,//the same name function is collectively referred to as overloading (overload) #include < Iostream>using namespace Std; int square (int x)//The 1th version of the overloaded function, int type parameter {return x * x;}  float square (float x)//2 version {return x * x;} Double square (Double x = 1.5)//3 Version {return x * x;} int main () {cout << "square () =" << square () << endl;//calls the third version because it has a default value of cout << "square" = "&l t;< Square << endl;//call the first version cout << "Square (2.5f) =" << Square (2.5f) << Endl;//2editioncou T << "Square (1.1) =" << Square (1.1) << Endl;return 0;}



(Basic) C + + syntax _1

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.