Getting Started with C ++ first post Hello Word

C ++ based tutorial
                                                                                                Author: lanneret Time: 2018-06-23
1, basic grammar
<> Is the reference path compiler library header files inside
"" Header files referenced are relative paths in your program directory
 
2, namespaces
    
 1 namespace;
 2 #include<iostream>
 3 using namespace std;
 4 namespace A{
 5       void fun(){
 6         cout<<"A"<<endl;
 7     }
 8 }
 9 namespace B{
10       void fun(){
11         cout<<"B"<<endl;
12     }
13     namespace C{ 
14         struct Tearche{
15                 char name[10];
16                  int age;
17     };
18       void fun(){
19         cout<<"C"<<endl;
20     }
21  
22 }
23 }
24 int main(){
25  
26 }

3, the output stream

    
#include<iostream>
using namespace std;
int main () {
    cout <<"I love C++ "<<endl;
    int i=0;
    cout << "Please enter an integer:" << endl;
    cin>>i;
    cout << "output decimal" << endl;
    cout<<dec<<i<<endl;
    cout << "output octal" << endl;
    cout<<otc<<i<<endl;
    cout << "output hex" << endl;
    cout<<hex<<i<<endl;
 
    cout << "Please enter a Boolean value (0 or 1):" << endl;
     bool bl=false;
    cin>>bl;
    cout<<boolalpha<<i<<endl;
    return 0;
}

4, practicality increase

    Anywhere in the definition of variables
 
5, detection enhancement function type
    Define the same global or local variables 
    C: the same variable may be performed, directed to the same address
    C ++: not allowed
 
6, struct types to strengthen
    C     :struct Teacher teatch;
    C++:Teacher teach
 
7, C ++ type must specify the type of the function, the variable
        void test1(i){cout <<i<<endl;}
        test2(i){return i;}
8, Bool type
   
Only true and false, that is, really pay attention to non-zero, 0 is the false;
C ++ syntax
if(flag) {
    ;
 
} else{}  
  bool  flag = ture;  
   //flag = 5;
   //flag = -5;
   cout<<"flag="<<flag<<endl;
   bool flag1 = false;
   cout<<"flag1="<<flag1<<endl;

C language

if(flag==1){
;
}else{}
 
Combat application: do flag
9, three head operation
    C language returns the value of the variable, in registers, it is not operational when the left eye three values;
C: -----------------------------------------
    int a=10,int b=10;
        (? A> b a: b) = 10; // error
       int fun () {return (a> b a:? b);} // can
       * (A: b & a:? & B) = 15; // returns the address of a variable that is variable itself
   
 C++:--------------------------------------------------------
    Return variable itself, i.e. including an address to access, can be left when the value;
       int a=10,int b=10;
        (? A> b a: b) = 10; // correct
Trinocular actual application operation: confirming parameters in the correct range;
 
10 Exercises
 
/ * BOOL, input and output streams, namespace ** * / 
/ * * BOOL by user input, to achieve the maximum or minimum output * * / 
/ * ***************************************************** **************************************** * / 
#include <the iostream>
 the using  namespace STD ;
 // namespace COMPARE 1 { 
int GetMaxorMin ( int * ARR, int COUNT, isMax) {
    int TEMP = ARR [ 0 ];    
         for ( int I = . 1 ; I <COUNT; I ++ ) {
            IF (isMax) {
                            IF (TEMP < arr [i]) {
                                temp =  arr[i]
                            } 
                    }else{
                              if(temp>arr[i]){
                                temp =  arr[i]
                            } 
                    }
    }
    return temp;
 
}
// } 
int ()
{
   int arr_t[10] = {10,7,5,1,8,4,3,60,20,16};
    COUT << " Enter 1 for the maximum, selects the minimum input 0: " << endl;
     BOOL isMax = to false ;
    cin>>isMax
    if(isMax){
        COUT << " Array maximum " << GetMaxorMin (arr_t, 10 , isMax) << endl;
    }else{
        COUT << " array minimum " << GetMaxorMin (arr_t, 10 , isMax) << endl;
    }
 
 
     return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/zhangjixiao/p/12005691.html