11.Basic - C++

2 minute read

11.Basic - C++




#include <iostream>

//19. Global scope
int counter;
void AddCounter(int intr){
    counter += intr;
};

// 21. File scope
static int counter3;
static void Addcounter3(int intr){
    counter3 += intr;
};

// 22. Static storage class
int Gather(){
    static int wool = 50;
    return ++wool;
};


// 23. Extern storage class
float amount3;
void AccCalculate(){
    amount3 = 5.75;
};

// 26. Struct Data Type
struct Date {
    short int day,month, year;
};

// 27. Structures within structures
struct Employee {
    int EmpNum;
    float salary;
    Date datehired;
};

// 28. The Union Data Type
union Holder {
    int hint;
    char hchar;
};
void DisplayHoder(Holder hld, char * t){
    std::cout << "\nInit'd " << t << "\n";
    std::cout << "hint " << hld.hint << "\n";
    std::cout << "char " << hld.hchar << "\n";
}






int main(int argc, const char * argv[]) {
    // 19. Golbal Scope
    AddCounter(53);
    std::cout << "counter = " << counter << "\n";
    
    // 20. Local Scope
    int i3 = 123;
    if (i3>0){
        int j = 456;
        if (j>0){
            int k = 700;
            std::cout << i3 << ' ' << j << ' ' << k << "\n";
        }
    }
    
    // 21. File scope
    Addcounter3(1040);
    std::cout << "counter = " << counter3 << "\n";
     
    
    // 22. Static Storage Class
    int gwool = 0;
    while(gwool<52){
        gwool = Gather();
        std::cout << gwool << '\n';
    }
    
    
    // 23. Extern storage class
    extern float amount3;
    AccCalculate();
    std::cout << amount3 << " \n";
    
    
    // 24. Register storage class
    // register 키워드를 통해 main 메모리가아닌 cpu와 가까운 레지스터 공간을 사용하여 연산속도를 향상
    register unsigned int ctr;
    for (ctr = 100 ; ctr < 200 ; ctr += 50) {
        std::cout << ctr << "\n";
    }
    
    
    // 25. Const Type Qualifier
    // const는 변수에 재선언, 재할당 불가능 - let은 변수 재선언 불가능, 재할당 가능
    const int ex = 200;
    for (int ctr = 100 ; ctr < ex ; ctr += 50) std::cout << "ctr - " << ctr << "\n";
    
    
    // 26. Struct Data Type
    Date dt;
    dt.day = 20;
    dt.month = 1;
    dt.year = 2016;
    std::cout << dt.day <<'.'  << dt.month <<'.'  << dt.year  << "\n";
    
    
    // 27. Structures within structures
    Employee jee = {2,100000, {24,10,1989}};
    std::cout << "\nEmp # " << jee.EmpNum
            << "\nSalary = " << jee.salary
    << "\nBirth_Date = " << jee.datehired.day <<'.'  << jee.datehired.month <<'.'  << jee.datehired.year << '\n';
    
    // 28. The Union Data Type
    // 변수들이 메모리를 공유하기 때문에 데이터 통신에 사용
    // 보낼때 buffer로 한번에 보내고 받을때 struct의 변수들로 각각 받을수있다.
    Holder hld;
    hld.hchar = 'A';
    DisplayHoder(hld,"Char");
    hld.hint = 77;
    DisplayHoder(hld,"int");
    
    
    
    
    
    return 0;
}





Leave a comment