[c++] ๐ŸฆŠ ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ

ํŒŒ์ดํ†จ์น˜ยท2022๋…„ 5์›” 24์ผ
0

๋Œ€ํ•™์ˆ˜์—…

๋ชฉ๋ก ๋ณด๊ธฐ
19/32

๐Ÿ‘€ ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ

์˜ค๋ฒ„๋กœ๋”ฉ์ด ๋ฌด์—‡์ธ๊ฐ€? ๋ฎ์–ด ์”Œ์šฐ๋Š” ๊ฒƒ์ด๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์€ ๋ฌด์—‡์ผ๊นŒ? ๋‹น์—ฐํžˆ ์—ฐ์‚ฐ์ž๋ฅผ ์ƒˆ๋กœ ๋ฎ์–ด ์”Œ์šฐ๋Š” ๊ฒƒ์ด๋‹ค!

์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์€ ์–ด๋–ป๊ฒŒ ํ• ๊นŒ? ๊ทธ๋ฆฌ๊ณ  ์™œ ํ• ๊นŒ? ํ•  ๋•Œ ์ฃผ์˜ ํ•  ์ ์€ ๋ฌด์—‡์ด ์žˆ์„๊นŒ?

Overloading +

class vector2D
{
private:
  int x;
  int y;
public:
  vector2D();
  vector2D(int X, int Y);
  int getX() const;
  int getY() const;
  void output() const;
};

๋‹ค์Œ๊ณผ ๊ฐ™์€ ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž. ๋ฒกํ„ฐ์—์„œ ๋”ํ•˜๊ธฐ ์—ฐ์‚ฐ์€ ๊ต‰์žฅํžˆ ์ค‘์š”ํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์ด ์ƒํƒœ์—์„œ๋Š” ๋”ํ•˜๊ธฐ๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์—†๋‹ค.

๋•Œ๋ฌธ์— ๋”ํ•˜๊ธฐ์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ c++์—์„œ ๋”ํ•˜๊ธฐ ํ•จ์ˆ˜๋ณด๋‹ค ๋” ์ข‹์€ ๊ฒƒ์„ ์ œ๊ณตํ•ด์ค€๋‹ค. ๋ฐ”๋กœ ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋“œ์ด๋‹ค. ๋ณดํ†ต +์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ •์ˆ˜๋‚˜ ์‹ค์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๊ฒƒ์„ ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹์œผ๋กœ ๋ง์ด๋‹ค.

int main(void)
{
  vector2D v1(2, 2), v2(3, 3), v3;
  v3 = v1 + v2;
}

๐Ÿ–ฅ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ / ์™ธ๋ถ€์—์„œ ์„ ์–ธ

const vector2D operator +(const vector2D& v1, const vector2D& v2)
{
  int x = v1.getX() + v2.getX();
  int y = v1.getY() + v2.getY();
  return vector2D(x, y);
}

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋ฉด ์—ฐ์‚ฐ์ž๋ฅผ ์˜ค๋ฒ„๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ๋‹ค. vector2Dํ˜• operator + ๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉด ์กฐ๊ธˆ ํŽธํ•  ๊ฒƒ์ด๋‹ค.

์ง€๊ธˆ ๋ณด์—ฌ์ค€ ๊ฒƒ์€ ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ์„ ์–ธ ํ•œ ๊ฒƒ์ด๋ฉฐ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋„ ์•„๋‹ˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด์ œ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋กœ์จ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์„ ์–ธํ•˜๋ฉด ๋ชจ์–‘์ด ์กฐ๊ธˆ ๋‹ฌ๋ผ์ง„๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹์œผ๋กœ ๋ง์ด๋‹ค.

๐Ÿ–ฅ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋กœ ํฌํ•จ๋œ ๊ฒฝ์šฐ / friend๋ฅผ ์“ฐ์ง€ ์•Š์„ ๋•Œ

class vector2D
{
private:
  int x;
  int y;
public:
  vector2D();
  vector2D(int X, int Y);
  int getX() const;
  int getY() const;
  void output() const;
  vector2D operator+(vector2D v);
};

vector2D vector2D::operator+(vector2D v)
{
  int new_x = x + v.getX();
  int new_y = y + v.getY();
  return vector2D(new_x, new_y);
}

๊ทผ๋ฐ ์—ฌ๊ธฐ์„œ ์กฐ๊ธˆ ๋ถˆํŽธํ•œ ์ ์ด ์žˆ๋‹ค. ๋ฐ”๋กœ accessor/mutator์˜ ์กด์žฌ์ด๋‹ค.
(getX, setX์™€ ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ๋งํ•จ)

๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” friend๋ผ๋Š” ๊ฒƒ์„ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ’ก ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋ฉด friend๋ผ๋ฆฌ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ข€ ๋” ์‰ฝ๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์‹ค friend๋ฅผ ์“ฐ๊ฒŒ ๋˜๋ฉด ํ˜•์‹์€ ๋ฉค๋ฒ„๋ณ€์ˆ˜๊ฐ€ ์•„๋‹ ๋•Œ์™€ ๋” ๋น„์Šทํ•˜๋‹ค ํ•˜์ง€๋งŒ ์ฐจ์ด์ ์€ ๋ฉค๋ฒ„๋ณ€์ˆ˜์— ์ง์ ‘ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์‚ฌ์‹ค์ด๋‹ค.

๐Ÿ–ฅ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋กœ ํฌํ•จ๋œ ๊ฒฝ์šฐ / friend๋ฅผ ์“ธ ๋•Œ

class vector2D
{
private:
  int x;
  int y;
public:
  vector2D();
  vector2D(int X, int Y);
  int getX() const;
  int getY() const;
  void output() const;
  friend vector2D operator+(vector2D v1, vector2D v2);
};

vector2D operator+(vector2D v1, vector2D v2)
{
  int x = v1.x + v2.x;
  int y = v1.y + v2.y;
  return vector2D(x, y);
}

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ์ข€ ๋” ์ง๊ด€์ ์ธ ์ฝ”๋”ฉ์„ ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ๊ผญ ๊ธฐ์–ตํ•˜์ž!

Overloading <<

๋”ํ•˜๊ธฐ ์—ฐ์‚ฐ์ž์™€ ๋”ฐ๋กœ ๊ตฌ๋ถ„ํ•œ ์ด์œ ๋Š” << ์—ฐ์‚ฐ์ž๋Š” ์กฐ๊ธˆ ๋…ํŠนํ•œ ์นœ๊ตฌ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์™œ ํŠน์ดํ•˜๋ƒ๋ฉด ์ด๊ฒƒ์€ ostream์„ ๊ฑฐ์น˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ฐธ๊ณ ๋กœ, >> ์—ฐ์‚ฐ์ž๋Š” istream์„ ๊ฑฐ์นœ๋‹ค. ์ด๊ฒƒ๋“ค๋„ ํด๋ž˜์Šค ์ด๋ฆ„์ธ๋ฐ ์กฐ๊ธˆ ๋…ํŠนํ•œ ์นœ๊ตฌ๋“ค์ด๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์‚ฌ์‹ค ๋‚˜๋„ ์™œ ์ €๋ ‡๊ฒŒ ์“ฐ๋Š”์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค... ๋‚˜~์ค‘์— ๊ณต๋ถ€ํ•ด์•ผ์ง€ ใ…Žใ…Ž

class vector2D
{
private:
  int x;
  int y;
public:
  vector2D();
  vector2D(int X, int Y);
  friend vector2D operator+(vector2D v1, vector2D v2);
  friend ostream& operator<<(ostream& outStream, vector2D v);
};

ostream& operator<<(ostream& outstream, vector2D v)
{
  outstream << "(" << v.x << ","<< v.y <<")" << endl;
  return outstream;
}

๐Ÿ’ก friend๋ฅผ ์„ ์–ธ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง์ ‘ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

int main(void)
{
  vector2D v1(2, 2), v2(3, 3), v3;
  cout << v1 << v2;
  v3 = v1+ v2;
  cout << v3;
}

์ด์ œ output ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ํ•  ํ•„์š” ์—†์ด << ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด์„œ ๋ฐ”๋กœ๋ฐ”๋กœ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•ด์กŒ๋‹ค!

Overloading >>

/// ์ค‘๋žต
	  friend istream& operator>>(istream& inputStream, vector2D& v);
};
istream& operator>>(istream& inputStream, vector2D& v )
{ 
  cout << "INPUT VECTOR: ";
  int x, y;
  inputStream >> x;
  inputStream >> y;
  v.x = x;
  v.y = y;

  return inputStream;
}

๐Ÿ’ก ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฐธ์กฐ์ž & ๋Š” ๊ผญ ๋ถ™์—ฌ์ค˜์•ผ ํ•œ๋‹ค!
๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋‚ด๊ฐ€ ์›์น˜ ์•Š๋Š” ๊ฐ’์ด ๋‚˜์˜จ๋‹ค!


int main(void)
{
  vector2D v1(2, 2), v2(3, 3), v3;
  cin >> v1;
  cin >> v2;
  cout << v1 << v2;
  v3 = v1 + v2;
  cout << v3;
}

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ’์„ ๋„ฃ์–ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค!

profile
์•ˆ์•Œ๋žด์คŒ

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