JiwonDev

2. Cpp์˜ ๋…ํŠนํ•œ ์‚ฌ์šฉ๋ฒ•

by JiwonDev

1. ์—ฐ์†์ ์ธ ๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ

#include <iostream>
using namespace std;

int main() {
    int A[5]={0,};

    for(int i=0; i<5; i++) // for
        cout << A[i] << " ";
    cout << "\n";

    for(int x:A) // for-each
        cout << x << " ";

    return 0;
}

 

2. Reference ๋ณ€์ˆ˜

call-by-reference ๋ฅผ ๋ณ€์ˆ˜์— ์ ์šฉํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ’์„ ๋ณต์‚ฌํ•˜์ง€ ์•Š๊ณ  ์ฃผ์†Œ๊ฐ’ ์ž์ฒด๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

์ฃผ์†Œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” & ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

#include <iostream>

using namespace std;

int main()
{
    int a = 10;
    int &r = a; // ๋ ˆํผ๋Ÿฐ์Šค ๋ณ€์ˆ˜ r
    r++;
    cout << r << endl; // 11
    cout << a << endl; // 11
}

์ฐธ๊ณ ๋กœ a = &r ์„ ํ•˜๊ฒŒ๋˜๋ฉด r์˜ ์ฃผ์†Œ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค. (๋ ˆํผ๋Ÿฐ์Šค ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค.)

 

2-1 ํฌ์ธํ„ฐ ๋ณ€์ˆ˜์™€ ๋ ˆํผ๋Ÿฐ์Šค ๋ณ€์ˆ˜

ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๋Š” ํฌ์ธํ„ฐ ๊ฐ’์„ ๋‹ด๋Š” 4๋น„ํŠธ ์ž๋ฃŒํ˜•์ด๊ณ 

๋ ˆํผ๋Ÿฐ์Šค ๋ณ€์ˆ˜๋Š” ์ตœ์ ํ™”. ์ฆ‰ ๋˜‘๊ฐ™์€ ๋‚ด์šฉ์„ ๋ณ€์ˆ˜์ด๋ฆ„ ํ•˜๋‚˜ ๋” ๋‘๊ณ  ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

๋ ˆํผ๋Ÿฐ์Šค ๋ณ€์ˆ˜์˜ ์žฅ์ ์€ ์“ธ๋ชจ์—†๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ .

#include <iostream>
using namespace std;

void swap_by_reference(int &a, int &b){ // ๋ณ€์ˆ˜ ์ž์ฒด๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค.
    int temp;
    temp = a;
    a = b;
    b = temp;
}

void swap_by_pointer(int *a, int *b){ // ์ฃผ์†Œ๊ฐ’์„ ์ „๋‹ฌ ๋ฐ›๋Š”๋‹ค.
    int temp;
    temp = *a; // ํฌ์ธํ„ฐ ๋ณ€์ˆ˜์˜ ์‚ฌ์šฉ๋ฒ•
    *a = *b; // *๋Š” ํ•ด๋‹น ์ฃผ์†Œ๊ฐ€ ๊ฐ€๋ฅดํ‚ค๋Š” ๊ฐ’.
    *b = temp;
}

int main()
{
    int a=10, b=20;
    swap_by_reference(a, b);
    cout << a << b << endl;

    swap_by_pointer(&a, &b);
    cout << a << b << endl;
}

 

3. Const (์ƒ์ˆ˜ํ™”)

์•ž์˜ 2๊ฐœ์˜ const์˜ ์˜๋ฏธ๋Š” ๊ฐ™๋‹ค. *๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์ž.

 

4. Struct (๊ตฌ์กฐ์ฒด)

๊ฐ„๋‹จํ•œ ๊ตฌ์กฐ๋Š” ๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ๋‹ค๊ณ ๋Š” ํ•˜๋Š”๋ฐ

์†”์งํ•˜๊ฒŒ ๊ตณ์ด ์จ์•ผํ•˜๋Š” ์ด์œ ๋Š” ๋ชจ๋ฅด๊ฒ ๋‹ค. C์–ธ์–ด์— ์žˆ์–ด์„œ ํ˜ธํ™˜์„ฑ ๋•Œ๋ฌธ์— ๋‚จ์€๊ฒŒ ์•„๋‹๊นŒ?

์ฐธ๊ณ ๋กœ ์ ‘๊ทผ์ง€์ •์ž๊ฐ€ public์ด๋ผ๋Š” ๊ฑธ ์ œ์™ธํ•˜๋ฉด Class์™€ ๋น„์Šทํ•˜๋‹ค.

#include <iostream>
using namespace std;

struct Rectangle {
    int length; // ๋ช…์‹œํ•˜์ง€์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์€ public
    int breadth; // ๊ทผ๋ฐ ์†”์งํžˆ struct๋ฅผ ์™œ ์“ฐ๋Š”์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค.
}; // ์„ธ๋ฏธ์ฝœ๋ก ์ด ๋ถ™๋„ค?

int main()
{
    struct Rectangle r = {10,5};
    struct Rectangle *p = &r;
    
    /* p = ๋™์ ํ• ๋‹น์„ ํ•  ๊ฒฝ์šฐ
    	p = (struct Rectangle*) malloc( sizeof(struct Rectangle) );
    */
    
    r.length = 15;
    cout << r.length << endl; // r.length = 15

    (*p).length = 25;
    cout << r.length << endl; // r.length == 25

    p->length=30;
    cout << r.length << endl; // r.length == 30
}

 

5. Class

ํด๋ž˜์Šค๋Š” ๋‹ค์Œ์žฅ์— ์ถ”๊ฐ€๋กœ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝ”๋“œ๋งŒ ๋ณด๊ณ ๊ฐ€์ž

#include <iostream>
#include <stdio.h>

using namespace std;

class Rectangle {
private: // ๋ช…์‹œํ•˜์ง€์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์€ private์ž…๋‹ˆ๋‹ค.
    int length;
    int breadth;

public:
    Rectangle() { // ์ƒ์„ฑ์ž
        length = 0;
        breadth = 0;
    }

    Rectangle(int l, int b) { // ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋ผ์ด๋”ฉ
        length = l;
        breadth = b;
    }
    
    ~Rectangle(){ // ์†Œ๋ฉธ์ž
        cout << "Destructor";
    }

    int area(){ // ๋„ˆ๋น„
        return length * breadth;
    }
    int perimeter(){ // ๋‘˜๋ ˆ
        return 2*(length+breadth);
    }
    
    void setLength(int l){ // setter
        length=l;
    }
    void setBreadth(int b){
        breadth=b;
    }
    int getLength(){ // getter
        return length;
    }
    int getBreadth(){
        return breadth;
    }

}; // cpp class๋Š” ์„ธ๋ฏธ์ฝœ๋ก ์„ ๋‹ต๋‹ˆ๋‹ค ใ…Žใ…Ž;

int main(){
    Rectangle r(10,5);

    cout << "Area " << r.area() << endl;
    cout << "Perimeter " << r.perimeter() << endl;
}

 

 

 

'๐ŸŒฑBackend > CPP' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

4. STL  (0) 2020.12.29
3. ์˜ˆ์™ธ์ฒ˜๋ฆฌ  (0) 2020.12.29
3. Class  (0) 2020.12.28
1. Hello World  (0) 2020.12.26

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ