C++ Programming 1

LuuuuucyΒ·2024λ…„ 10μ›” 21일

C Programming study review

λͺ©λ‘ 보기
8/8

πŸ”– REVIEW

❗ μ„ μƒλ‹˜μ˜ 말씀

  1. class λ•Œλ¬Έμ—λΌλ„ c보닀 c++을 써야 함
  2. ν”„λ‘œκ·Έλž¨μ€ μ½”λ“œ(ν•¨μˆ˜)와 데이터(λ³€μˆ˜)둜 이루어져 있음 -> cμ–Έμ–΄μ˜ 단점은 μ½”λ“œμ™€ 데이터가 λΆ„λ¦¬λ˜μ–΄ 있음 -> c++μ–Έμ–΄λŠ” 이걸 ν•œ ꡰ데둜 λͺ¨μ•„λ†“μ•˜μŒ -> 클래슀

❗ C μ–Έμ–΄

1. shallow copy vs. deep copy

deep copy: μ‹€μ œ 데이터λ₯Ό 볡사
shallow copy: μ£Όμ†Œλ§Œ 볡사

❗ C++ μ–Έμ–΄

1. namespace

#include <iostream>
// using namespace std; // λ‚˜λ§Œμ˜ κ³ μœ ν•œ 이름

int main()
{
   printf("%d\n", 1234);
   std::cout << 1234; // stdλΌλŠ” namespace에 λ§Œλ“€μ–΄μ Έ μžˆλŠ” κ±° 
   // --> global namespace
   return 0;
}

2. λ‹€λ₯Έ μ’…λ₯˜μ˜ ν•¨μˆ˜ λ§Œλ“€κΈ°

int main()
{
    std::cout << 1234 << ' ' << 3.14 << ' '<< "abc" << '\n';
}

[좜λ ₯κ°’]
1234 3.14 abc

3. endl μ‚¬μš©ν•˜κΈ°

int main()
{
	cout << 5678 << endl;
}

[좜λ ₯κ°’]
5678
-> endl = '\n'

4. default parameter

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

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

[좜λ ₯κ°’]
20
30

5. reference

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

int main()
{
    int& t = k;     // & : μ°Έμ‘°(reference)
    t = 99; 
    change(k);
    cout << k << endl;
}

6. string

int main()
{
    string s = "my name is kim"; //string은 classμž„.. ν•˜μ§€λ§Œ μ‹ κ²½μ“°μ§€ μ•Šμ•„λ„ 됨 
    cout << s << endl;
    cout << s.length() <<endl;
     cout << s[0] << s.operator[](0) << endl;
}

[좜λ ₯κ°’]
my name is kim
14
mm

7. class

클래슀λͺ… = 카멜 ν‘œκΈ°λ²•

클래슀 = ν•¨μˆ˜ + λ³€μˆ˜

class Point // struct -> class
{
    int x, y;
};

int main()
{
    Point pt;

    return 0;
}

8. class public/private

public: λͺ¨λ“  μ½”λ“œ(λˆ„κ΅¬λ‚˜) μ ‘κ·Ό
protected: κ΄€λ ¨ μžˆλŠ” μ½”λ“œ(상속) μ ‘κ·Ό
private: λ‚΄ μ½”λ“œ(λ‚˜λ§Œ) μ ‘κ·Ό

class Point // struct -> class
{
    public:
    int x, y;
};

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

int main()
{
    Point pt;

    show(pt);

    return 0;
}

9. μƒμ„±μž

class Point // struct -> class
{
public:
    int x, y;

    // μƒμ„±μž(constructor)
    Point()
    {
        cout << "c`tor" << endl;
        x = y = 0;
    }
 };

10. 볡사 μƒμ„±μž(copy constructor)

원본 μˆ˜μ • λ°©μ§€

Point(const Point &pt)
{ 
   cout << "c`tor" << endl;
   x = pt.x;
   y = pt.y;
}

11. μ†Œλ©Έμž(destructor)

객체의 수λͺ…이 끝날 λ•Œ μžλ™μœΌλ‘œ 호좜

~Point()
{
    cout << "d`tor" << endl;
}

12. this

λ§€κ°œλ³€μˆ˜μ™€ 이름이 같은 경우, ν˜Όλ™μ„ ν”Όν•˜κΈ° μœ„ν•΄μ„œ μ‚¬μš©

class Rect
{
public:
    Point p1, p2;

    Rect(const Point &p1, const Point p2) : p1(p1), p2(p2)
    {
        cout << "Rect" << endl;
        this->p1.show();

        this->p1 = p1;
        this->p2 = p2;
    }
};
    
profile
Hi, I am Lucy. Welcome to Moon in the Room. 🌝

0개의 λŒ“κΈ€