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;
}
'๐ค Study > C++ & C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
04. ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ / ์ด๋ ์์ฑ์&์ด๋ ๋์ ์ฐ์ฐ์ / ๋์ ๋ฉ๋ชจ๋ฆฌ (1) | 2024.03.27 |
---|---|
03. C++ ์ฃผ์ ๊ธฐ๋ฅ & ์์ฑ์/์๋ฉธ์/๋ณต์ฌ์์ฑ์ (0) | 2024.03.20 |
02. ์ฐ์ฐ์ / ํจ์ / ํด๋์ค (0) | 2024.03.13 |
01. ๊ฐ์ฒด์ ๊ธฐ๋ณธ ๋ฌธ๋ฒ (0) | 2024.03.12 |