๐ŸŒˆ๋ช…ํ’ˆ C++ ํ”„๋กœ๊ทธ๋ž˜๋ฐ 4์žฅ๐Ÿง‘โ€๐Ÿ’ป

Se0ng_1lยท2022๋…„ 6์›” 20์ผ
0

๋ช…ํ’ˆCPPํ”„๋กœ๊ทธ๋ž˜๋ฐ

๋ชฉ๋ก ๋ณด๊ธฐ
3/10
post-thumbnail

4์žฅ ์š”์ 

๊ฐ์ฒด ํฌ์ธํ„ฐ์™€ ๊ฐ์ฒด ๋ฐฐ์—ด, ๊ฐ์ฒด์˜ ๋™์  ์ƒ์„ฑ

๊ฐ์ฒดํฌ์ธํ„ฐ

	Circle coin;
    Circle *p; // Circle ํด๋ž˜์Šค์˜ ๊ฐ์ฒดํฌ์ธํ„ฐ p์„ ์–ธ
    p = &coin; // ๊ฐ์ฒดํฌ์ธํ„ฐ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•
    coin.getArea(); // ๊ฐ์ฒด๋กœ ๋ฉค๋ฒ„ํ•จ์ˆ˜ ํ˜ธ์ถœ
    p->getArea(); // ๊ฐ์ฒด ํฌ์ธํ„ฐ๋กœ ๋ฉค๋ฒ„ํ•จ์ˆ˜ ํ˜ธ์ถœ
    (*p).getArea(); // ๊ฐ์ฒดํฌ์ธํ„ฐ๋กœ ๋ฉค๋ฒ„ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๋Š” ๋˜๋‹ค๋ฅธ ๋ฐฉ๋ฒ•
    ๊ฐ์ฒด ๋ฐฐ์—ด
    

๊ฐ์ฒด ๋ฐฐ์—ด ์„ ์–ธ ๋ฐ ํ™œ์šฉ


    Circle arr[3]; // ๊ฐ์ฒด ๋ฐฐ์—ด ์ƒ์„ฑ

    arr[0].getArea(); // ๊ฐ์ฒด ๋ฐฐ์—ด ๋ฉค๋ฒ„ํ•จ์ˆ˜ ํ˜ธ์ถœ
    arr[1].getArea();
    arr[2].getArea();

    Circle *ps;
    ps = arr; // ๊ฐ์ฒด ๋ฐฐ์—ด ํฌ์ธํ„ฐ ์ดˆ๊ธฐํ™”
    for(int i = 0; i < 3; i++)
    {
        ps->getArea(); // ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ํ˜ธ์ถœ
        ps++; // ๋ฐฐ์—ด์˜ ๋‹ค์Œ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด
    }

๊ฐ์ฒด๋ฐฐ์—ด ํŠน์„ฑ

// ๊ฐ์ฒดํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฐฐ์—ด์„ ๋‹ค๋ฃจ๋Š” ๋‹ค์–‘ํ•œ ์‚ฌ๋ก€
// ๊ฐ์ฒด ๋ฐฐ์—ด์€ ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋  ๋•Œ ๋†’์€ ์ธ๋ฑ์Šค์˜ ์›์†Œ๋ถ€ํ„ฐ ์†Œ๋ฉธ๋œ๋‹ค.
for(int i = 0; i < 3; i++)
    {
        ps->getArea();
        ps[i].getArea();
        (ps + i)->getArea();
        ps++;
    }
/* 
์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•œ ๊ฐ์ฒด ๋ฐฐ์—ด ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•, Circle() = ๊ธฐ๋ณธ ์ƒ์„ฑ์ž
๋งŒ์•ฝ ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ ๊ฒฝ์šฐ ์ž๋™์œผ๋กœ 
๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์œผ๋ฏ€๋กœ ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
*/
Circle arr[3] = {Circle(),Circle(30), Circle(8)};
//๋‹ค์ฐจ์› ๊ฐ์ฒด ๋ฐฐ์—ด, ์ผ๋ฐ˜์ ์ธ ๋ฐฐ์—ด ์‚ฌ์šฉ๋ฒ•์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
Circle circles[3][8];

๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ๋ฐ ๋ฐ˜ํ™˜

ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์œ ๋™์ ์œผ๋กœ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
C์–ธ์–ด : malloc() / free()
C++ : new, delete

new : ๋™์  ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ํž™(heap)์œผ๋กœ๋ถ€ํ„ฐ ํ• ๋‹น๋ฐ›๋Š”๋‹ค.
delete : ํ• ๋‹น๋ฐ›์€ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ํž™(heap)์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

new ๊ธฐ๋ณธ ํ˜•์‹

๋ฐ์ดํ„ฐํƒ€์ž… *ํฌ์ธํ„ฐ๋ณ€์ˆ˜ = new ๋ฐ์ดํ„ฐํƒ€์ž…;

    int *pInt = new int;
    char *pChar = new char;
    Circle *pCircle = new Circle(); 

delete ๊ธฐ๋ณธ ํ˜•์‹

delete ํฌ์ธํ„ฐ ๋ณ€์ˆ˜;

    delete pInt;
    delete pChar;
    delete pCircle;

์ดˆ๊ธฐํ™”

๋ฐ์ดํ„ฐํƒ€์ž… *๋ฐ์ดํ„ฐ๋ณ€์ˆ˜ = new ๋ฐ์ดํ„ฐํƒ€์ž…(์ดˆ๊นƒ๊ฐ’);

    int *pInt = new int(20);
    char *pChar = new char('a');

๋ฐฐ์—ด์˜ ๋™์  ํ• ๋‹น ๋ฐ ๋ฐ˜ํ™˜

๋ฐฐ์—ด์˜ ๋™์  ํ• ๋‹น/๋ฐ˜ํ™˜ ๊ธฐ๋ณธ ํ˜•์‹

ํ• ๋‹น : ๋ฐ์ดํ„ฐํƒ€์ž… *ํฌ์ธํ„ฐ๋ณ€์ˆ˜ = new ๋ฐ์ดํ„ฐํƒ€์ž…[๋ฐฐ์—ด์˜ ํฌ๊ธฐ];

    int *pInt = new int[5];

delete ๊ธฐ๋ณธ ํ˜•์‹

๋ฐ˜ํ™˜: delete [] ํฌ์ธํ„ฐ๋ณ€์ˆ˜;

    delete [] pInt;

์ดˆ๊ธฐํ™”

    int *pInt = new int [] {1,2,3,4,5};

๊ฐ์ฒด์˜ ๋™์  ํ• ๋‹น ๋ฐ ๋ฐ˜ํ™˜

๊ฐ์ฒด์˜ ๋™์  ํ• ๋‹น / ๋ฐ˜ํ™˜ ๊ธฐ๋ณธ ํ˜•์‹

ํ• ๋‹น
ํด๋ž˜์Šค์ด๋ฆ„ ํฌ์ธํ„ฐ๋ณ€์ˆ˜ = new ํด๋ž˜์Šค์ด๋ฆ„;
ํด๋ž˜์Šค์ด๋ฆ„
ํฌ์ธํ„ฐ๋ณ€์ˆ˜ = new ํด๋ž˜์Šค์ด๋ฆ„(์ƒ์„ฑ์ž๋งค๊ฐœ๋ณ€์ˆ˜);

    Circle *p = new Circle;
    Circle *p = new Circle(30);

Tip : ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด์ƒ์„ฑ์‹œ ๋’ค์— ()๋ฅผ ๋ถ™์ด์ง€ ์•Š๋Š”๋‹ค.
ํ•จ์ˆ˜๋กœ ์ธ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

Circle pizza; // O
Circle pizza(); // X

ํ•˜์ง€๋งŒ ํฌ์ธํ„ฐ๋Š” ์ƒ๊ด€ ์—†๋‹ค.

Circle *p = new Circle; // O
Circle *q = new Circle(); // O

๊ฐ์ฒด ๋ฐฐ์—ด์˜ ๋™์  ํ• ๋‹น ๋ฐ ๋ฐ˜ํ™˜

๋ฐฐ์—ด์˜ ๋™์  ํ• ๋‹น / ๋ฐ˜ํ™˜ ๊ธฐ๋ณธ ํ˜•์‹

ํ• ๋‹น : ํด๋ž˜์Šค์ด๋ฆ„ *ํฌ์ธํ„ฐ๋ณ€์ˆ˜ = new ํด๋ž˜์Šค์ด๋ฆ„[๋ฐฐ์—ด ํฌ๊ธฐ];

    Circle *pArray = new Circle[3];

์ดˆ๊ธฐํ™”

    Circle *pArray = new Circle[3]{Circle(1),Circle(2), Circle(3)};

delete ๊ธฐ๋ณธ ํ˜•์‹

๋ฐ˜ํ™˜: delete [] ํฌ์ธํ„ฐ๋ณ€์ˆ˜;

    delete [] pArray;

this ํฌ์ธํ„ฐ

๊ฐ์ฒด ์ž๊ธฐ ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ

class Circle{
    int radius;
    ๋ฉค๋ฒ„๋ณ€์ˆ˜;
public:
    Circle() { this->radius = 1;}
    Circle(int radius) { this->radius = radius;}
// Circle(ํŒŒ๋ผ๋ฏธํ„ฐ ๋งค๊ฐœ๋ณ€์ˆ˜) { this->๋ฉค๋ฒ„๋ณ€์ˆ˜ = ํŒŒ๋ผ๋ฏธํ„ฐ ๋งค๊ฐœ๋ณ€์ˆ˜;}
// ์‹ค์ œ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ this์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•, ์•”๋ฌต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.
/* Circle(int radius) { this->radius = radius;}
=> Circle(Circle *this, int radius) { this->radius = radius;}
*/

์ƒ์„ฑ์ž์˜ ๊ฒฝ์šฐ this-> ๋ฅผ ์ƒ๋žตํ•ด๋„ ๋œ๋‹ค.
ํ•˜์ง€๋งŒ, ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋ฉค๋ฒ„๋ณ€์ˆ˜๊ฐ€ ์ด๋ฆ„์ด ๊ฐ™๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์ด ๊ผญ ํ•ด์ค˜์•ผํ•œ๋‹ค.

Circle(int radius) { radius = 1;}

this๋Š” ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜์—์„œ ๊ฐ์ฒด ์ž์‹ ์˜ ์ฃผ์†Œ๋ฅผ ๋ฆฌํ„ด ํ• ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

class Sample{
public:
    Sample *foo(){
        return this;
    }
};

this๋Š” ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ํ•จ์ˆ˜์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
๋ฉค๋ฒ„ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ํ•จ์ˆ˜๋Š” ๊ฐ์ฒด์— ์†ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
๋ฉค๋ฒ„ํ•จ์ˆ˜๋ผ๋„ ์ •์ ๋ฉค๋ฒ„ํ•จ์ˆ˜๋ผ๋ฉด ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
์ •์ ๋ฉค๋ฒ„ํ•จ์ˆ˜๋Š” ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ธฐ ์ „์—๋„ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ, ๊ฐ์ฒด๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์—๋„ ์‹คํ–‰์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

string ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•œ ๋ฌธ์ž์—ด ์‚ฌ์šฉ

string ๊ฐ์ฒด ์ƒ์„ฑ

string str; // ๋นˆ ๋ฌธ์ž์—ด ์ŠคํŠธ๋ง ๊ฐ์ฒด
string address("์„œ์šธ์‹œ ๊ด€์•…๊ตฌ ์„œ์šธ๋Œ€ํ•™๊ต"); // ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ์ดˆ๊ธฐํ™”
char text[] = {'H','e', 'l', 'l', 'o', ' ', '\0'};
string *p = new string("C++"); // ๋™์  ์ƒ์„ฑ
string copyAddress(address); // address ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌ
string title(text); // C์Šคํƒ€์ผ ๋ฌธ์ž์—ด์„ string ๊ฐ์ฒด๋กœ ๋ณต์‚ฌ
title.append("World!");
cout << copyAddress << endl; // ์„œ์šธ์‹œ ๊ด€์•…๊ตฌ ์„œ์šธ๋Œ€ํ•™๊ต
cout << title << endl; // Hello, World!
cout << *p; // C++
p->append(", nice");
cout << *p; // C++, nice
delete p; // ๋ฐ˜ํ™˜

๋ช…ํ’ˆ C++ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ฑ…
https://book.naver.com/bookdb/book_detail.naver?bid=13395206

โš ๏ธ ์ฃผ์˜ โš ๏ธ
1. ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ์‹ค์Šต๋ฌธ์ œ ์ •๋‹ต์ž…๋‹ˆ๋‹ค.
2.์ œ๊ฐ€ ์ž‘์„ฑํ•œ ์ •๋‹ต์ด ํ‹€๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜น์‹œ ํ‹€๋ฆฐ๊ฒŒ ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํžˆ ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
3. C++๋ฅผ ๊ณต๋ถ€ํ•˜์‹œ๋Š” ํ•™์ƒ์ด์‹œ๋ผ๋ฉด ๋ณด์‹œ๊ธฐ ์ „์— ์ง์ ‘ ๊ผญ ํ•œ ๋ฒˆ ํ’€์–ด๋ณด์„ธ์š”!

์‹ค์Šต ๋ฌธ์ œ 4 - 1
๋ฌธ์ œ : ์•„๋ž˜ ์ฝ”๋“œ ๋นˆ์นธ ์ฑ„์šฐ๊ธฐ

#include <iostream>
using namespace std;

class Color{
    int red, green, blue;
public:
    Color(){ red = green = blue = 0;}
    Color(int r, int g, int b){ red = r; green = g; blue = b;}
    void setColor(int r, int g, int b) { red = r; green = g; blue = b;}
    void show() {cout << red << ' ' << green << ' ' << blue << endl; }
};

int main()
{
    Color screenColor(255, 0, 0);
    Color *p;
    p = &screenColor;   // (1) p๊ฐ€ screenColor์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๋„๋ก ์ฝ”๋“œ ์ž‘์„ฑ
    p->show();          // (2) p์™€ show()๋ฅผ ์ด์šฉํ•˜์—ฌ screenColor ์ƒ‰ ์ถœ๋ ฅ
    Color colors[3];    // (3) Color์˜ ์ผ์ฐจ์› ๋ฐฐ์—ด colors์„ ์–ธ. ์›์†Œ๋Š” 3๊ฐœ
    p = colors;         // (4) p๊ฐ€ colors๋ฐฐ์—ด์„ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ์ฝ”๋“œ ์ž‘์„ฑ
    // (5) p์™€ setColor()๋ฅผ ์ด์šฉํ•˜์—ฌ colors[0], colors[1], colors[2]๊ฐ€
    // ๊ฐ๊ฐ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘์ƒ‰์„ ๊ฐ€์ง€๋„๋ก ์ฝ”๋“œ ์ž‘์„ฑ
    p[0].setColor(255, 0, 0);
    p[1].setColor(0, 255, 0);
    p[2].setColor(0, 0, 255);
    // (6) p์™€ show()๋ฅผ ์ด์šฉํ•˜์—ฌ colors๋ฐฐ์—ด์˜ ๋ชจ๋“  ๊ฐ์ฒด์˜ ์ƒ‰ ์ถœ๋ ฅ. for ๋ฌธ ์ด์šฉ
    for(int i = 0; i < 3; i++)
    {
        p->show();
        p++;
    }
    delete p;
}

์‹ค์Šต ๋ฌธ์ œ 4 - 2
๋ฌธ์ œ : ๋ฐฐ์—ด์˜ ๋™์  ํ• ๋‹น ๋ฐ ๋ฐ˜ํ™˜์„ ์ด์šฉํ•ด ์ •์ˆ˜ 5๊ฐœ์˜ ํ‰๊ท ๊ตฌํ•˜๊ธฐ

#include <iostream>
using namespace std;

int main()
{
    int *arr = new int[5];
    int sum = 0;
    cout << "์ •์ˆ˜ 5๊ฐœ ์ž…๋ ฅ>> ";
    for(int i = 0; i < 5; i++)
    {
        cin >> arr[i];
        sum += arr[i];
    }
    cout << "ํ‰๊ท  " << (double)sum / 5 << endl;
    delete [] arr;

}

์‹ค์Šต ๋ฌธ์ œ 4 - 3
๋ฌธ์ œ : string ํด๋ž˜์Šค ๋ฉค๋ฒ„ ํ•จ์ˆ˜ find() ๋˜๋Š” for๋ฌธ์„ ์‚ฌ์šฉํ•ด a์˜ ๊ฐœ์ˆ˜ ์ถœ๋ ฅํ•˜๊ธฐ

#include <iostream>
using namespace std;

int main()
{
    string str;
    int cnt = 0;
    cout << "๋ฌธ์ž์—ด ์ž…๋ ฅ>> ";
    getline(cin, str);
    // ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•
    for(int i = 0; i < str.length(); i++)
    {
        if(str[i] == 'a')
            cnt++;
    }
    cout << "๋ฌธ์ž a๋Š” " << cnt << "๊ฐœ ์žˆ์Šต๋‹ˆ๋‹ค." << endl;
    // ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•
    cnt = 0;
    int index = -1;
    while(true)
    {
        index = str.find('a', index + 1);
        if(index == -1)
        {
            cout << "๋ฌธ์ž a๋Š” " << cnt << "๊ฐœ ์žˆ์Šต๋‹ˆ๋‹ค." << endl;
            break;
        }
        cnt += 1;
    }
}

์‹ค์Šต ๋ฌธ์ œ 4 - 4
๋ฌธ์ œ :
Sampleํด๋ž˜์Šค ์™„์„ฑํ•˜๊ธฐ

#include <iostream>
using namespace std;

class Sample{
    int *p;
    int size;
public:
    Sample(int n){
        size = n; p = new int[n];
    }
    void read();
    void write();
    int big();
    ~Sample();
};

void Sample::read() {
    for(int i = 0; i < size; i++)
    {
        cin >> p[i];
    }
}
void Sample::write() {
    for(int i = 0; i < size; i++)
    {
        cout << p[i] << ' ';
    }
    cout << endl;
}
int Sample::big() {
    int max = p[0];
    for(int i = 1; i < size; i++)
    {
        if(max < p[i])
            max = p[i];
    }
    return max;
}
Sample::~Sample() {
    delete [] p;
}

int main()
{
    Sample s(10);
    s.read();
    s.write();
    cout << "๊ฐ€์žฅ ํฐ ์ˆ˜๋Š” " << s.big() << endl;
}

์‹ค์Šต ๋ฌธ์ œ 4 - 5
๋ฌธ์ œ : ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด ๋žœ๋ค์œ„์น˜๋ฅผ ๋žœ๋ค์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ

#include <iostream>
#include <string>
#include <random>
#include <locale>

using namespace std;

int main()
{
    random_device rd;
    mt19937 gen(rd());
    uniform_int_distribution<> alpha(97,122);
    string str;
    cout << "์•„๋ž˜์— ํ•œ ์ค„์„ ์ž…๋ ฅํ•˜์„ธ์š”.(exit๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค)" << endl;
    while(true)
    {
        cout << ">>";
        getline(cin, str);
        if(str == "exit")
            break;
        unsigned int len = str.length();
        uniform_int_distribution<> pos(0, (int)len - 1);
        while(true)
        {
            int n = pos(gen);
            if(isalpha(str[n]))
            {
                str[n] = (char)alpha(gen);
                break;
            }
        }
        cout << str << endl;
    }
}

์‹ค์Šต ๋ฌธ์ œ 4 - 6
๋ฌธ์ œ : ์ž…๋ ฅ ๋ฐ›์€ ๋ฌธ์ž์—ด ๊ฑฐ๊พธ๋กœ ์ถœ๋ ฅํ•˜๊ธฐ

#include <iostream>
#include <string>
#include <random>
#include <locale>

using namespace std;

int main()
{
    string str;
    cout << "์•„๋ž˜์— ํ•œ ์ค„์„ ์ž…๋ ฅํ•˜์„ธ์š”.(exit๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค)" << endl;
    while(true)
    {
        cout << ">>";
        getline(cin, str);
        if(str == "exit")
            break;
        unsigned int len = str.length();
        for(int i = 0; i < len / 2; i++)
        {
            swap(str[i], str[len - 1 - i]);
        }
        cout << str << endl;
    }
}

์‹ค์Šต ๋ฌธ์ œ 4 - 7
๋ฌธ์ œ : Circleํด๋ž˜์Šค์™€ main()ํ•จ์ˆ˜ ์™„์„ฑํ•˜๊ณ  3๊ฐœ์˜ ๊ฐ์ฒด ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ๋ฉด์ ์ด 100์ด์ƒ์ธ ๊ฐ์ฒด์˜ ๊ฐœ์ˆ˜ ์ถœ๋ ฅํ•˜๊ธฐ

#include <iostream>
using namespace std;
#define pi 3.14

class Circle{
    int radius;
public:
    void setRadius(int radius);
    double getArea();
};

void Circle::setRadius(int radius) {
    this->radius = radius;
}
double Circle::getArea() {
    return radius * radius * pi;
}

int main()
{
    Circle *pCircles = new Circle[3];
    int cnt = 0;
    int n = 0;
    for(int i = 0; i < 3; i++)
    {
        cout << "์› "<< i + 1 << "์˜ ๋ฐ˜์ง€๋ฆ„ " << ">> ";
        cin >> n;
        pCircles[i].setRadius(n);
        if(pCircles[i].getArea() > 100)
            cnt++;
    }
    cout << "๋ฉด์ ์ด 100๋ณด๋‹ค ํฐ ์›์€ " << cnt << "๊ฐœ ์ž…๋‹ˆ๋‹ค";
    delete [] pCircles;
}

์‹ค์Šต ๋ฌธ์ œ 4 - 8
๋ฌธ์ œ : ์†Œ์ˆ˜์ ์„ ๊ฐ€์ง€๋Š” 5๊ฐœ์˜ ์‹ค์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ์ œ์ผ ํฐ ์ˆ˜๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋ผ

#include <iostream>
using namespace std;
#define pi 3.14

class Circle{
    int radius;
public:
    void setRadius(int radius);
    double getArea();
};

void Circle::setRadius(int radius) {
    this->radius = radius;
}
double Circle::getArea() {
    return radius * radius * pi;
}

int main()
{
    int num = 0;
    cout << "์›์˜ ๊ฐœ์ˆ˜ >> ";
    cin >> num;
    Circle *pCircles = new Circle[num];
    int cnt = 0;
    int n = 0;
    for(int i = 0; i < num; i++)
    {
        cout << "์› "<< i + 1 << "์˜ ๋ฐ˜์ง€๋ฆ„ " << ">> ";
        cin >> n;
        pCircles[i].setRadius(n);
        if(pCircles[i].getArea() > 100)
            cnt++;
    }
    cout << "๋ฉด์ ์ด 100๋ณด๋‹ค ํฐ ์›์€ " << cnt << "๊ฐœ ์ž…๋‹ˆ๋‹ค";
    delete [] pCircles;
}

์‹ค์Šต ๋ฌธ์ œ 4 - 9
๋ฌธ์ œ : Personํด๋ž˜์Šค ์™„์„ฑํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ถœ๋ ฅ์ด ๋‚˜์˜ค๊ฒŒ mainํ•จ์ˆ˜ ์™„์„ฑํ•˜๊ธฐ

#include <iostream>
#include <string>
using namespace std;

class Person{
    string name;
    string tel;
public :
    Person();
    string getName() {return name;}
    string getTel() {return tel;}
    void set(string name, string tel);
};
Person::Person() {

}
void Person::set(string name, string tel) {
    this->name = name;
    this->tel = tel;
}

int main()
{
    Person *persons = new Person[3];
    cout << "์ด๋ฆ„๊ณผ ์ „ํ™” ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”" << endl;
    string name;
    string tel;
    for(int i = 0; i < 3; i++)
    {
        cout << "์‚ฌ๋žŒ " << i + 1 << ">> ";
        cin >> name >> tel;
        persons[i].set(name, tel);
    }
    cout << "๋ชจ๋“  ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์€ ";

    for(int i = 0; i < 3; i++)
    {
        if(i == 2)
        {
            cout << persons[i].getName() << endl;
        }
        else
        {
            cout << persons[i].getName() << " ";
        }
    }

    cout << "์ „ํ™”๋ฒˆํ˜ธ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”>>";
    cin >> name;
    for(int i = 0; i < 3; i++)
    {
        if(persons[i].getName() == name)
        {
            cout << "์ „ํ™” ๋ฒˆํ˜ธ๋Š” " << persons[i].getTel() << endl;
            break;
        }
    }
    delete [] persons;
}

์‹ค์Šต ๋ฌธ์ œ 4 - 10
๋ฌธ์ œ : ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด Familyํด๋ž˜์Šค์™€ Personํด๋ž˜์Šค ์™„์„ฑํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ถœ๋ ฅ ๋งŒ๋“ค๊ธฐ

#include <iostream>
#include <string>
using namespace std;

class Person{
    string name;
public:
    Person(){}
    Person(string name) {this->name = name;}
    string getName() {return name;}
    void setName(string name) {this->name = name;}
};

class Family{
    Person *p;
    int size;
    string name;
public:
    Family(string name,int size);
    void show();
    void setName(int n, string name) {
        this->p[n].setName(name);
    }
    ~Family();
};
Family::Family(string name, int size) {
    p = new Person[size];
    this->name = name;
    this->size = size;
}
void Family::show() {
    cout << name << "๊ฐ€์กฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด " << size << "๋ช… ์ž…๋‹ˆ๋‹ค." << endl;
    for(int i = 0; i < size; i++)
    {
        cout << p[i].getName() << "    ";
    }
}
Family::~Family() {
    delete [] p;
}
int main()
{
    Family *shin = new Family("shin", 4);
    shin->setName(0, "hyungman");
    shin->setName(1, "misun");
    shin->setName(2, "jjanggu");
    shin->setName(3, "jjanga");
    shin->show();
    delete shin;
}

์‹ค์Šต ๋ฌธ์ œ 4 - 11
๋ฌธ์ œ : ์ปคํ”ผ์žํŒ๊ธฐ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

#include <iostream>
using namespace std;

class Container{ // ํ†ต ํด๋ž˜์Šค
    int size; // ํ˜„์žฌ ์ €์žฅ๋Ÿ‰ ์ตœ๋Œ€ 10
public:
    Container() {size = 10;}
    void fill(); // ์ตœ๋Œ€๋Ÿ‰ 10์œผ๋กœ ์ฑ„์šฐ๊ธฐ
    void consume(); // 1๋งŒํผ ์†Œ๋น„
    int getSize(); // ํ˜„์žฌ ํฌ๊ธฐ ๋ฆฌํ„ด
};

void Container::fill() {
    size = 10;
}
int Container::getSize() {
    return size;
}

void Container::consume() {
    size--;
}
class CoffeeVendingMachine{ // ์ปคํ”ผ ์žํŒ๊ธฐ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ํด๋ž˜์Šค
    Container tong[3]; // 0 = ์ปคํ”ผ 1 = ๋ฌผ 2 = ์„คํƒ•
    void fill(); // 3ํ†ต ๋ชจ๋‘ 10
    void selectEspresso(); // ์ปคํ”ผ -1 ๋ฌผ -1
    void selectAmericano(); // ์ปคํ”ผ -1 ๋ฌผ -2
    void selectSugarCoffee(); // ์ปคํ”ผ -1 ๋ฌผ -2 ์„คํƒ• -1
    void show(); // ํ˜„ํ™ฉ ๋ณด์—ฌ์ฃผ๊ธฐ
public:
    void run();
};

void CoffeeVendingMachine::fill() {
    for(int i = 0; i < 3; i++)
    {
        tong[i].fill();
    }
    show();
}

void CoffeeVendingMachine::selectEspresso() {
    if(tong[0].getSize() >= 1 && tong[1].getSize() >= 1)
    {
        tong[0].consume();
        tong[1].consume();
        cout << "์—์Šคํ”„๋ ˆ์†Œ ๋“œ์„ธ์š”" << endl;
    }
    else
    {
        cout << "์›๋ฃŒ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค." << endl;
    }

}

void CoffeeVendingMachine::selectAmericano() {
    if(tong[0].getSize() >= 1 && tong[1].getSize() >= 2)
    {
        tong[0].consume();
        tong[1].consume();
        tong[1].consume();
        cout << "์•„๋ฉ”๋ฆฌ์นด๋…ธ ๋“œ์„ธ์š”" << endl;
    }
    else
    {
        cout << "์›๋ฃŒ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค." << endl;
    }
}
void CoffeeVendingMachine::selectSugarCoffee() {
    if(tong[0].getSize() >= 1 && tong[1].getSize() >= 2 && tong[2].getSize()>=1)
    {
        tong[0].consume();
        tong[1].consume();
        tong[1].consume();
        tong[2].consume();
        cout << "์„คํƒ•์ปคํ”ผ ๋“œ์„ธ์š”" << endl;
    }
    else
    {
        cout << "์›๋ฃŒ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค." << endl;
    }
}
void CoffeeVendingMachine::show() {
    cout << "์ปคํ”ผ " << tong[0].getSize() << ", ๋ฌผ " << tong[1].getSize() << ", ์„คํƒ• " << tong[2].getSize() << endl;
}

void CoffeeVendingMachine::run() {
    int n = 0;
    cout << "****** ์ปคํ”ผ์žํŒ๊ธฐ๋ฅผ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ******" << endl;
    while(true)
    {
        cout << "๋ฉ”๋‰ด๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”(1: ์—์Šคํ”„๋ ˆ์†Œ, 2:์•„๋ฉ”๋ฆฌ์นด๋…ธ, 3:์„คํƒ•์ปคํ”ผ, 4:์ž”๋Ÿ‰๋ณด๊ธฐ, 5:์ฑ„์šฐ๊ธฐ)>> ";
        cin >> n;
        switch (n) {
            case 1:
                selectEspresso();
                break;
            case 2:
                selectAmericano();
                break;
            case 3:
                selectSugarCoffee();
                break;
            case 4:
                show();
                break;
            case 5:
                fill();
                break;
        }
    }
}
int main()
{
    CoffeeVendingMachine cvm;
    cvm.run();
}

์‹ค์Šต ๋ฌธ์ œ 4 - 12
๋ฌธ์ œ : ์ถœ๋ ค๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก Circle, CircleManagerํด๋ž˜์Šค ์™„์„ฑํ•˜๊ณ  mainํ•จ์ˆ˜๋„ ์™„์„ฑํ•˜๊ธฐ

#include <iostream>
#include <string>
using namespace std;

class Circle{
    int radius;
    string name;
public:
    void setCircle(string name, int radius){
        this->name = name;
        this->radius = radius;
    }
    double getArea(){return radius * radius * 3.14;}
    string getName(){return name;}
};

class CircleManager{
    Circle *p;
    int size;
public:
    CircleManager(int size)
    {
        p = new Circle[size];
        string name;
        int radius;
        for(int i = 0; i < size; i++)
        {
            cout << "์› " << i + 1 << "์˜ ์ด๋ฆ„๊ณผ ๋ฐ˜์ง€๋ฆ„ >> ";
            cin >> name >> radius;
            p[i].setCircle(name, radius);
        }

    } //size ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ๋™์  ์ƒ์„ฑ. ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ ์™„๋ฃŒ
    ~CircleManager(){delete [] p;}
    void searchByName(){// ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์›์˜ ์ด๋ฆ„์„ ์ž…๋ ฅ๋ฐ›์•„ ๋ฉด์  ์ถœ๋ ฅ
        string name;
        cout << "๊ฒ€์ƒ‰ํ•˜๊ณ ์ž ํ•˜๋Š” ์›์˜ ์ด๋ฆ„ >> ";
        cin >> name;
        for(int i = 0; i < size; i++)
        {
            if(name == p[i].getName())
            {
                cout << p[i].getName() << "์˜ ๋ฉด์ ์€ " <<  p[i].getArea() << endl;
                return;
            }
        }
    }
    void searchByArea(){// ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋ฉด์ ์„ ์ž…๋ ฅ๋ฐ›์•„ ๋ฉด์ ๋ณด๋‹ค ํฐ ์›์˜ ์ด๋ฆ„ ์ถœ๋ ฅ
        int n;
        cout << "์ตœ์†Œ ๋ฉด์ ์„ ์ •์ˆ˜๋กœ ์ž…๋ ฅํ•˜์„ธ์š” >> ";
        cin >> n;
        cout << n << "๋ณด๋‹ค ํฐ ์›์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค." << endl;
        for(int i = 0; i < size; i++)
        {
            if(p[i].getArea() > n)
            {
                cout << p[i].getName() << "์˜ ๋ฉด์ ์€ " << p[i].getArea() << ", ";
            }
        }
    }
};

int main()
{
    int n;
    cout << "์›์˜ ๊ฐœ์ˆ˜ >> ";
    cin >> n;
    CircleManager cm(n);
    cm.searchByName();
    cm.searchByArea();
}

์‹ค์Šต ๋ฌธ์ œ 4 - 13
๋ฌธ์ œ : ์ถœ๋ ฅ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก Histogramํด๋ž˜์Šค์™€ mainํ•จ์ˆ˜ ์™„์„ฑํ•˜๊ธฐ

#include <iostream>
#include <string>
#include <locale>
using namespace std;

class Histogram{
    string str;
public:
    Histogram(string str){this->str = str;}
    void put(string str){
        this->str.append(str);
    }
    void putc(char c){
        char *pChar = new char[2] {c, '\0'};
        str.append(pChar);
        delete [] pChar;
    }
    void print()
    {
        int alpha[26] = {0,};
        cout << str << endl << endl;
        int cnt = 0;
        for(int i = 0; i <str.size(); i++)
        {
            if(isalpha(str[i]))
            {
                cnt++;
                str[i] = tolower(str[i]);
                alpha[int(str[i]) - 97]++;
            }
        }
        cout << "์ด ์•ŒํŒŒ๋ฒณ ์ˆ˜ " << cnt << endl << endl;
        for(int i = 0; i < 26; i++)
        {
            cout << char(i + 97) << " (" << alpha[i] << ")    : ";
            for(int j = 0; j < alpha[i]; j++)
            {
                cout << "*";
            }
            cout << endl;
        }

    }
};
int main()
{
    Histogram elvisHisto("Wise men say, only fools rush in But I can't help, ");
    elvisHisto.put("falling in love with you");
    elvisHisto.putc('-');
    elvisHisto.put("Elvis Presley");
    elvisHisto.print();

}

์‹ค์Šต ๋ฌธ์ œ 4 - 14
๋ฌธ์ œ : ์ถœ๋ ฅ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก Player, GamblingGameํด๋ž˜์Šค์™€ mainํ•จ์ˆ˜ ์™„์„ฑํ•˜๊ธฐ

#include <iostream>
#include <string>
#include <random>
using namespace std;

class Player{
    string name;
public:
    void setName(){
        cin >> name;
    }
    string getName()
    {
        return name;
    }
};
class GamblingGame{
    Player *p;
    mt19937 gen;
public:
    GamblingGame() : gen((random_device())()){
        p = new Player[2];
        cout << "์ฒซ๋ฒˆ์งธ ์„ ์ˆ˜ ์ด๋ฆ„>>";
        p[0].setName();
        cout << "๋‘๋ฒˆ์งธ ์„ ์ˆ˜ ์ด๋ฆ„>>";
        p[1].setName();
        cin.ignore();
    }
    ~GamblingGame(){
        delete [] p;
    }
    int randNum()
    {
        uniform_int_distribution<> dist(0, 2);
        return dist(gen);
    }
    void run(){
        int n = 0;
        while(true)
        {
            cout << p[n%2].getName() << ":";
            string c;
            if(c == "\0")
            {
                cin.get();
                int temp[3] = {randNum(),randNum(),randNum()};
                if(temp[0] == temp[1] && temp[0] == temp[2])
                {
                    cout << "\t\t\t" << temp[0] << "\t" << temp[1] << "\t" << temp[2] << "\t" << p[n%2].getName() << "๋‹˜ ์Šน๋ฆฌ!!";
                    break;
                }
                else
                {
                    cout << "\t\t\t" << temp[0] << "\t" << temp[1] << "\t" << temp[2] << "\t" << "์•„์‰ฝ๊ตฐ์š”!" << endl;
                }
            }
            n++;
        }
    }
};

int main()
{
    GamblingGame gbg;
    gbg.run();
}![](https://velog.velcdn.com/images/5p2rs5/post/0115e09c-50b1-4331-9f51-b64515bcf544/image.png)
profile
์น˜ํƒ€๊ฐ€ ๋˜๊ณ  ์‹ถ์€ ์ทจ์ค€์ƒ

0๊ฐœ์˜ ๋Œ“๊ธ€