The c ++ language's learning note code and note comment "1", learning note "1"

Source: Internet
Author: User

The c ++ language's learning note code and note comment "1", learning note "1"

The specific notes are written in the code in the form of annotations, and each knowledge point is represented in the form of functions.

1 # include <iostream>; 2 # include <cmath> 3 4 const double PI = 3.1415926; 5 int func (int & a, int & B) {6 int c = a + B; 7 int t = a; 8 a = B; 9 B = t; 10 // value reference, with semantic reference 11 // exchange, the value of B, which is changed from memory. 12 return c; 13} 14 15 int func1 () {// & get address; 16 int P; 17 P = 10; 18 std :: cout <"" <& P <std: endl; 19 return 0; 20 21 // The inline keyword indicates that this function is recommended to be compiled as an inline function during compilation, 22 // that is, embedding the function body in the called position. 23} 24 inline double func2 (double l) {25 return PI * l; 26 // when the function body is simple enough, the overhead of function call and return can be reduced. 27} 28 29 30 31 // use of the constexpr function. 32 // note that the return value must be a constant value. 33 34 constexpr int func3 () {35 return 20; 36 37} 38 // use of the default parameter value function. 39 int func4 (int a, int B = 10, int c = 12) {40 if (B! = 10) {41 std: cout <"B value:" <B <"\ n"; 42 std :: cout <"not the default function value"; 43} else if (c! = 12) {44 std: cout <"c value: \ n"; 45 std: cout <"c is not the default function value! \ N "; 46} else {47 std: cout <" B, c is the default value \ n "; 48 49} 50} 51 52 // method of using the function for overloading 53 // Note: The parameter name cannot distinguish between overload functions, and the return value cannot be different from that of overload functions, do not declare functions of different functions as overload functions to avoid confusion. 54 // The first case is that the parameter types are different, in the second case, the number of parameters is different 55 // The first (similar to the second) 56 57 int sumOfsquare (int a, int B) {58 return a + B; 59} 60 double sumOfsquare (double a, double B) {61 return a + B; 62} 63 64 // variable length parameter table, variable parameter. 65 // 1. The parameter types of standard library type 66 // initialer_list using initialer_list must be the same. 67 // 2 if the real parameters have different types, you need to compile a variable parameter template. 68 69 70 // initialer_list its object is always a constant value and cannot be changed. 71 // there can be other types of parameters. 72 // main methods of initialer_list: 73 // initialer_list <T> lst; default initialization; T-type empty list 74 // initialer_list <T> lst {a, B, c ...}; 75 // lst. number of elements in the size () List 76 // lst. begin returns the pointer to the first element of lst 77 // lst. end () returns the pointer 78 79 int main () {80 // int func (int & a, int & B); 81 int a, B; 82 a = 10; 83 B = 12; 84 std: cout <"prior to assignment a:" <a <", prior to assignment B: "<B <std: endl; 85 int ls = func (a, B); 86 std: cout <" id of: "<& ls <std: endl; 87 std: cout <" A = "<a <", "<" B = "<B <std: endl; 88 // int func1 (); 89 func1 (); 90 double pi = func2 (2.00000000); 91 std: cout <pi <"\ n"; 92 std: cout <"usage of the constexpr function: "<func3 () <" \ n "<std: endl; 93 func4 (4, 12); 94 int sum = sumOfsquare (2, 4 ); 95 double sum1 = sumOfsquare (2.111, 2.0101); 96 printf ("\ n % d %. 10f ", sum, sum1); 97 std: cout <" \ nint sum: "<sum <" \ ndouble sum: "<sum1 <" \ n "; 98 std: cout <sin (30); 99 // cmat The function value in h needs to be input in radians ." Use of system functions. 100 return 0; 101}

 

Running result

1/home/leo/CLionProjects/yinyong/cmake-build-debug/yinyong 2 values before a: 10, before B: 12 3 id of a: 0x7ffeab257ac0 4 a = 12, B = 10 5 0x7ffeab257a84 6 12.5664 7 usage of the constexpr function: 20 8 9 B value: 1210 not the default value of the function 11 6 4.121100000012 int sum: 613 double sum: 4.121114-0.98803215 Process finished with exit code 0

 

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.