C++ Programming

dongwoo·2024년 10월 21일

day 1

  1. basic
c 	-> *.c , gcc
c++ -> *cpp, g++
// io
#include <iostream>
using namespace std;

int main()
{
    cout << 1234 << ' ' << 3.14 << ' ' << "abc" << '\n';
    cout << 5678 << endl;
  	cout << true << ' ' << false << endl;
  	cout.operator<<(1234).operator<<(5678);
   
  	int a;
  	double b;
  	cin >> a >> b;
  	cout << a << ' ' << b << endl;
  
  	string s = "my name is Kim";
  	cout << s << endl;
  	cout << s.length() << endl;
  	cout << s[0] << s.operator[](0) << endl;
    
	return 0;
}
#include <iostream>
using namespace std;

// default parameter
void show_my_age(int age = 20) 
{
    cout << "age : " << age << endl;
}

// &: reference
void change(int &z)
{
    z = 33;
}

int main()
{
    show_my_age();
    show_my_age(30);

    int k = 7;
    cout << k << endl;

    int &t = k;
    t = 99;
    cout << k << endl;

    change(k);
    cout << k << endl;

    return 0;
}
  1. class (생성자, 복사 생성자, 소멸자)
#include <iostream>
using namespace std;

// 프로그램 = 코드(함수) + 데이터(변수)
// 클래스 = 함수 + 변수

// public: 모든 코드(누구나) 접근
// protected: 관련 있는 코드(상속) 접근
// private: 내 코드(나만) 접근
// C++ class 는 default 가 private
class Point // struct 는 모두 대문자, class는 camel case
{
public:
    int x, y; // 멤버 변수

    // Point()
    // {
    //     cout << "c`tor" << endl;

    //     x = y = 0;
    // }

    // Point(int x, int y)
    // {
    //     cout << "c`tor" << endl;

    //     this->x = x;
    //     this->y = y;
    // }

    // 퀴즈
    // 앞에서 만든 생성자 2개를 1개의 생성자로 합쳐보세요

    // 생성자 (constructor)
    Point(int x = 0, int y = 0)
    {
        cout << "c`tor" << endl;
        this->x = x;
        this->y = y;
    }

    // 복사 생성자(copy constructor)
    Point(const Point &pt)
    {
        cout << "c`tor" << endl;
        x = pt.x;
        y = pt.y;
    }

    // 소멸자(destructor)
    ~Point()
    {
        cout << "d`tor" << endl;
    }

    void show() // 멤버 함수
    {
        cout << x << ' ' << y << endl;
    }
};

void show(Point pt) // 전역 함수
{
    cout << pt.x << ' ' << pt.y << endl;
}

int main()
{
    Point pt;

    show(pt); // 0 0
    pt.show(); // 0 0

    Point pt2(10, 20);
    pt2.show(); // 10 20

    Point pt3(30);
    pt3.show(); // 30 0

    return 0;
}
  1. class (연산자 오버로딩, Rect 예제)
#include <iostream>
using namespace std;

class Point
{
public:
    int x, y;

    Point(int x = 0, int y = 0) : x(x), y(y)
    {
        // this->x = x;
        // this->y = y;
    }

    // 연산자 오버로딩
    const Point &operator=(const Point &pt)
    {
        cout << "= operator" << endl;

        x = pt.x;
        y = pt.y;

        return *this;
    }

    void show()
    {
        cout << x << ' ' << y << endl;
    }
};

// 퀴즈
// 참조 변수를 사용해서 int 변수를 swap 하는 함수를 만드세요.
void swap(int &a, int &b)
{
    int t = a;
    a = b;
    b = t;
}

// 퀴즈
// Point 변수 2개를 갖는 Rect 클래스를 정의하세요.
class Rect
{
public:
    Point pt1, pt2;

    Rect() : pt1(0, 0), pt2(0, 0)
    {
        cout << "c`tor [Rect]" << endl;
    }

    Rect(const Point &pt1, const Point &pt2) : pt1(pt1), pt2(pt2)
    {
        cout << "c`tor [Rect]" << endl;
        // this->pt1 = pt1;
        // this->pt2 = pt2;
        adjust();
    }

    Rect(int x1, int y1, int x2, int y2) : pt1(x1, y1), pt2(x2, y2)
    {
        cout << "c`tor [Rect]" << endl;
        // pt1.x = x1;
        // pt1.y = y1;
        // pt2.x = x2;
        // pt2.y = y2;
        adjust();
    }

    void show()
    {
        pt1.show();
        pt2.show();
    }

    int width()
    {
        return pt2.x - pt1.x;
    }

    int height()
    {
        return pt2.y - pt1.y;
    }

    int area()
    {
        return width() * height();
    }

private:
    void adjust()
    {
        if (pt1.x > pt2.x)
            swap(pt1.x, pt2.x);

        if (pt1.y > pt2.y)
            swap(pt1.y, pt2.y);
    }
};

int main()
{
    Rect rc;
    rc.show();

    Point pt1(10, 20), pt2(30, 40);
    Rect rc2(pt1, pt2);
    rc2.show();

    Rect rc3(Point(50, 60), Point(70, 80));
    rc3.show();

    Rect rc4(10, 30, 50, 70);
    rc4.show();

    Rect rc5(50, 70, 10, 30);
    rc5.show();
    cout << rc5.width() << endl;
    cout << rc5.height() << endl;
    cout << rc5.area() << endl;

    Point pt3(1, 2), pt4(3, 4), pt5(5, 6);

    pt3 = pt4 = pt5;
    pt3.show();

    return 0;
}
  1. class (Stack 예제)
#include <iostream>
using namespace std;

// 퀴즈
// C 버전으로 만든 스택을 클래스 버전으로 만드세요.

class Stack
{
private:
    int buf[256];
    int top;

public:
    Stack() : top(0)
    {
        cout << "Stack" << endl;
    }

    void push(int n)
    {
        buf[top++] = n;
    }

    int pop()
    {
        return buf[--top];
    }

    bool is_empty()
    {
        return top == 0;
    }
};

int main()
{
    Stack stack;

    int orders[] = {5, 1, 8, 3, 4, 9, 2, 1};
    int size = sizeof(orders) / sizeof(orders[0]);

    for (int i = 0; i < size; i++)
        stack.push(orders[i]);

    for (int i = 0; !stack.is_empty(); i++)
        orders[i] = stack.pop();

    for (int i = 0; i < size; i++)
        cout << orders[i] << ' ';

    return 0;
}
profile
hello world!

0개의 댓글