728x90
< ์ฐ์ฐ์ >
#include <iostream>
using namespace std;
// ๋ฉ๋ชจ๋ฆฌ ์์ญ
// STACK - HEAP - DATA - CODE
// 01. ์ฐ์ฐ์
// ๋ฐ์ดํฐ ์ฐ์ฐ ๋ฐ ๊ฐ๊ณต ๊ฐ๋ฅ
// 1) ์ฐ์ ์ฐ์ฐ์
// 2) ๋์
์ฐ์ฐ์
// 3) ์ฆ๊ฐ ์ฐ์ฐ์
// 4) ๋น๊ต ์ฐ์ฐ์
// 5) ๋
ผ๋ฆฌ ์ฐ์ฐ์
// 6) ๋นํธ ์ฐ์ฐ์
// 7) ์ผํญ ์ฐ์ฐ์
int a = 1;
int b = 7;
bool isSame;
bool isDiff;
bool isGreater;
bool isSmaller;
bool test;
int hp = 100;
bool isInvincible = true;
unsigned char flag;
const int Air = 0;
const int Stun = 1;
const int PolyMorph = 2;
const int Invincible = 3;
int main()
{
// 01) ์ฐ์ ์ฐ์ฐ์
// ์ฌ์น ์ฐ์ฐ
// ๋ฐ๋ฏธ์ง ๊ณ์ฐ, ์ฒด๋ ฅ ๊น๊ธฐ, ๋ฃจํ๋ฌธ ๋ฑ ๋ค์ํ ๊ณณ์์ ์ฌ์ฉ
a = a + 3; // add (์ฐ์ฐ์๊ฐ ๊ฐ์ ์๋ฏธ)a += 3
cout << a << endl;
a = b - 3; // sub a -= 3
cout << a << endl;
a = b * 3; // mul a *= 3
cout << a << endl;
a = b / 3; // ๋๋์
div a /= 3
cout << a << endl;
a = b % 3; // ๋๋จธ์ง dic a %= 3
cout << a << endl;
// 02) ๋์
์ฐ์ฐ์
// = : ์ํ์์๋ ๊ฐ๋ค์ ์๋ฏธ์ง๋ง, ์ฝ๋ฉ์์ ๋์
a = b; // a๋ฅผ b์ ๋์
ํ๊ณ b ๋ฐํ
cout << a << " " << b << endl;
// += -= *= /= %=
// 03) ์ฆ๊ฐ ์ฐ์ฐ์
// 1์ฉ ์ฆ๊ฐ ํน์ ๊ฐ์์ํฌ ๋ ์ฌ์ฉ
// 7 7
b = a++;
cout << a << " " << b << endl; // 8 7
b = ++a;
cout << a << " " << b << endl; // 9 9
b = a--;
cout << a << " " << b << endl; // 8 9
b = --a;
cout << a << " " << b << endl; // 7 7
// 04) ๋น๊ต ์ฐ์ฐ์
// ๋ ํญ ์ฌ์ด์ ํฌ๊ธฐ๋ฅผ ๋น๊ตํ๋ ์ฐ์ฐ์
// ex) ์ฒด๋ ฅ์ด 0๋๋ฉด ์ฌ๋ง, ์ฒด๋ ฅ์ด 30%์ดํ๋ฉด ๋ฌด์ ๋ฐ๋ ๋ฑ
// ์ฆ ํน์ ์กฐ๊ฑด์ด ๋๋ฉด ์ด๋ ํ event
// a == b : a์ b๊ฐ ๊ฐ์๊ฐ?
// ๊ฐ์ผ๋ฉด 1, ๋ค๋ฅด๋ฉด 0
isSame = (a == b);
cout << isSame << endl;
// a != b : a์ b๊ฐ ๋ค๋ฅธ๊ฐ?
// ๋ค๋ฅด๋ฉด 1, ๊ฐ์ผ๋ฉด 0
isDiff = (a != b);
cout << isDiff << endl;
// a > b . a >= b
isGreater = (a > b);
cout << isGreater << endl;
// a < b . a <= b
isSmaller = (a < b);
cout << isSmaller << endl;
// 05) ๋
ผ๋ฆฌ ์ฐ์ฐ์
// ์ธ์ ํ์ํ๊ฐ? ์กฐ๊ฑด์ ๋ํ ๋
ผ๋ฆฌ์ ์ฌ๊ณ ๊ฐ ํ์
// ex) ๋ก๊ทธ์ธํ ๋ ์์ด๋๋ ๊ฐ๊ณ + ๋น๋ฒ๋ ๊ฐ์์ผ ํ๋ค (๋์์ ๋ง์กฑํด์ผ ํจ)
// ex) ๊ธธ๋ ๋ง์คํฐ์ด๊ฑฐ๋ ์ด์์ ๊ณ์ ์ด๋ฉด ๊ธธ๋ ํด์ฐ๊ฐ๋ฅ (OR)
// ! not
test = !isSame; // isSame ๊ฐ์ ๋ฐ๋๋ก, isDiff์ ๊ฐ์ ์๋ฏธ
cout << test << endl;
// && and
// a && b -> ๋๋ค 1์ด๋ฉด 1, ๊ทธ์ธ 0 (๋ ์กฐ๊ฑด ๋ง์กฑํด์ผํจ)
test = (hp <= 0 && isInvincible == false);; //์ฃฝ์
cout << test << endl;
// || or
// a || b -> ๋ ์ค ํ๋๋ผ๋ 1์ด๋ฉด 1 (๋๋ค 0์ด๋ฉด 0)
test = (hp > 0 || isInvincible == true); //์์กด
cout << test << endl;
// 06) ๋นํธ ์ฐ์ฐ์
// ์ธ์ ํ์ํ๊ฐ?
// ex) ๋นํธ ๋จ์์ ์กฐ์์ด ํ์ํ ๋
// ๋ํ์ ์ผ๋ก BitFlag
// ~ bitwise not
// ๋จ์ผ ์ซ์์ ๋ชจ๋ ๋นํธ๋ฅผ ๋์์ผ๋ก 0์ 1๋ก, 1์ 0์ผ๋ก ๋ฐ๊ฟ
// & bitwise and
// ๋ ์ซ์์ ๋ชจ๋ ๋นํธ ์์ ๋์์ผ๋ก and๋ฅผ ํ๋ค
// | bitwise or
// ๋ ์ซ์์ ๋ชจ๋ ๋นํธ ์์ ๋์์ผ๋ก or๋ฅผ ํ๋ค
// ^ bitwise wor
// ๋ ์ซ์์ ๋ชจ๋ ๋นํธ ์์ ๋์์ผ๋ก xorํ๋ค
// << ๋นํธ ์ข์ธก ์ด๋
// ๋นํธ์ด์ n๋งํผ ์ผ์ชฝ์ผ๋ก ์ด๋
// ์ผ์ชฝ์ ๋์น๋ n๊ฐ์ ๋นํธ๋ ๋ฒ๋ฆผ, ์๋ก ์์ฑ๋๋ n๊ฐ์ ๋นํธ๋ 0
// >> ๋นํธ ์ฐ์ธก ์ด๋
// ๋นํธ๋ฅผ n๋งํผ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋
// ์ค๋ฅธ์ชฝ์ ๋์น๋ n๊ฐ์ ๋นํธ๋ ๋ฒ๋ฆผ
// ์ผ์ชฝ ์์ฑ๋๋ n๊ฐ์ ๋นํธ๋
// - ๋ถํธ๊ฐ ์กด์ฌํ ๊ฒฝ์ฐ ๋ถํธ ๋นํธ๋ฅผ ๋ฐ๋ผ๊ฐ
// - ์๋๋ฉด 0
// Ob0000 [๋ฌด์ ][๋ณ์ด][์คํด][๊ณต์ค๋ถ์] 4bit
// ๋ฌด์ ์ํ๋ก ๋ง๋ ๋ค
flag = (1 << Invincible); // 0b1000 3์นธ์ด๋!
// ๋ณ์ด ์ํ
flag |= (1 << PolyMorph); // Ob1100
// ๋ฌด์ ์ธ์ง ํ์ธํ๊ณ ์ถ๋ค?
// bitmask
bool invincible = ((flag & (1 << Invincible)) != 0);
// ๋ฌด์ ์ด๊ฑฐ๋ ์คํด ์ํ์ธ์ง ํ์ธํ๊ณ ์ถ๋ค
bool stunOrInvincible = ((flag & 0b1010) != 0);
// 07. ์ผํญ ์ฐ์ฐ์
// ์กฐ๊ฑด์? ๋ณ์ 1 : ๋ณ์ 2
// ๊ฒฐ๊ณผ๊ฐ true๋ผ๋ฉด ๋ณ์ 1์ ๋ฐํ, false ๋ผ๋ฉด ๋ณ์ 2๋ฅผ ๋ฐํ
int x = 20;
int y = 14;
int result;
result = (x > y) ? x : y;
cout << result << endl;
}
< ํจ์ >
#include <iostream>
using namespace std;
// 02. ํจ์
// ์ ๋ฒ ์๊ฐ์ ๊ฐ์ฒด์ ๋ํด ์ค๋ช
ํ๋๋ฐ ์์ฑ๊ณผ ๊ธฐ๋ฅ์ด ์๋ค๊ณ ํ์๋ค.
// ์ค๋์ ์ด๋ ํ ๊ฐ์ฒด์ ๋ํ ๊ธฐ๋ฅ์ ๋ง๋๋ ค๊ณ ํ๋ค.
// ์์ฌ์ด๋ผ๋ ํ๋ ์ด์ด ๊ฐ์ฒด๊ฐ ์ด๋ํ๋ ๊ธฐ๋ฅ์ ๋ง๋ค๊ธฐ ์ํด์๋
// ์์ฌ์ด๊ฐ ์ด๋ํ ์ ์๋ค๋ ๊ฒ์ "์ ์ธ"ํด์ผ ํ๊ณ
// ๊ตฌ์ฒด์ ์ผ๋ก ์ด๋ป๊ฒ ์ด๋ํ๋์ง "์ ์"ํด์ผ ํ๋ฉฐ,
// ์ด๋ค ์ํฉ์์ ๋ฐ๋์ง ์๋ ค์ฃผ๋ ค๋ฉด ๋ฐ๋ ๊ธฐ๋ฅ์ "ํธ์ถ" ํด์ผ ํ๋ค
class YeSeul {
public:
void Move(); // ์ ์ธ
};
void YeSeul::Move() // ์ ์
{
cout << "์์ฌ์ด๋ ๋ฌ๋ฆฐ๋ค" << endl;
}
int main()
{
YeSeul player;
player.Move(); // ํธ์ถ
}
// * ํจ์์ ๊ตฌ์ฑ์์
// : ๋ฐํ๊ฐ ํจ์๋ช
{๋งค๊ฐ๋ณ์ ๋ชฉ๋ก}
// : ๋ฐํ๊ฐ - ํจ์๋ฅผ ์ํํ ๋ค์ ๋ฐํํ๋ ๊ฐ, ์์ผ๋ฉด void
// : ํจ์๋ช
- ํจ์ ์๋ณ์์
// : ๋งค๊ฐ๋ณ์ ๋ชฉ๋ก : ํจ์์ ๊ธฐ๋ฅ์ ์ํํ๊ธฐ ์ํ ์
๋ ฅ๊ฐ
#include <iostream>
using namespace std;
// ์๋์ ์ฝ๋๋ ์ปดํ์ผ ์๋ฌ ๋ฐ์
// ํด๋์ค ๋ณ์์ ํด๋์ค ๋ฉค๋ฒ ํจ์๋ ์ฌ์ฉ ์ ์ ์ ์ธ๋์ด์ผ ํ๋ค !!
// * ํจ์ ์ ์ธ
// ์ปดํ์ผ๋ฌ๋ ํจ์ ์ ์ธ์ ๋ณด๊ณ ๋ฐํ๊ฐ์ ์๋ฃํ์ด ๋ฌด์์ธ์ง ์๋ค
// ์ปดํ์ผ๋ฌ๋ ํจ์ ์ ์ธ์ ๋ณด๊ณ ๋งค๊ฐ ๋ณ์์ ๊ฐ์๊ฐ ์ฌ๋ฐ๋ฅธ์ง ํ์ธํ๋ค
// ์ปดํ์ผ๋ฌ๋ ํจ์ ์ ์ธ์ ๋ณด๊ณ ๋งค๊ฐ๋ณ์์ ์๋ฃํ์ด ์ฌ๋ฐ๋ฅธ์ง ํ์ธํ๋ค
// * ํจ์ ์ ์
// ์ ์๊ฐ ์์ ๊ฒฝ์ฐ -> LNK2019, LNK1120 ์ค๋ฅ
// ๊ธฐ๋ฅ์ ์ค์ ๋ก ๊ตฌํํ๋ ๋ถ๋ถ์ด๊ธฐ์ ์์ผ๋ฉด ์๋จ
int main()
{
YeSeul player;
player.Move(); // ํธ์ถ
}
class YeSeul {
public:
void Move(); // ์ ์ธ
};
void YeSeul::Move() // ์ ์
{
cout << "์์ฌ์ด๋ ๋ฌ๋ฆฐ๋ค" << endl;
#include <iostream>
using namespace std;
// * ๊ฐ์ ์ํ ํธ์ถ๊ณผ ์ฐธ์กฐ์ ์ํ ํธ์ถ & ๋งค๊ฐ๋ณ์
// ๊ฐ์ ์ํ ํธ์ถ์ ๋ณ์๋ฅผ ๋ณต์ฌํ๋ ๊ฒ๊ณผ ๊ฐ์ ๊ฐ๋
class Restaurant
{
public:
int steakCost;
};
class YeSeul {
public:
void Move(); // ์ ์ธ
int Eat(int);
int Eat2(int&);
int Eat3(int steakCost3);
int Eat4(int steakCost3, int coffeeCost, int iceCost);
};
void YeSeul::Move() // ์ ์
{
cout << "์์ฌ์ด๋ ๋ฌ๋ฆฐ๋ค" << endl;
}
int YeSeul::Eat(int steakCost2)
{
steakCost2 = 10000;
return steakCost2;
}
int YeSeul::Eat2(int& steakCost2)
{
steakCost2 = 10000;
return steakCost2;
}
int YeSeul::Eat3(int steakCost3)
{
cout << "์์ฌ์ด๋ " << steakCost3 << "์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน๋๋ค" << endl;
return steakCost3;
}
int YeSeul::Eat4(int steakCost3, int coffeeCost, int iceCost)
{
cout << "์์ฌ์ด๋ " << steakCost3 << "์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน๊ณ , " <<
coffeeCost << "์์ง๋ฆฌ ์ปคํผ๋ฅผ ๋ง์๊ณ , " <<
iceCost << "์์ง๋ฆฌ ์์ด์คํฌ๋ฆผ์ ๋จน๋๋ค." << endl;
return steakCost3;
}
int main()
{
YeSeul player;
Restaurant rest, rest2;
rest.steakCost = 20000;
rest2.steakCost = 20000;
// rest
cout << "์์ฌ์ด๋ " << player.Eat(rest.steakCost) <<
"์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน์๋ค๊ณ ๊ฑฐ์ง๋ง ํ๋ค." << endl;
cout << "๋ ์คํ ๋์ ์์ฌ์ด๊ฐ " << rest.steakCost <<
"์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน์๋ค๊ณ ์๊ณ ์๋ค." << endl;
// rest2
cout << "์์ฌ์ด๋ " << player.Eat2(rest.steakCost) <<
"์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน์๋ค๊ณ ๊ฑฐ์ง๋ง ํ๋ค." << endl;
cout << "๋ ์คํ ๋์ ์์ฌ์ด๊ฐ " << rest.steakCost <<
"์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน์๋ค๊ณ ์๊ณ ์๋ค." << endl;
// ๋งค๊ฐ๋ณ์
player.Eat3(30000);
player.Eat4(43333, 3000, 1000);
}
// main -> int YeSeul::Eat(int steakCost)ํจ์ ์คํ -> main
// rest ์คํ - ๊ฐ์ ์ํ ํธ์ถ
// ํจ์ ํธ์ถ -> ์คํ
์ดํฌ ๊ฐ๊ฒฉ์ด ๋งค๊ฐ๋ณ์๋ก ๋ค์ด๊ฐ. ๊ฐ์ ์ํ ํธ์ถ ๋ฐฉ์
// rest2 ์คํ - ์ฐธ์กฐ์ ์ํ ํธ์ถ
// ํจ์ ํธ์ถ -> ๋งค๊ฐ๋ณ์๋ฅผ ์คํ
์ดํฌ ๊ฐ๊ฒฉ์ผ๋ก ์ด๊ธฐํ,
// ์ฐธ์กฐ์์์ผ๋ก ๋จ์ํ ๊ฐ์ ๋ณต์ฌํ๋ ๊ฒ์ด ์๋ ์ฃผ์๋ฅผ ์ฐธ์กฐํ์ฌ
// Eat2 ํจ์์ streakCost๋ rest2์ steakCost ๋ณ์์ ๋ณ์นญ์ด ๋๋ค.
// ๋ฐ๋ผ ๋ ๋ณ์ ์ค ํ๋๋ผ๋ ๊ฐ์ ๋ณ๊ฒฝํ๊ฒ ๋๋ฉด ์คํ
์ดํฌ ๊ฐ์ด ๋ณ๊ฒฝ ๋จ
// ์ฐธ์กฐ์ ์ํ ํธ์ถ ๋ฐฉ์์ ํจ์ ๋ด๋ถ์์ ์ธ๋ถ์ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ค (๋งค์ฐ ์ค์)
// ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ๋ฌ ๊ฐ ๋ง๋ค ์ ์๋ค.
#include <iostream>
using namespace std;
// ** ํจ์ ์ค๋ฒ๋ก๋ฉ
// overloading : ์ง์ ๋ง์ด ์ฃ๋๋ค
// C++์ ์ค์ํ ๊ฐ๋
, ๋คํ์ฑ๊ณผ ๋ฐ์ ํ๊ธฐ์
// ์ค์ํ ๊ฐ๋
์ด๋ค
// ํ๋์ ํจ์์ ๊ณ์ ์๋๋ค
// ์ฐ๋ฆฌ๊ฐ ์ง๊ธ๊น์ง ํ๋ ๊ฒ์ ์ด๋ฆ์ ๋์ผํ๊ฒ ๋ง๋ค์
// ๋จ ๋งค๊ฐ๋ณ์ ๋ชฉ๋ก์ ๋ฌ๋ผ์ผ ํ๋ค. (์ฐธ์กฐ์ธ ๊ฒฝ์ฐ -> ํ์
์ด ๊ฐ์ผ๋ฉด x)
class Restaurant
{
public:
int steakCost;
};
class YeSeul {
public:
void Move(); // ์ ์ธ
int Eat(int);
double Eat(double);
int Eat(int steakCost3, int coffeeCost, int iceCost);
};
void YeSeul::Move() // ์ ์
{
cout << "์์ฌ์ด๋ ๋ฌ๋ฆฐ๋ค" << endl;
}
int YeSeul::Eat(int steakCost3)
{
cout << "์์ฌ์ด๋ " << steakCost3 << "์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน๋๋ค" << endl;
return steakCost3;
}
double YeSeul::Eat(double steakCost3)
{
cout << "์์ฌ์ด๋ " << steakCost3 << "์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน๋๋ค" << endl;
return steakCost3;
}
int YeSeul::Eat(int steakCost3, int coffeeCost, int iceCost)
{
cout << "์์ฌ์ด๋ " << steakCost3 << "์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน๊ณ , " <<
coffeeCost << "์์ง๋ฆฌ ์ปคํผ๋ฅผ ๋ง์๊ณ , " <<
iceCost << "์์ง๋ฆฌ ์์ด์คํฌ๋ฆผ์ ๋จน๋๋ค." << endl;
return steakCost3;
}
int main()
{
YeSeul player;
Restaurant rest, rest2;
rest.steakCost = 20000;
rest2.steakCost = 20000;
// rest
cout << "์์ฌ์ด๋ " << player.Eat(rest.steakCost) <<
"์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน์๋ค๊ณ ๊ฑฐ์ง๋ง ํ๋ค." << endl;
cout << "๋ ์คํ ๋์ ์์ฌ์ด๊ฐ " << rest.steakCost <<
"์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน์๋ค๊ณ ์๊ณ ์๋ค." << endl;
// ๋งค๊ฐ๋ณ์
player.Eat(30000);
player.Eat(30000.0);
player.Eat(43333, 3000, 1000);
}
< ์ธ๋ผ์ธ(inline) ํจ์ >
#include <iostream>
using namespace std;
// * inline ํจ์
// ์ฅ์ : ํจ์ ์ ์๋ถ๊ฐ ์คํ ์ฝ๋๋ฅผ ๋์ฒด ํ๋ฏ๋ก, ์คํ ์ ์ค๋ฒํค๋๊ฐ ์ค์ด ์๋ ๋น ๋ฆ
// ๋จ์ : ํจ์ ์ ์๋ถ๊ฐ ๊ธธ๋ฉด ์ธ๋ผ์ธ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ป์ ์ ์๋ ์ฅ์ ์ค์ด๋ฆ
// (ํธ์ถํ๋ ๊ตฌ๋ฌธ์ด ๋ชจ๋ ์ธ๋ผ์ธ ํจ์์ ์ ์๋ก ๋ฐ๋์ด ์ ์ฒด ์ฝ๋ ์์ด ์ฆ๊ฐํจ)
class YeSeul {
public:
void Eat(int);
inline void EatInline(int steakCost3)
{
cout << "EatInline() :: ์์ฌ์ด๋ " << steakCost3 << "์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน๋๋ค" << endl;
}
};
void YeSeul::Eat(int steakCost3)
{
cout << "Eat() :: ์์ฌ์ด๋ " << steakCost3 << "์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน๋๋ค" << endl;
}
int main()
{
YeSeul player;
player.Eat(500); // ์๋๋ฐฉ์ ๋ณต์ก ) main -> eatํจ์ -> ํจ์ ์คํ -> main
player.EatInline(1500); // (ํจ์ ํธ์ถ ๋จ๊ณ ์ค์) ์ฝ๋ ๋์ฒด๋จ ) cout << "EatInline() :: ์์ฌ์ด๋ " << ....
}
< ๋๋ค ํจ์ >
#include <iostream>
using namespace std;
// * ๋๋ค ํจ์
// ๋ณต์กํ ํจ์ ํธ์ถ ๊ณผ์ ์๋ตํด ์๊ฐ ์ ์ฝ ๋ฐ ๊ฐ๋
์ฑ UP
// : ์ธ๋ผ์ธ์ผ๋ก ์ ์์ ํธ์ถ์ ํ ๋ฒ์ ํ๋ ํจ์
// [์บก์ฒ] (๋งค๊ฐ๋ณ์ ๋ชฉ๋ก) mutable ์์ธ๋ชฉ๋ก -> ๋ฐํํ {ํจ์ ๋ชธ์ฒด}
class YeSeul {
public:
void Eat(int);
inline void EatInline(int steakCost3)
{
cout << "EatInline() :: ์์ฌ์ด๋ " << steakCost3 << "์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน๋๋ค" << endl;
}
};
void YeSeul::Eat(int steakCost3)
{
cout << "Eat() :: ์์ฌ์ด๋ " << steakCost3 << "์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน๋๋ค" << endl;
}
int main()
{
YeSeul player;
player.Eat(500);
player.EatInline(1500);
[](int steakCost3) {
cout << "eatLamda():: ์์ฌ์ด๋ " << steakCost3 << "์์ง๋ฆฌ ์คํ
์ดํฌ๋ฅผ ๋จน๋๋ค" << endl;}(1000);
}
< Class >
#include <iostream>
using namespace std;
#define NAME_LEN 20
#define SEX_LEN 10
#define JOB_LEN 20
#define CHARACTER_LEN 20
// 03. Class
// ๋ฐ์ดํฐ ์๋ (๊ตฌ์กฐ์ฒด์ ๋ค๋ฅธ ์ -> ์๋ ๊ทธ๋ฅ ๋ชจ๋ ๊ฒ public)
// : ํด๋์ค ์์ ์์ฑ๊ณผ ๊ธฐ๋ฅ์ ์ ํ์ ์ผ๋ก ์จ๊ธธ ์ ์๋ค
// ์จ๊ธฐ๋ ์ด์ : ์์ ์ ์์ฑ๊ณผ ๊ธฐ๋ฅ์ ๋ค๋ฅธ ์์ญ์์ ๋ณ๊ฒฝํ์ง ๋ชปํ๋๋ก ํด์ผํ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค
// ๋ฉค๋ฒ ์ ๊ทผ ์ง์ ์
// public : ํด๋์ค ๋ด๋ถ, ์ธ๋ถ ๋ชจ๋ ๋ฉค๋ฒ์ ์ ๊ทผ ๊ฐ๋ฅํจ -> ์ธํฐํ์ด์ค ํจ์
// private : ํด๋์ค ๋ด๋ถ์์๋ง ๋ฉค๋ฒ์ ์ ๊ทผ ๊ฐ๋ฅํจ -> ๋ณดํต ๋ฉค๋ฒ ๋ณ์
// protected : ๊ธฐ๋ณธ์ ์ผ๋ก ํด๋์ค ๋ด๋ถ ์์ญ์์๋ง ํด๋์ค ๋ฉค๋ฒ์ ์ ๊ทผ ๊ฐ๋ฅํ๋,
// ์์๊ด๊ณ์์๋ ์์ ํด๋์ค ๋ฉค๋ฒ์ ์ ๊ทผ ๊ฐ๋ฅํ๋๋ก ํ๋ค.
// ๊ฐ์ฒด๋ฅผ ๋ถ์ด๋นต ํ์ฒ๋ผ ์ฐ์ด๋ผ ์ ์์ผ๋ฉฐ
// ์ด๋ค์ ์๋ก ์ํธ์์ฉ ํ ์ ์์
class Person {
public:
// ์์ฑ์์ ๊ฐ์ ์ญํ , ์ถํ์ ๋ฐฐ์ธ ์์
void constructor(const char* name, const char* sex, const char* job, const char* character, int age, bool marriageStatus)
{
strcpy_s(this->name, name);
strcpy_s(this->sex, sex);
strcpy_s(this->job, job);
strcpy_s(this->character, character);
this->age = age;
this->marriageStatus = marriageStatus;
// ์์ ์ ๊ฐ๋ฅดํค๋ ํฌ์ธํฐ (this) : ์ถํ ๋ฐฐ์ธ ์์
}
void introduce()
{
cout << "์ด๋ฆ: " << name << endl;
cout << "์ฑ๋ณ: " << sex << endl;
cout << "์ง์
: " << job << endl;
cout << "์ฑ๊ฒฉ: " << character << endl;
cout << "๋์ด: " << age << endl;
cout << "๊ฒฐํผ์ฌ๋ถ: " << (marriageStatus ? "YES" : "NO") << endl;
}
void eat(const char* food)
{
cout << name << "์(๋) " << food << "๋ฅผ ๋จน๋๋ค" << endl;
}
void sleep()
{
cout << name << "์(๋) ์๋ค" << endl;
}
void drive(const char* destination)
{
cout << name << "์(๋)" << destination << "์ผ๋ก ์ด์ ํ๋ค" << endl;
}
void write()
{
cout << name << "์(๋) ์ฑ
์ ์ด๋ค" << endl;
}
void read()
{
cout << name << "์(๋) ์ฑ
์ ์ฝ๋๋ค" << endl;
}
private:
char name[NAME_LEN];
char sex[SEX_LEN];
char job[JOB_LEN];
char character[CHARACTER_LEN];
int age;
bool marriageStatus;
};
int main()
{
Person player1, player2;
player1.constructor("๋ฏธ์", "์ฌ์ฑ", "์ฝ๋ฉ๋
ธ์", "๊ณ ์ง", 23, false);
player2.constructor("๋ฐ๋ณด", "๋จ์ฑ", "ํ์", "์ฑ์ค", 23, true);
player1.introduce();
player2.introduce();
player1.eat("๋ง๋ผํ");
}
#include <iostream>
using namespace std;
#define NAME_LEN 20
#define SEX_LEN 10
#define JOB_LEN 20
#define CHARACTER_LEN 20
// 03. Class
// ๋ฉค๋ฒํจ์ ์ ์ธ๊ณผ ์ ์ ๋ถ๋ฆฌ
// :: (๋ฒ์ ์ง์ ์ฐ์ฐ์) ์ฌ์ฉ
class Person {
public:
void constructor(const char* name, const char* sex, const char* job, const char* character, int age, bool marriageStatus);
void introduce();
void eat(const char* food);
void sleep();
void drive(const char* destination);
void write();
void read();
private:
char name[NAME_LEN];
char sex[SEX_LEN];
char job[JOB_LEN];
char character[CHARACTER_LEN];
int age;
bool marriageStatus;
};
int main()
{
Person player1, player2;
player1.constructor("๋ฏธ์", "์ฌ์ฑ", "์ฝ๋ฉ๋
ธ์", "๊ณ ์ง", 23, false);
player2.constructor("๋ฐ๋ณด", "๋จ์ฑ", "ํ์", "์ฑ์ค", 23, true);
player1.introduce();
player2.introduce();
player1.eat("๋ง๋ผํ");
}
void Person::constructor(const char* name, const char* sex, const char* job, const char* character, int age, bool marriageStatus)
{
strcpy_s(this->name, name);
strcpy_s(this->sex, sex);
strcpy_s(this->job, job);
strcpy_s(this->character, character);
this->age = age;
this->marriageStatus = marriageStatus;
// ์์ ์ ๊ฐ๋ฅดํค๋ ํฌ์ธํฐ (this) : ์ถํ ๋ฐฐ์ธ ์์
}
void Person::introduce()
{
cout << "์ด๋ฆ: " << name << endl;
cout << "์ฑ๋ณ: " << sex << endl;
cout << "์ง์
: " << job << endl;
cout << "์ฑ๊ฒฉ: " << character << endl;
cout << "๋์ด: " << age << endl;
cout << "๊ฒฐํผ์ฌ๋ถ: " << (marriageStatus ? "YES" : "NO") << endl;
}
void Person::eat(const char* food)
{
cout << name << "์(๋) " << food << "๋ฅผ ๋จน๋๋ค" << endl;
}
void Person::sleep()
{
cout << name << "์(๋) ์๋ค" << endl;
}
void Person::drive(const char* destination)
{
cout << name << "์(๋)" << destination << "์ผ๋ก ์ด์ ํ๋ค" << endl;
}
void Person::write()
{
cout << name << "์(๋) ์ฑ
์ ์ด๋ค" << endl;
}
void Person::read()
{
cout << name << "์(๋) ์ฑ
์ ์ฝ๋๋ค" << endl;
}
'๐ค Study > C++ & C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
05. ์์๊ณผ ํฌํจ (0) | 2024.04.11 |
---|---|
04. ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ / ์ด๋ ์์ฑ์&์ด๋ ๋์ ์ฐ์ฐ์ / ๋์ ๋ฉ๋ชจ๋ฆฌ (1) | 2024.03.27 |
03. C++ ์ฃผ์ ๊ธฐ๋ฅ & ์์ฑ์/์๋ฉธ์/๋ณต์ฌ์์ฑ์ (0) | 2024.03.20 |
01. ๊ฐ์ฒด์ ๊ธฐ๋ณธ ๋ฌธ๋ฒ (0) | 2024.03.12 |