๐์์
์ฝ๋์ ์ค๋ณต์ ์ค์ด๊ณ ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๊ธฐ ์ํด ํด๋์ค ๊ฐ์ ์์ ๊ด๊ณ๋ฅผ ์ ์ํ ์ ์๋ค.
์๋ฅผ ๋ค์ด `name`๊ณผ `address`๋ฅผ ๊ฐ๋ `RagularEmployee`์ `TemporaryEmployee`๊ฐ ์์ ๋, ๋์ ` Employee`๋ก ์ผ๋ฐํํ์ฌ ์ ์ํ๊ณ ์์๋ฐ์ ์ฌ์ฉํ ์ ์๋ค.
#include <iostream>
using namespace std;
class Employee {
public :
Employee() {
}
Employee(const char* name, const char* address) {
this->name = new char[strlen(name) + 1];
this->address = new char[strlen(address) + 1];
strcpy_s(this->name, strlen(name) + 1, name);
strcpy_s(this->address, strlen(address) + 1, address);
}
~Employee() {
delete[] name;
delete[] address;
}
char* getName() {
return this->name;
}
char* getAddress() {
return this->address;
}
void print() {
cout << "์ด๋ฆ: " << this->getName() << ", ์ฃผ์: " << this-> getAddress() << endl;
}
protected:
char* name;
char* address;
};
class RegularEmployee : public Employee {
private :
int ์ฐ๋ด;
};
class TemporaryEmployee : public Employee {
private :
int ์ฃผ๊ธ;
};
๋์ ํด๋์ค ๋ด๋ถ์ ๋ ๋ค์ ์์ฑ์ ์ ์ํ์ง ์๋๋ผ๋ ์์์ ๋ฐ์๊ธฐ ๋๋ฌธ์ ์ด๋ฆ๊ณผ ์ฃผ์๋ฅผ ๊ฐ์ง๋ค. ๋ฉค๋ฒ ํจ์๋ ํจ๊ป ์์๋ฐ์ผ๋ฏ๋ก `getter()` ์ `print()` ๋ ์ฌ์ฉํ ์ ์๋ค.
์ด๋ `private` ์ผ๋ก ์ ์ธ๋ ๋ฉค๋ฒ ๋ณ์๋ ์์์ด ๋๋๋ผ๋ ์ ๊ทผ ์ ํ์ ๋ฐ๊ธฐ ๋๋ฌธ์, ๋ง์ฝ ์์์๊ฒ ์ ๊ทผ ๊ถํ์ ์ฃผ๊ณ ์ถ๋ค๋ฉด `protected` ๋ก ์ค์ ํด์ผํ๋ค.
๊ฐ์ฒด ์์ฑ ์์๋ ๋ฌด์กฐ๊ฑด ๋ถ๋ชจ์ ์์ฑ์๋ฅผ ๋จผ์ ํธ์ถํ๋ค.(๋ถ๋ชจ๊ฐ ์กด์ฌํ๋์ง ํ์ธํ ํ ๋ถ๋ชจ๋ฅผ ๋จผ์ ๋ง๋ค๊ธฐ ๋๋ฌธ์ด๋ค.) ๋ถ๋ชจ์ ์ด๋ค ์์ฑ์๋ฅผ ํธ์ถํ ์ง ๋ช ์ํ์ง ์์ผ๋ฉด ๋ฌด์กฐ๊ฑด ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ํธ์ถํ๋ค.
์ํ๋ ๋ถ๋ชจ์ ์์ฑ์๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ๋ถ๋ชจ์ ์์ฑ์ ์ค ์ด๋ค ์์ฑ์๋ฅผ ํธ์ถํ ์ง ์์ฑ์์ ์๊ทธ๋์ฒ์ ๋ช ์ํด์ผํ๋ค.
๊ฐ์ฒด๊ฐ ์คํ์ ์์ฑ๋๊ธฐ ๋๋ฌธ์, ํ์ ํด๋์ค๊ฐ ๋จผ์ ์๋ฉธ๋๊ณ ์์ ํด๋์ค๊ฐ ์๋ฉธ๋๋ค. ์ด๋ ์์ ํด๋์ค์ ์๋ฉธ์๋ฅผ ๋จผ์ ํธ์ถํ๊ณ , ๋ถ๋ชจ ํด๋์ค์ ์๋ฉธ์๋ฅผ ํธ์ถํ๋ค.
class RegularEmployee : public Employee {
public :
RegularEmployee() {
cout << "reg ๋ํดํธ ์์ฑ์ " << endl;
}
RegularEmployee(const char* name, const char* address, double salary)
: Employee(name, address) {
cout << "reg ์ธ์ ์ธ๊ฐ ์์ฑ์ " << endl;
this->salary = salary;
}
~RegularEmployee() {
cout << "rag ์๋ฉธ์ ํธ์ถ" << endl;
}
double PayCheck() {
return this->salary;
}
private :
double salary;
};
void main() {
Employee e("ํ์ฌ์", "ํ์ฌ์ ์ฃผ์");
e.print();
RegularEmployee re("์ ๊ท์ง์", "์ ๊ท์ง์ ์ฃผ์", 5500);
cout << re.PayChack() << endl;
TemporaryEmployee te("๊ณ์ฝ์ง", "๊ณ์ฝ์ง ์ฃผ์", 10, 20);
cout << te.PayCheck() << endl;
}
emp ์ธ์ ๋๊ฐ ์์ฑ์
์ด๋ฆ: ํ์ฌ์, ์ฃผ์: ํ์ฌ์ ์ฃผ์
emp ์ธ์ ๋๊ฐ ์์ฑ์
reg ์ธ์ ์ธ๊ฐ ์์ฑ์
5500
emp ์ธ์ ๋๊ฐ ์์ฑ์
temp ์ธ์ ์ธ๊ฐ ์์ฑ์
2400
temp ์๋ฉธ์ ํธ์ถ
emp ์๋ฉธ์ ํธ์ถ
rag ์๋ฉธ์ ํธ์ถ
emp ์๋ฉธ์ ํธ์ถ
emp ์๋ฉธ์ ํธ์ถ
๐๋คํ์ฑ(Polymorphism)
์ฌ์ : ๊ฐ์ ์ข ์ ์๋ฌผ์ด๋ฉด์๋ ์ด๋ค ํํ๋ ์ฑ์ง์ด ๋ค์ํ๊ฒ ๋ํ๋๋ ํ์. 'poly(๋ง์)' ๊ณผ 'morphism(ํํ)'์ ํฉ์ฑ์ด.
ํ๋์ ๊ฐ์ฒด๋ฅผ ๋ค๋ฅธ ์๋ฃํ์ ๋ณ์๋ก ์ฐธ์กฐํ์ฌ ๋ค์ํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ด๋ผ ์ ์๋ ์ฑ์ง. ํด๋์ค๊ฐ ์์ ๊ด๊ณ์ ์์ ๋ ๋ํ๋๋ ๋ค์ฑ๋ก์ด ์ฑ์ง์ ๋งํ๋ค.
๋์ ๊ฒฐํฉ/๋์ ๋ฐ์ธ๋ฉ(Dynamic Binding)
์คํ ๋์ค์ ๊ฒฐํฉ(๋ฐ์ธ๋ฉ)์ด ์ด๋ฃจ์ด์ง๋ ๊ฒ์ ๋์ ๊ฒฐํฉ์ด๋ผ๊ณ ํ๋ค. ๋์ ๋ฐ์ธ๋ฉ์ผ๋ก ์ธํด ํฌ์ธํฐ๊ฐ ์ด๋ค ํ์ ์ด๋ ์๊ด ์์ด ๊ทธ ์ค์ ๊ฐ์ฒด๋ฅผ ๊ธฐ์ค์ผ๋ก ํจ์๋ฅผ ํธ์ถํ๋ค. ์ด ๋์ ๋ฐ์ธ๋ฉ ๋๋ถ์ ๋คํ์ฑ์ด ์ ์ฉ๋๋ ๊ฒ!
๐์ค๋ฒ๋ผ์ด๋ฉ(Overriding)
์ฌ์ ์ ์๋ฏธ: '~์ ๋ฎ์ด์ฐ๋ค.', '~์ ์ฐ์ ํ๋ค.'
๋ถ๋ชจ ํด๋์ค๋ก๋ถํฐ ์์ ๋ฐ์ ๋ฉค๋ฒ ํจ์๋ฅผ ์์ ํด๋์ค์์ ์ฌ์ ์ํ์ฌ ์๋ก์ด, ๋๋ ์์ ๋ฐ ํ์ฅ๋ ๊ธฐ๋ฅ์ ๋ง๋๋ ๊ฒ. ํจ์์ ์๊ทธ๋์ฒ๊ฐ ๋์ผํด์ผํ๋ค. (์ด๋ฆ ๋์ผ, ๋งค๊ฐ๋ณ์์ ํ์ ๋ฐ ๊ฐ์ ๋์ผ, ๋ฐํ ํ์ ๋์ผ)
๋ถ๋ชจ ํด๋์ค์ ๊ฐ์ฒด ํฌ์ธํฐ๋ ์์ ํด๋์ค ๊ฐ์ฒด์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค. ์ฆ, ๋ถ๋ชจ ํ์ ์ผ๋ก ์ ์ธ๋ ํฌ์ธํฐ๋ก ์์ ํ์ ์ ๊ฐ์ฒด๋ฅผ ๋ค๋ฃฐ ์ ์๋ค.
๐๊ฐ์ ํจ์(Virtual Function)
๋คํ์ฑ์ ์ง์ํ๊ธฐ ์ํด ์์ ํด๋์ค์ ๊ฐ์ฒด๊ฐ ์ฌ์ ์ํ ํจ์๋ก ๋์ํ ์ ์๋๋ก ํ๋ ๊ฒ. C++์์๋ ํจ์๋ฅผ ๊ฐ์ํจ์๋ก ๋ฑ๋กํ์ง ์์ผ๋ฉด ์์ ๊ฐ์ฒด์์ ํจ์๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํ๋๋ผ๋, ๋ถ๋ชจ ํ์ ์ ๋ณ์๋ก ์์ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ์ ๋ ๋ถ๋ชจ ํ์ ์ ์ ์๋ ํจ์๋ก ๋์ํ๋ค.
๊ฐ์ ํจ์๊ฐ ํฌํจ๋ ํด๋์ค๋ ์ถ์ ํด๋์ค๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋ค.
์๋ฐ์์๋ ๋ชจ๋ ํจ์๊ฐ ๊ฐ์ํจ์๋ก ์ ์ธ๋์ด ์๊ธฐ ๋๋ฌธ์, ๋ถ๋ชจ ํ์ ์ ๋ณ์๋ก ์์ ํ์ ์ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋๋ผ๋ ์์ ๊ฐ์ฒด์์ ์ฌ์ ์๋ ํจ์๋ก ๋์ํ๋ค.
๊ฐ์ํจ์ ๋ฏธ์ฌ์ฉ
#include <iostream>
class Base {
public:
void display() { // ๋น๊ฐ์ ํจ์
std::cout << "Base display" << std::endl;
}
};
class Derived : public Base {
public:
void display() { // ํจ์ ์ฌ์ ์ (์ค๋ฒ๋ผ์ด๋ฉ์ด ์๋)
std::cout << "Derived display" << std::endl;
}
};
int main() {
Base* basePtr = new Derived(); // ๋ถ๋ชจ ํ์
ํฌ์ธํฐ๋ก ์์ ๊ฐ์ฒด ์ฐธ์กฐ
basePtr->display(); // Base์ display ํธ์ถ
delete basePtr;
return 0;
}
๊ฐ์ํจ์ ์ฌ์ฉ
#include <iostream>
class Base {
public:
virtual void display() {
std::cout << "Base display" << std::endl;
}
};
class Derived : public Base {
public:
void display() override { // ๊ฐ์ ํจ์ ์ฌ์ ์
std::cout << "Derived display" << std::endl;
}
};
int main() {
Base* basePtr = new Derived(); // ๋ถ๋ชจ ํ์
ํฌ์ธํฐ๋ก ์์ ๊ฐ์ฒด ์ฐธ์กฐ
basePtr->display(); // Derived์ display ํธ์ถ
delete basePtr;
return 0;
}
C#์์์ ๊ฐ์ํจ์
๊ฐ์ํจ์ ๋ฏธ์ฌ์ฉ
using System;
class Base
{
public void Display() // ๋น๊ฐ์ ๋ฉ์๋
{
Console.WriteLine("Base display");
}
}
class Derived : Base
{
public new void Display() // ๋ฉ์๋ ์จ๊ธฐ๊ธฐ
{
Console.WriteLine("Derived display");
}
}
class Program
{
static void Main()
{
Base baseRef = new Derived(); // ๋ถ๋ชจ ํ์
์ฐธ์กฐ๋ก ์์ ๊ฐ์ฒด ์ฐธ์กฐ
baseRef.Display(); // Base์ Display ํธ์ถ
}
}
๊ฐ์ํจ์ ์ฌ์ฉ
using System;
class Base
{
public virtual void Display() // ๊ฐ์ ๋ฉ์๋๋ก ๋ณ๊ฒฝ
{
Console.WriteLine("Base display");
}
}
class Derived : Base
{
public override void Display() // ์ฌ์ ์๋ ๊ฐ์ ๋ฉ์๋
{
Console.WriteLine("Derived display");
}
}
class Program
{
static void Main()
{
Base baseRef = new Derived(); // ๋ถ๋ชจ ํ์
์ฐธ์กฐ๋ก ์์ ๊ฐ์ฒด ์ฐธ์กฐ
baseRef.Display(); // Derived์ Display ํธ์ถ
}
}
๐ ์์ ์์
#include <iostream>
using namespace std;
class Employee {
public :
Employee() {
cout << "emp ๋ํดํธ ์์ฑ์ " << endl;
}
Employee(const char* name, const char* address) {
cout << "emp ์ธ์ ๋๊ฐ ์์ฑ์ " << endl;
this->name = new char[strlen(name) + 1];
this->address = new char[strlen(address) + 1];
strcpy_s(this->name, strlen(name) + 1, name);
strcpy_s(this->address, strlen(address) + 1, address);
}
~Employee() {
cout << "emp ์๋ฉธ์ ํธ์ถ" << endl;
delete[] name;
delete[] address;
}
char* getName() {
return this->name;
}
char* getAddress() {
return this->address;
}
void Print() {
cout << "์ด๋ฆ: " << this->getName() << ", ์ฃผ์: " << this-> getAddress() << endl;
}
virtual double PayCheck() const = 0;
protected :
char* name;
char* address;
};
class RegularEmployee : public Employee {
public :
RegularEmployee() {
cout << "reg ๋ํดํธ ์์ฑ์ " << endl;
}
RegularEmployee(const char* name, const char* address, double salary)
: Employee(name, address) {
cout << "reg ์ธ์ ์ธ๊ฐ ์์ฑ์ " << endl;
this->salary = salary;
}
~RegularEmployee() {
cout << "rag ์๋ฉธ์ ํธ์ถ" << endl;
}
double PayCheck() const override {
return salary * 12;
}
private :
double salary;
};
class TemporaryEmployee : public Employee {
public :
TemporaryEmployee() {
cout << "temp ๋ํดํธ ์์ฑ์ " << endl;
}
TemporaryEmployee(const char* name, const char* address, double dailyPayCheck, int days)
: Employee(name, address) {
cout << "temp ์ธ์ ์ธ๊ฐ ์์ฑ์ " << endl;
this->dailyPayCheck = dailyPayCheck;
this->days = days;
}
~TemporaryEmployee() {
cout << "temp ์๋ฉธ์ ํธ์ถ" << endl;
}
double PayCheck() const override {
return this->dailyPayCheck * days * 12;
}
private :
double dailyPayCheck;
int days;
};
class SalesMan : public RegularEmployee {
public :
SalesMan() {
cout << "sales ๋ํดํธ ์์ฑ์ " << endl;
}
SalesMan(const char* name, const char* address, double salary, double allowance)
: RegularEmployee(name, address, salary) {
cout << "sales ์ธ์ ๋ค๊ฐ ์์ฑ์ " << endl;
this->allowance = allowance;
}
~SalesMan() {
cout << "sales ์๋ฉธ์ ํธ์ถ" << endl;
}
double PayCheck() const override {
return RegularEmployee::PayCheck() + (allowance * 12);
}
private:
double allowance;
};
// Employee ๊ด๋ฆฌ ํด๋์ค
class Department {
public :
Department() {
this->count = 0;
}
~Department() {
}
// ๊ฐ์ฒด๋ฅผ ์ ๋ฌ ์ธ์๋ก ๋ฐ์ ๊ฐ์ฒด ๋ฐฐ์ด์ ๋ฃ์ด ๊ด๋ฆฌ
void Insert(Employee& emp) {
this->emp[count] = &emp;
count++;
}
// ํ์ฌ ๊ด๋ฆฌ ์ค์ธ ๊ฐ์ฒด์ ๊ธ์ฌ ์ถ๋ ฅ
void Print() const {
for (int i = 0; i < count; i++) {
cout <<"[" << this->emp[i]->getName() << "] ";
cout << this->emp[i]->PayCheck() << endl;
}
}
private:
Employee* emp[10];
int count;
};
void main() {
//Employee e("ํ์ฌ์", "ํ์ฌ์ ์ฃผ์");
//e.print();
RegularEmployee re("์ ๊ท์ง์", "์ ๊ท์ง์ ์ฃผ์", 550);
//cout << re.PayCheck() << endl;
TemporaryEmployee te("๊ณ์ฝ์ง", "๊ณ์ฝ์ง ์ฃผ์", 10, 20);
//cout << te.PayCheck() << endl;
SalesMan sm("jung", "๋ถ๋น", 300, 50);
//cout << sm.PayCheck() << endl;
/*Employee* salesMan = (Employee*) new SalesMan("์ ์์
", "๋ถ๋น", 500, 50);
cout << salesMan->PayCheck() << endl;*/
Department dpt;
dpt.Insert(re);
dpt.Insert(te);
dpt.Insert(sm);
dpt.Print();
}
๐์ฐธ๊ณ
[C++] ๋์ ๋ฐ์ธ๋ฉ, ์ ์ ๋ฐ์ธ๋ฉ
๋์ ์ ์ ๋ฐ์ธ๋ฉ ์ฌ์ ์ค๋น * ๋จผ์ ๋คํ์ฑ์ ๋ํ ๊ฐ๋ ์ ์๊ณ ์์ด์ผ ํ๋ค. https://licktwice.tistory.com/59https://licktwice.tistory.com/59 [JAVA]์๋ฐ - ๋คํ์ฑ Polymorphism ๋คํ์ฑ Polymorphism OOP์ 4๋ ํน์ฑ * ๋คํ
licktwice.tistory.com
'C, C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] STL (0) | 2024.09.05 |
---|---|
[C++] ํ ํ๋ฆฟ(Template) (0) | 2024.08.29 |
[C++] ๊ฐ์ฒด ์งํฅ (6) | 2024.08.19 |
[C] ๋์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น (0) | 2024.08.13 |
[C] ๊ตฌ์กฐ์ฒด, ๊ณต์ฉ์ฒด (0) | 2024.08.13 |