League Of Legends - Link Select 05. ์ƒ์†๊ณผ ํฌํ•จ
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿค“ Study/C++ & C#

05. ์ƒ์†๊ณผ ํฌํ•จ

by GAMEMING 2024. 4. 11.
728x90

 

 - ์ƒ์†๊ณผ ํฌํ•จ

// - ์ƒ์† (is a kind of, is a)
// ๋ง๊ทธ๋Œ€๋กœ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๋ฉค๋ฒ„๋ฅผ ๋ฌผ๋ ค ๋ฐ›์€ ๊ฒƒ + ์ƒˆ๋กœ์šด ๋ฉค๋ฒ„๋ฅผ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ

// ํŒŒ์ƒ ํด๋ž˜์Šค
// class ํŒŒ์ƒํด๋ž˜์Šค : public: ๋ถ€๋ชจํด๋ž˜์Šค

// ๋‹ค์ค‘์ƒ์† ์กด์žฌ

// : - ์ƒ์†์„ ๋‚˜ํƒ€๋ƒ„ 

// ํฌํ•จ - ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๊ฐ€ ์žˆ๋Š” ์ƒํ™ฉ (has a)

class AAA{
public:
   int a=10;
public:
  output(){ };
}
class CCC{
public:
   int c=30;
   AAA p;
public:
  output(){ cout<<p.a+b<<endl;;};
}


// ์ƒ์†
class A {
public:
	int a = 10;
public:
	void output() { };
};

class B : public A {
public:
	int b = 30;
public:
	void output() { cout << a << ", " << b << endl;; };
};

 

class Vehicle {
	int serial;
protected:
	int speed;
public:
	int price;
	Vehicle(int s = 0) : serial(s), speed(0), price(0){}
	void speedUp() { speed++; }
	void speedDown() { speed--; }
	void print() 
	{
		cout << "[Vehicle] serial : " << serial << ", speed :" << speed << ", price : " << price << endl;
	}
};

class Car : public Vehicle {
	int gear;
public:
	Car(int g = 0) : gear(g) {}
	void pushAccel() { speed += 5; }
	void pushBrake() { speedDown(); }
	void setGear(int g) { gear = g; }
	void printCarInfo(const char* msg) {
		cout << msg << " speed :" << speed << ", price : " << price << ", gear : "<< gear << endl;
	}
};

int main()
{
	Car myCar(2), dreamCar(5);

	myCar.pushAccel();
	myCar.price = 3000;
	dreamCar.price = 10000;

	myCar.print();
	dreamCar.print();

	myCar.printCarInfo("[My Car]");
	dreamCar.printCarInfo("[Dream Car]");
}


// ๋ฉค๋ฒ„ํ•จ์ˆ˜ ์ค‘ ์ƒ์†๋˜์ง€ ์•Š์€ ํ•จ์ˆ˜ -> ์ƒ์„ฑ์ž, ๋ณต์‚ฌ์ƒ์„ฑ์ž, ๋Œ€์ž… ์—ฐ์‚ฐ์ž, ์†Œ๋ฉธ์ž
๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ ‘๊ทผ ์ง€์ •์ž private ์ƒ์† protected ์ƒ์† public ์ƒ์†
private ์ž์‹์—์„œ ์ง์ ‘ ์ ‘๊ทผ ๋ถˆ๊ฐ€
private ๋ฉค๋ฒ„๊ฐ€ ๋จ
์ž์‹์—์„œ ์ง์ ‘ ์ ‘๊ทผ ๋ถˆ๊ฐ€
private ๋ฉค๋ฒ„๊ฐ€ ๋จ
์ž์‹์—์„œ ์ง์ ‘ ์ ‘๊ทผ ๋ถˆ๊ฐ€
private ๋ฉค๋ฒ„๊ฐ€ ๋จ
protected ์ž์‹์—์„œ ์ง์ ‘ ์ ‘๊ทผ ๊ฐ€๋Šฅ
private ๋ฉค๋ฒ„๊ฐ€ ๋ณ€ํ•จ
์ž์‹์—์„œ ์ง์ ‘ ์ ‘๊ทผ ๊ฐ€๋Šฅ
protected  ๋ฉค๋ฒ„
์ž์‹์—์„œ ์ง์ ‘ ์ ‘๊ทผ ๊ฐ€๋Šฅ
protected ๋ฉค๋ฒ„
public ์ž์‹์—์„œ ์ง์ ‘ ์ ‘๊ทผ ๊ฐ€๋Šฅ
private ๋ฉค๋ฒ„๊ฐ€ ๋ณ€ํ•จ
์ž์‹์—์„œ ์ง์ ‘ ์ ‘๊ทผ ๊ฐ€๋Šฅ
protected ๋ฉค๋ฒ„๋กœ ๋ณ€ํ•จ
์ž์‹์—์„œ ์ง์ ‘ ์ ‘๊ทผ ๊ฐ€๋Šฅ
public ๋ฉค๋ฒ„

 

 

 

- ์˜ค๋ฒ„๋ผ์ด๋”ฉ(overriding)

// ์˜ค๋ฒ„๋ผ์ด๋”ฉ
// ๋ถ€๋ชจ์—๊ฒŒ ์ •์˜๋œ ๋ฉค๋ฒ„๋ฅผ ์ž์‹ ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ํ•˜๋Š” ๊ฒƒ

// ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์˜ ์žฌ์ •์˜ X -> ๊ฐ์ฒด์— ๋™์ผํ•œ ์˜๋ฏธ์˜ ๋ณ€์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ๊ธธ ์ˆ˜ ์žˆ์Œ

class A {
public:
	void overriding() { cout << "A ํด๋ž˜์Šค์˜ over ํ•จ์ˆ˜ ํ˜ธ์ถœ!" << endl; }
};

class B : public A {
public:
	void overriding() { cout << "B ํด๋ž˜์Šค์˜ over ํ•จ์ˆ˜ ํ˜ธ์ถœ!" << endl; }
};

int main()
{
	B b;
	b.overriding();
	return 0;
}

 

// ์˜ค๋ฒ„๋กœ๋”ฉ
// - ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์ƒ์œ„ ํด๋ž˜์Šค์— ์กด์žฌ
// - ๋ฉ”์†Œ๋“œ ์ด๋ฆ„์ด ๊ฐ™์•„์•ผ ํ•จ
// - ํ”ผ๋ผ๋ฏธํ„ฐ์˜ ์ˆ˜์™€ ๊ทธ ํ”ผ๋ผ๋ฏธํ„ฐ์˜ ์ž๋ฃŒํ˜•์ด ๊ฐ™์•„์•ผ ํ•จ
// - ๋ฆฌํ„ดํ˜•๋„ ๊ฐ™์•„์•ผ ํ•จ
// - ์ƒ์œ„ ๋ฉ”์†Œ๋“œ์™€ ๋™์ผํ•˜๊ฑฐ๋‚˜ ๋‚ด์šฉ์ด ์ถ”๊ฐ€๋˜์–ด์•ผ ํ•จ


class Base {
private:
	string str;

public:
	Base(const string& _str) { str = _str; }
	void printStr() { cout << "๊ธฐ์ดˆํด๋ž˜์Šค: " << str << endl; }
};

class Derived : public Base {
private:
	int num;

public:
	Derived(const string& _str, int _num) : Base(_str) { num = _num; }
	void printStr() { cout << "ํŒŒ์ƒํด๋ž˜์Šค: " << num << endl; }	// str ๋Œ€์‹  num ์ถœ๋ ฅํ•˜๋„๋ก ์˜ค๋ฒ„๋ผ์ด๋”ฉ
};

int main() {
	Base* ptr;
	Base a("base class");
	Derived b("derived class", 10);

	ptr = &a;
	ptr->printStr();
	ptr = &b;	// Derived๋Š” Base๋ฅผ ์ƒ์†๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— is-a ๊ด€๊ณ„. ๋”ฐ๋ผ์„œ Base ๊ฐ์ฒด ํฌ์ธํ„ฐ๋Š” Derived ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ์Œ
	ptr->printStr();	// ์ด๋ฅผ ์—… ์บ์ŠคํŒ…(ํŒŒ์ƒ ํด๋ž˜์Šค์—์„œ ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค๋กœ ์บ์ŠคํŒ…ํ•˜๋Š” ๊ฒƒ)์ด๋ผํ•œ๋‹ค.

	return 0;
}

// ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ง์ ‘ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•  ๋•Œ์˜ ๋ฌธ์ œ์ ์€ -> ํฌ์ธํ„ฐ
// ptr์€ base ํด๋ž˜์Šค ํƒ€์ž…์˜ ํฌ์ธํ„ฐ๋กœ ์ •์˜๋˜์–ด ์žˆ๋‹ค.

// ptr์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด๋ฅผ ๋ณ€๊ฒฝํ•˜๋”๋ผ๋„ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ํฌ์ธํ„ฐ ptr์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ณ€์ˆ˜์˜ ํƒ€์ž… ๊ธฐ์ค€์œผ๋กœ ํ•จ์ˆ˜ ํ˜ธ์ถœ
// ๋”ฐ๋ผ ์ž์‹์ด ์•„๋‹Œ ๋ถ€๋ชจ ํด๋ž˜์Šค๊ฐ€ ํ˜ธ์ถœ๋จ -> viraual ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ


class Base {
private:
	string str;

public:
	Base(const string& _str) { str = _str; }
	virtual void printStr() { cout << "๊ธฐ์ดˆํด๋ž˜์Šค: " << str << endl; }
};

class Derived : public Base {
private:
	int num;

public:
	Derived(const string& _str, int _num) : Base(_str) { num = _num; }
	virtual void printStr() override { cout << "ํŒŒ์ƒํด๋ž˜์Šค: " << num << endl; }
	// ๊ธฐ์ดˆ ํด๋ž˜์Šค์—์„œ๋งŒ virtual ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋˜์ง€๋งŒ, ์ด์™•์ด๋ฉด ํŒŒ์ƒ ํด๋ž˜์Šค์—์„œ๋„ 
	// virtual ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ๊ฐ€์ƒํ•จ์ˆ˜์ž„์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋“œ๋Ÿฌ๋‚ด๋Š” ํŽธ์ด ์ข‹๋‹ค.
	// override ํ‚ค์›Œ๋“œ ๋ช…์‹œํ•ด ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•  ์ˆ˜ ์žˆ์Œ
};

int main() {
	Base* ptr;
	Base a("base class");
	Derived b("derived class", 10);

	ptr = &a;
	ptr->printStr();
	ptr = &b;
	ptr->printStr();

	return 0;
}