13.Basic - C++

3 minute read

12.Basic - C++



#include <iostream>
#include <cctype>
#include <cstdlib>
#include <new>

// 36. Function Pointers
void FileFunc(){
    std::cout << "File Function \n";
}

// 37. Pointer to Pointers
void FileCredit(float** fpp){
    while(**fpp != 0) {
        if (**fpp<0){
            break;
        } else {
            (*fpp)++;
        }
    }
}

// 38.const Pointer Arguments
void CopyUpper(char* s1, const char* s2){
    while ( ( *s1++ = (toupper(*s2++))) !='\0');
}

// 39. The new and delete operators
struct Date {
    int day;
    int month;
    int year;
};

//43. Passing References
struct bigone {
    int serno;
    char text[1000];
};
void slowfunc(bigone p1){
    std::cout << p1.serno << '\n';
    std::cout << p1.text << '\n';
};
void fastfunc(bigone& p1){
    std::cout << p1.serno << '\n';
    std::cout << p1.text << '\n';
};


// 44. Returning References
struct Date2 {
    int day,month,year;
};
Date2 birthdays[] = {
    {17,12,37},
    {31,22,13},
    {76,52,32},
    {27,12,37},
    {67,12,37},
};

const Date2& getdate(int n){
    return birthdays[n-1];
};



// 45. Const Reference
struct Date3 {
    int day, month, year;
} dt3 = {1,2,98};


// 46. Const Reference Parameter
void DisplayDate(const Date3& dr){
    std::cout << dr.day << ' '
                << dr.month << ' '
                << dr.year << ' ' ;
};


// 47. Stringizing Operators
#define Error(n) std::cout << "\nError "  #n << '\n'

// 48. Token Concatenation Operators
#define BookChaperVerse(b,c,v) b ## c ## v


// 49. More Concatenation
#define AbleBaker "alpha beta"
#define cat(a,b) a ## b


// 50. If & End-If
#define DEBUG 1
#define DEBUG2

int main(int argc, const char * argv[]) {
    // 36. Function Pointers
    void (*funcp)(); // 1 make function
    funcp = FileFunc; // 2 put address
    (*funcp)(); // 3 run function
    
    // 37. Pointer to Pointers
    // double pointer = pointer의 메모리 주소를 저장
    float vals[] {34.12, 76.12, 43.09, -92.14, 84.32,0};
    float* fp = vals;
    FileCredit(&fp);
    std::cout << *fp << '\n';
    
    // 38.const Pointer Arguments
    char rcv[25];
    const char snd[] = "han\n";
    CopyUpper(rcv, snd);
    std::cout << rcv;
    
    // 39. The new and delete operators
    Date* birthday = new Date;
    birthday->day = 20;
    birthday->month = 8;
    birthday->year = 2020;
    std::cout << "Born in = " << birthday->day <<'.'
    << birthday->month <<'.' << birthday -> year << '\n';
    
    
    // 40.New Delete & Dynamic Arrays
    int size = 3;
    int *array = new int[size];
    for( int i = 0 ; i < size ; i++){
        array[i] = std::rand();
    }
    for( int i =0; i <size ; i++){
        std::cout << '\n' << array[i];
    }
    delete [] array;
    std::cout << '\n';
    
    
    // 41. References
    int actualint = 123;
    int &otherint = actualint;
    std::cout << actualint << " = ";
    std::cout << otherint << '\n';
    otherint++;
    std::cout << actualint << " = ";
    std::cout << otherint << '\n';
    actualint++;
    std::cout << actualint << " = ";
    std::cout << otherint << '\n';
    
    // 42. Addresses of References
    int ac = 123;
    int &other = ac;
    std::cout << &ac << " = " << &other << '\n';
    
    // 43. Passing References
    static bigone bo = {123,"This is a BIG structure"};
    slowfunc(bo);
    fastfunc(bo);

    // 44. Returning References
    int dt = 3;
   
    if (dt > 0 && dt < 6){
        const Date2& rd = getdate(dt);
        std::cout << rd.day << '/'
        << rd.month << '/'
        << rd.year << '/' << '\n';
    }
    
    // 45. Const Reference
    const Date3& dr = dt3;
    std::cout << dr.day << '/'
    << dr.month << '/'
    << dr.year << '/' << '\n';
    
    
    // 46. Const Reference Parameter
    //const Date3& dr = dt3;
    DisplayDate(dr);
    
    
    // 47. Stringizing Operators
    Error(53);
    
    // 48. Token Concatenation Operators
    unsigned bcv = BookChaperVerse(5, 12, 3);
    std::cout << bcv << '\n';
    
    
    // 49. More Concatenation
    std::cout << cat(Able,Baker) << '\n';
    
    
    // 50. If & End-If
    #if DEBUG
    std::cout << "Debugging \n";
    #endif
    std::cout << "Running \n";
    
    #if defined DEBUG2
        std::cout << "Debugging2 \n";
    #endif
        std::cout << "Running2 \n";
    
    
}    
    
    
    
    
}







Leave a comment