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

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

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

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

5์žฅ ์š”์ 

ํ•จ์ˆ˜์™€ ์ฐธ์กฐ, ๋ณต์‚ฌ ์ƒ์„ฑ์ž

ํ•จ์ˆ˜์˜ ์ธ์ž ์ „๋‹ฌ ๋ฐฉ์‹

๊ฐ’์— ์˜ํ•œ ํ˜ธ์ถœ

๊ฐ’์— ์˜ํ•œ ํ˜ธ์ถœ์‹œ ๋‹จ์ˆœํžˆ ๊ฐ’์ด ๋ณต์‚ฌ๋˜์–ด ํ•จ์ˆ˜์˜ ์Šคํƒ์— ์ €์žฅ๋œ๋‹ค.
๊ฐ’์ด ๋ณต์‚ฌ๋˜์—ˆ๊ธฐ์— mainํ•จ์ˆ˜๋‚ด ๋ณ€์ˆ˜์˜ ๊ฐ’์—๋Š” ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š”๋‹ค.

swapํ•จ์ˆ˜ 
=> swap(int a, int b)
mainํ•จ์ˆ˜
=> swap(a, b);

์ฃผ์†Œ์— ์˜ํ•œ ํ˜ธ์ถœ

ํ•จ์ˆ˜์— ํฌ์ธํ„ฐ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์Šคํƒ์— ์ƒ์„ฑ๋œ๋‹ค. ์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์€
mainํ•จ์ˆ˜์— m, n์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ ํ•จ์ˆ˜ ์ˆ˜ํ–‰์‹œ mainํ•จ์ˆ˜์—์„œ๋„ ๊ฐ’์ด ๋ฐ”๋€๋‹ค.

swapํ•จ์ˆ˜ 
=> swap(int *a, int *b)
mainํ•จ์ˆ˜
=> swap(&a, &b);

ํ•จ์ˆ˜ ํ˜ธ์ถœ์‹œ ๊ฐ์ฒด ์ „๋‹ฌ

๊ฐ’์— ์˜ํ•œ ํ˜ธ์ถœ๋กœ ๊ฐ์ฒด ์ „๋‹ฌ

๊ฐ์ฒด๋ฅผ ๊ฐ’์— ์˜ํ•œ ํ˜ธ์ถœ์‹œ ์ƒ์„ฑ์ž๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๊ณ  ์†Œ๋ฉธ์ž๋งŒ ์‹คํ–‰๋˜๋Š” ๋น„๋Œ€์นญ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
ํ•ด๊ฒฐ๋ฐฉ๋ฒ• = ๋ณต์‚ฌ์ƒ์„ฑ์ž

์ฃผ์†Œ์— ์˜ํ•œ ํ˜ธ์ถœ๋กœ ๊ฐ์ฒด ์ „๋‹ฌ

๊ฐ์ฒด์˜ ์ฃผ์†Œ๋ฅผ ์ „๋‹ฌํ•˜๋ฏ€๋กœ ๋น„๋Œ€์นญ์˜ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

๊ฐ์ฒด ์น˜ํ™˜ ๋ฐ ๊ฐ์ฒด ๋ฆฌํ„ด

๊ฐ์ฒด ์น˜ํ™˜

Circle c1(5)
Circle c2(30)
c1 = c2; // c1์— c2์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ๋น„ํŠธ๋‹จ์œ„๋กœ ๋ณต์‚ฌ๋œ๋‹ค.

๊ฐ์ฒด ๋ฆฌํ„ด

Shape getName()
{
	Shape temp("๋„ค๋ชจ");
    return temp;
}

์ฐธ์กฐ์™€ ํ•จ์ˆ˜

์ฐธ์กฐ

๋ณ€์ˆ˜์˜ ๋ณ„๋ช…๊ณผ ๊ฐ™์€ ๊ฐœ๋…

// ์„ ์–ธ์€ &๋ฅผ ์ด์šฉํ•œ๋‹ค.
int num = 7;
int &refnum = n;
// => num๊ณผ refnum์€ ๋™์ผํ•œ ๋ณ€์ˆ˜๋‹ค.
refnum = 9;
// => num์˜ ๊ฐ’์€ 9๊ฐ€ ๋œ๋‹ค.

๊ฐ์ฒด ์ฐธ์กฐ

์ฐธ์กฐ๋ณ€์ˆ˜๋Š” ํฌ์ธํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ -> ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

Shape nemo;
Shape &refnemo = nemo;
refnemo.setName("๋„ค๋ชจ");

ํฌ์ธํ„ฐ์™€ ๊ฐ™์ด
p๋Š” refnum์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง„๋‹ค. ์ด๋Š” num์˜ ์ฃผ์†Œ๋„ ๊ฐ€์ง„๋‹ค๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค.

int *p = &refnum;
*p = 45; // n = 45, refnum = 45;

์ฐธ์กฐ ์ฃผ์˜์‚ฌํ•ญ

  1. ์„ ์–ธํ›„ ๋ฐ”๋กœ ์ดˆ๊ธฐํ™”
  2. ์ฐธ์กฐ์ž &์˜ ์œ„์น˜ ํƒ€์ž…๊ณผ ๋ณ€์ˆ˜๋ช… ์‚ฌ์ด ์–ด๋Š ์œ„์น˜๋“  ๋ฌด๊ด€ํ•˜๋‹ค.
  3. ์ฐธ์กฐ ๋ณ€์ˆ˜๋กœ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค.
  4. ์ฐธ์กฐ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ฐธ์กฐ ์„ ์–ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์ฐธ์กฐ์— ๋Œ€ํ•œ ํ˜ธ์ถœ

๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•œ๋‹ค.
mainํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜๊ฐ€ ์ฐธ์กฐ๋˜์–ด ํ•จ์ˆ˜์— ๋“ค์–ด๊ฐ€๋ฏ€๋กœ ํ•จ์ˆ˜๋‚ด์—์„œ ๊ฐ’์— ๋ณ€ํ™”๊ฐ€ ์žˆ๋‹ค๋ฉด mainํ•จ์ˆ˜์—๋„ ๋ฐ˜์˜๋œ๋‹ค.
๊ฐ’์— ์˜ํ•œ ํ˜ธ์ถœ๊ณผ ์ฃผ์†Œ์— ์˜ํ•œ ํ˜ธ์ถœ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ํ•จ์ˆ˜์˜ ์Šคํƒ๊ณต๊ฐ„์ด ๋งŒ๋“ค์–ด์ง€์ง€ ์•Š๊ณ  ์ด๋ฆ„๋งŒ ์กด์žฌํ•œ๋‹ค.

swapํ•จ์ˆ˜ 
=> swap(int &a, int &b)
mainํ•จ์ˆ˜
=> swap(a, b);

์ฐธ์กฐ์— ์˜ํ•œ ํ˜ธ์ถœ๋กœ ๊ฐ์ฒด ์ „๋‹ฌ

์ฐธ์กฐ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ๋ชจ๋“  ์—ฐ์‚ฐ์€ ์›๋ณธ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์ด ๋œ๋‹ค.
์ฐธ์กฐ ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ์ด๋ฆ„๋งŒ ์ƒ์„ฑ๋˜๋ฏ€๋กœ,
โ—๏ธ์ƒ์„ฑ์ž์™€ ์†Œ๋ฉธ์ž๋Š” ์•„์˜ˆ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.โ—๏ธ

์ฐธ์กฐ ๋ฆฌํ„ด

char c = 'a';
char& find()
{
	return c;
}
char a = find(); // a = 'a'
char &ref = find(); // ref๋Š” c์— ๋Œ€ํ•œ ์ฐธ์กฐ
ref = 'M'; // c = 'M'
find() = 'b' // c = 'b'๊ฐ€ ๋œ๋‹ค.

๋ณต์‚ฌ์ƒ์„ฑ์ž

์–•์€๋ณต์‚ฌ

ํฌ์ธํ„ฐ ๋ณ€์ˆ˜์™€ ๊ด€๋ จ๋˜์–ด ์žˆ๋‹ค.

์–•์€ ๋ณต์‚ฌ :
์ฃผ์†Œ๊ฐ’์„ ๋ณต์‚ฌ, ์›๋ณธ ๋˜๋Š” ์‚ฌ๋ณธ์ด ๋ฐ”๋€Œ๋ฉด ์‚ฌ๋ณธ ๋˜๋Š” ์›๋ณธ๋„ ๋ฐ”๋€๋‹ค.

์น˜ํ™˜๋ฌธ์€ ์–•์€ ๋ณต์‚ฌ๋‹ค.

๊นŠ์€ ๋ณต์‚ฌ :
๋ณ€์ˆ˜ ๋ฐ ๊ฐ์ฒด ์ž์ฒด๋ฅผ ๋ณต์‚ฌํ•œ๋‹ค. ์›๋ณธ ๋˜๋Š” ์‚ฌ๋ณธ์ด ๋ฐ”๋€Œ๋”๋ผ๋„ ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค.
๊ฐ์ฒด์•ˆ์— ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ์–•์€ ๋ณต์‚ฌ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ์ƒ๊ด€์€ ์—†๋‹ค.

๋ณต์‚ฌ์ƒ์„ฑ์ž

// ์„ ์–ธ
class Shape{
	string name;
public:    
	Shape(const Shape& c);
};
//์‚ฌ์šฉ
Shape nemo(10); // ์ผ๋ฐ˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
shape cNemo(nemo); // ๋ณต์‚ฌ์ƒ์„ฑ์ž ํ˜ธ์ถœํ•ด์„œ cNemo์— nemo๋ณต์‚ฌ
Shape (const Shape& c){
	this->name = c.name;
}

๋””ํดํŠธ ๋ณต์‚ฌ ์ƒ์„ฑ์ž

๋ณต์‚ฌ์ƒ์„ฑ์ž๋ฅผ ์„ ์–ธํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ์•”๋ฌต์ ์œผ๋กœ ๋””ํดํŠธ ๋ณต์‚ฌ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ์ปดํŒŒ์ผ๋œ๋‹ค.

Shape(const Circle& c){
	this->name = c.name;
}

๋ณต์‚ฌ์ƒ์„ฑ์ž ์–•์€ ๋ณต์‚ฌ์™€ ๊นŠ์€ ๋ณต์‚ฌ

๋™์ ํ• ๋‹น ๋œ ํฌ์ธํ„ฐ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ฐ์ฒด๊ฐ€ ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ๋กœ ์†Œ๋ฉธ์ž์— ์˜ํ•ด ํž™์— ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•  ๊ฒฝ์šฐ ๋ณต์‚ฌ๋œ ๊ฐ์ฒด์˜ ๋™์ ํ• ๋‹น ๋œ ํฌ์ธํ„ฐ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜์ด ๋œ๋‹ค.
์ด ๋•Œ ์›๋ณธ์˜ ๊ฐ์ฒด๋„ ๋ฐ˜ํ™˜์ด ์ง„ํ–‰๋˜๋Š”๋ฐ ๋ฐ˜ํ™˜ํ•œ ๊ฒƒ์„ ํ•œ๋ฒˆ ๋” ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ์‚ฌ์šฉ์ž ๋ณต์‚ฌ ์ƒ์„ฑ์ž(๊นŠ์€๋ณต์‚ฌ)๋ฅผ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์ž

class Shape{
    char *name;
public:
    Shape(const char *name) // ์ƒ์„ฑ์ž
    {
        int len = strlen(name);
        this->name = new char[len + 1];
        strcpy(this->name, name);
    }
    Shape(const Shape &c){ // ์‚ฌ์šฉ์ž ๋ณต์‚ฌ ์ƒ์„ฑ์ž
        unsigned int len = strlen(c.name);
        this->name = new char[len + 1]; 
        // ํž™์œผ๋กœ ๋ถ€ํ„ฐ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น ๋ฐ›๋Š” ๋ชจ์Šต
        strcpy(this->name, c.name);
    }
};

๋ณต์‚ฌ ์ƒ์„ฑ์ž ์ฃผ์˜์‚ฌํ•ญ

ํ˜ธ์ถœ ๋ฐฉ๋ฒ•

  1. Shape cNemo = nemo; // ๊ฐ์ฒด๊ฐ€ ๊ฐ์ฒด๋กœ ์ดˆ๊ธฐํ™”๋  ๊ฒฝ์šฐ
  2. Shape cNemo(nemo);
  3. ์น˜ํ™˜๋ฌธ์€ ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.
    ex) cNemo = nemo;
  4. ๊ฐ’์— ์˜ํ•œ ํ˜ธ์ถœ์‹œ ๋ณต์‚ฌ์ƒ์„ฑ์ž๊ฐ€ ์žˆ๋‹ค๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ณต์‚ฌ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋œ๋‹ค.
    ๋ณต์‚ฌ์ƒ์„ฑ์ž๊ฐ€ ์—†๋”๋ผ๋„ ๋””ํดํŠธ ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค.
  5. ๊ฐœ์ฒด๋ฅผ ๋ฆฌํ„ดํ•  ๊ฒฝ์šฐ๋„ ๋ณต์‚ฌ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ  ๋ฆฌํ„ดํ•œ๋‹ค.

์ •๋ฆฌ

์‚ฌ์šฉ์ž ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ฅผ ์ œ์™ธํ•œ ๋Œ€๋ถ€๋ถ„์€ ๋‚˜๋จธ์ง€์˜ ๋ณต์‚ฌ์ƒ์„ฑ์ž๋Š” ์–•์€ ๋ณต์‚ฌ๋‹ค.

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

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

์‹ค์Šต ๋ฌธ์ œ 5 - 1
๋ฌธ์ œ : ๊ฐ์ฒด๋ฅผ ๊ตํ™˜ํ•˜๋Š” swapํ•จ์ˆ˜๋ฅผ ์ฐธ์กฐ์— ์˜ํ•œ ํ˜ธ์ถœ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ

#include <iostream>
using namespace std;

class Circle{
    int radius;
public:
    Circle(){}
    Circle(const Circle &a)
    {
        this->radius = a.radius;
    }
    int getRadius()
    {
        return radius;
    }
    void setRadius(int a)
    {
        this->radius = a;
    }

};
void swap(Circle &a, Circle &b)
{
    Circle temp;
    temp = a;
    a = b;
    b = temp;
}
int main()
{
    Circle a;
    Circle b;
    a.setRadius(3);
    b.setRadius(9);
    cout << "a : " << a.getRadius() << " b : "  << b.getRadius() << endl;
    swap(a, b);
    cout << "a : " << a.getRadius() << " b : "  << b.getRadius() << endl;
};

์‹ค์Šต ๋ฌธ์ œ 5 - 2
๋ฌธ์ œ : halfํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

10

#include <iostream>
using namespace std;

void half(double &n)
{
    n = n / 2;
}
int main()
{
    double n = 20;
    half(n);
    cout << n;
}

์‹ค์Šต ๋ฌธ์ œ 5 - 3
๋ฌธ์ œ : combine()ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

#include <iostream>
#include <string>
using namespace std;
void combine(string str1, string str2, string &str3)
{
    str3.append(str1).append(" ").append(str2);
}
int main()
{
    string text1("I love you"), text2("very much");
    string text3;
    combine(text1, text2, text3);
    cout << text3;
}

์‹ค์Šต ๋ฌธ์ œ 5 - 4
๋ฌธ์ œ : bigger()ํ•จ์ˆ˜ ์™„์„ฑํ•˜๊ธฐ

#include <iostream>
using namespace std;

bool bigger(int a, int b, int &big)
{

    if(a == b)
        return true;
    else{
        big = a > b ? a : b;
        return false;
    }
}

int main()
{
    int a, b;
    int big;
    cin >> a >> b;
    if(!bigger(a, b, big))
    {
        cout << big << endl;
    }
}

์‹ค์Šต ๋ฌธ์ œ 5 - 5
๋ฌธ์ œ : increaseByํ•จ์ˆ˜ ์ˆ˜์ •ํ•˜๊ธฐ

#include <iostream>
using namespace std;

class Circle{
    int radius;
public:
    Circle(int r){radius = r;}
    int getRadius(){return radius;};
    void setRadius(int r){radius = r;}
    void show(){cout << "๋ฐ˜์ง€๋ฆ„์ด " << radius << "์ธ ์›" << endl;}
};
void increaseBy(Circle &a, Circle b)
{
    int r= a.getRadius() + b.getRadius();
    a.setRadius(r);
}
int main()
{
    Circle x(10), y(5);
    increaseBy(x, y);
    x.show();
}

์‹ค์Šต ๋ฌธ์ œ 5 - 6
๋ฌธ์ œ : findํ•จ์ˆ˜ ์™„์„ฑํ•˜๊ธฐ

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

char &find(char a[], char c, bool &success)
{
    for(int i = 0; i < strlen(a); i++)
    {
        if(a[i] == c)
        {
            success = true;
            return a[i];
        }
    }
    success = false;
    return a[strlen(a)];
}

int main()
{
    char s[] = "Mike";
    bool b = false;
    char &loc = find(s, 'M', b);
    if(!b)
    {
        cout << "M์„ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์—†๋‹ค" << endl;
        return 0;
    }
    loc = 'm';
    cout << s << endl;
}

์‹ค์Šต ๋ฌธ์ œ 5 - 7
๋ฌธ์ œ : ์Šคํƒํด๋ž˜์Šค MyIntStack๊ตฌํ˜„ํ•˜๊ธฐ

#include <iostream>
using namespace std;

class MyIntStack{
    int p[10];
    int tos;
public:
    MyIntStack();
    bool push(int n);
    bool pop(int &n);
};
MyIntStack::MyIntStack() {
    for(int i = 0; i < 10; i++)
        p[i] = 0;
    tos = 0;
}
bool MyIntStack::push(int n) {
    if(tos > 9)
        return false;
    else
    {
        p[tos] = n;
        tos += 1;
        return true;
    }
}
bool MyIntStack::pop(int &n) {
    if(tos < 1)
        return false;
    else
    {
        tos -= 1;
        n = p[tos];
        return true;
    }
}

int main()
{
    MyIntStack a;
    for(int i = 0; i < 11; i++)
    {
        if(a.push(i))
            cout << i << ' ';
        else
            cout << endl << i + 1 << " ๋ฒˆ์งธ stack full" << endl;
    }
    int n;
    for(int i = 0; i < 11; i++)
    {
        if(a.pop(n))
            cout << n << ' ';
        else
            cout << endl << i + 1 << " ๋ฒˆ์งธ stack empty" << endl;
    }
    cout << endl;
}

์‹ค์Šต ๋ฌธ์ œ 5 - 8
๋ฌธ์ œ : 7๋ฒˆ๋ฌธ์ œ ์ˆ˜์ •ํ•˜๊ธฐ

#include <iostream>
using namespace std;

class MyIntStack{
    int *p;
    int tos;
    int size;
public:
    MyIntStack();
    MyIntStack(int size);
    MyIntStack(const MyIntStack &s);
    ~MyIntStack();

    bool push(int n);
    bool pop(int &n);

};
MyIntStack::MyIntStack() {
    for(int i = 0; i < 10; i++)
        p[i] = 0;
    tos = 0;
}
MyIntStack::MyIntStack(int size) {
    p = new int[size];
    for(int i = 0; i < size; i++)
        p[i] = 0;
    tos = 0;
}
MyIntStack::MyIntStack(const MyIntStack &s) {
    this->tos = s.tos;
    this->size = s.size;
    p = new int[size];
    for(int i = 0; i < size; i++)
    {
        p[i] = s.p[i];
    }
}
MyIntStack::~MyIntStack() {
    delete [] p;
}
bool MyIntStack::push(int n) {
    if(tos > 9)
        return false;
    else
    {
        p[tos] = n;
        tos += 1;
        return true;
    }
}
bool MyIntStack::pop(int &n) {
    if(tos < 1)
        return false;
    else
    {
        tos -= 1;
        n = p[tos];
        return true;
    }
}

int main()
{
    MyIntStack a(10);
    a.push(10);
    a.push(20);
    MyIntStack b = a;
    b.push(30);

    int n;
    a.pop(n);
    cout << "์Šคํƒ a์—์„œ ํŒํ•œ ๊ฐ’ " << n << endl;
    b.pop(n);
    cout << "์Šคํƒ b์—์„œ ํŒํ•œ ๊ฐ’ " << n << endl;
}

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

#include <iostream>
using namespace std;

class Accumulator{
    int value;
public:
    Accumulator(int value);
    Accumulator &add(int n);
    int get();
};

Accumulator::Accumulator(int value) {this->value = value;}

Accumulator &Accumulator::add(int n)
{
    this->value += n;
    return *this;
}

int Accumulator::get() {
    return value;
}
int main()
{
    Accumulator acc(10);
    acc.add(5).add(6).add(7);
    cout << acc.get();
}

์‹ค์Šต ๋ฌธ์ œ 5 - 10
๋ฌธ์ œ : appendํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

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

class Buffer{
    string text;
public:
    Buffer(string text){this->text = text;}
    void add(string next){text += next;}
    void print(){cout << text << endl;}
};

Buffer &append(Buffer &buf, string text)
{
    buf.add(text);
    return buf;
}

int main()
{
    Buffer buf("Helllo");
    Buffer &temp = append(buf, "Guys");
    temp.print();
    buf.print();
}

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

string์‚ฌ์šฉ ์ „ ์ฝ”๋“œ

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

class Book{
    char *title;
    int price;
public:
    Book(const char* title, int price){
        unsigned int len = strlen(title);
        this->title = new char[len + 1];
        strcpy(this->title, title);
        this->price = price;
    }
    /* ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์‚ฝ์ž…ํ•˜๋Š” ๋””ํดํŠธ ๋ณต์‚ฌ ์ƒ์„ฑ์ž
    Book(const Book &b)
    {
        this->title = b.title;
        this->price = b.price;
    }
     */
    Book(const Book &b)
    {
        unsigned int len = strlen(b.title);
        this->title = new char[len + 1];
        strcpy(this->title, b.title);
        this->price = b.price;
    }
    ~Book(){
        delete [] title;
    }
    void set(char* title, int price){
        if(this->title){
            delete [] this->title;
            this->title = new char[strlen(title) + 1];
            strcpy(this->title, title);
            this->price = price;
        }
        else
        {
            this->title = new char[strlen(title) + 1];
            strcpy(this->title, title);
            this->price = price;
        }
    }
    void show() {cout << title << ' ' << price << "์›" << endl;}
};

int main()
{
    Book cpp("๋ช…ํ’ˆC++", 10000);
    Book java = cpp;
    java.set("๋ช…ํ’ˆ์ž๋ฐ”", 12000);
    cpp.show();
    java.show();
}

string์‚ฌ์šฉ ํ›„ ์ฝ”๋“œ

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

class Book{
    string title;
    int price;
public:
    Book(string title, int price){
        unsigned int len = title.length();
        this->title = title;
        this->price = price;
    }
    void set(string title, int price){
            this->title = title;
            this->price = price;
    }
    void show() {cout << title << ' ' << price << "์›" << endl;}
};

int main()
{
    Book cpp("๋ช…ํ’ˆC++", 10000);
    Book java = cpp;
    java.set("๋ช…ํ’ˆ์ž๋ฐ”", 12000);
    cpp.show();
    java.show();
}

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

(1)

#include <iostream>
using namespace std;

class Dept{
    int size;
    int *scores;
public:
    Dept(int size)
    {
        this->size = size;
        scores = new int[size];
    }
    Dept(const Dept& dept)
    {
        this->size = dept.size;
        this->scores = new int[size];
        for(int i = 0; i < size; i++)
        {
            this->scores[i] = dept.scores[i];
        }
    }
    ~Dept(){
        delete [] scores;
    }
    int getSize(){return size;}
    void read(){
        cout << size << "๊ฐœ ์ •์ˆ˜ ์ž…๋ ฅ>> ";
        for(int i = 0; i < size; i++){
            cin >> scores[i];
        }
    }
    bool isOver60(int index){
        return scores[index] >= 60;
    }
};

int countPass(Dept dept){
    int count = 0;
    for(int i = 0; i < dept.getSize(); i++){
        if(dept.isOver60(i))
            count++;
    }
    return count;
}

int main()
{
    Dept com(10);
    com.read();
    int n = countPass(com);
    cout << "60์  ์ด์ƒ์€ " << n << "๋ช…";
}

(2)

counterPassํ•จ์ˆ˜์—์„œ ๊ฐ’์— ์˜ํ•œ ํ˜ธ์ถœ๋กœ ์–•์€ ๋ณต์‚ฌ๊ฐ€ ์ง„ํ–‰๋˜์–ด ๋ณต์‚ฌ์ƒ์„ฑ์ž๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. ์ด ๋•Œ ํ•จ์ˆ˜๊ฐ€ ๋๋‚˜๋ฉด์„œ com๊ฐ์ฒด์˜ scores๊ฐ€ ๋ฐ˜ํ™˜๋˜์—ˆ๋Š”๋ฐ
ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒํ•˜๋ฉด์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋” ์†Œ๋ฉธ์ž๊ฐ€ ์‹คํ–‰๋˜์–ด socres๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ ค๊ณ  ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜ ๋ฐœ์ƒ

(3)

์ฐธ์กฐ์— ์˜ํ•œ ํ˜ธ์ถœ๋กœ ์ˆ˜์ •

int countPass(Dept &dept){
    int count = 0;
    for(int i = 0; i < dept.getSize(); i++){
        if(dept.isOver60(i))
            count++;
    }
    return count;
}
profile
์น˜ํƒ€๊ฐ€ ๋˜๊ณ  ์‹ถ์€ ์ทจ์ค€์ƒ

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