๐ [CH2/02] OOP Summary
๐ [Notion] OOP Summary
makeSound()๋ผ๋ ์์ ๊ฐ์ ํจ์๋ฅผ ํฌํจํฉ๋๋ค.Zoo ํด๋์ค๋ฅผ ์ ์ํด์ฃผ์ธ์!class Zoo {
private:
Animal* animals[10]; // ๋๋ฌผ ๊ฐ์ฒด๋ฅผ ์ ์ฅํ๋ ํฌ์ธํฐ ๋ฐฐ์ด
public:
// ๋๋ฌผ์ ๋๋ฌผ์์ ์ถ๊ฐํ๋ ํจ์
// - Animal ๊ฐ์ฒด์ ํฌ์ธํฐ๋ฅผ ๋ฐ์ ํฌ์ธํฐ ๋ฐฐ์ด์ ์ ์ฅํฉ๋๋ค.
// - ๊ฐ์ ๋๋ฌผ์ด๋ผ๋ ์ฌ๋ฌ ๋ฒ ์ถ๊ฐ๋ ์ ์์ต๋๋ค.
// - ์
๋ ฅ ๋งค๊ฐ๋ณ์: Animal* (์ถ๊ฐํ ๋๋ฌผ ๊ฐ์ฒด)
// - ๋ฐํ๊ฐ: ์์
void addAnimal(Animal* animal);
// ๋๋ฌผ์์ ์๋ ๋ชจ๋ ๋๋ฌผ์ ํ๋์ ์ํํ๋ ํจ์
// - ๋ชจ๋ ๋๋ฌผ ๊ฐ์ฒด์ ๋ํด ์์ฐจ์ ์ผ๋ก ์๋ฆฌ๋ฅผ ๋ด๊ณ ์์ง์ด๋ ๋์์ ์คํํฉ๋๋ค.
// - ์
๋ ฅ ๋งค๊ฐ๋ณ์: ์์
// - ๋ฐํ๊ฐ: ์์
void performActions();
// Zoo ์๋ฉธ์
// - Zoo ๊ฐ์ฒด๊ฐ ์๋ฉธ๋ ๋, ๋๋ฌผ ๋ฒกํฐ์ ์ ์ฅ๋ ๋ชจ๋ ๋๋ฌผ ๊ฐ์ฒด์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํด์ ํฉ๋๋ค.
// - ๋ฉ๋ชจ๋ฆฌ ๋์๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ๋์ ํ ๋น๋ Animal ๊ฐ์ฒด๋ฅผ `delete` ํฉ๋๋ค.
// - ์
๋ ฅ ๋งค๊ฐ๋ณ์: ์์
// - ๋ฐํ๊ฐ: ์์
~Zoo();
};
#include <cstdlib>
#include <ctime>
// ๋๋ค ๋๋ฌผ์ ์์ฑํ๋ ํจ์
// - 0, 1, 2 ์ค ํ๋์ ๋์๋ฅผ ์์ฑํ์ฌ ๊ฐ๊ฐ Dog, Cat, Cow ๊ฐ์ฒด ์ค ํ๋๋ฅผ ๋์ ์ผ๋ก ์์ฑํฉ๋๋ค.
// - ์์ฑ๋ ๊ฐ์ฒด๋ Animal ํ์
์ ํฌ์ธํฐ๋ก ๋ฐํ๋ฉ๋๋ค.
// - ์
๋ ฅ ๋งค๊ฐ๋ณ์: ์์
// - ๋ฐํ๊ฐ: Animal* (์์ฑ๋ ๋๋ฌผ ๊ฐ์ฒด์ ํฌ์ธํฐ)
Animal* createRandomAnimal();
#pragma once
class Animal
{
public:
virtual ~Animal()
{
}
virtual void makeSound() = 0; // ์์ ๊ฐ์ ํจ์
};
class Dog : public Animal
// private๋ก ํ๋ฉด Animal์์ ๋ค์ด์ฌ ๋ private์ฒ๋ผ ๋ค์ด์์ ์์ํด๋์์์ makeSound() ์ธ ์ ์์
{
void makeSound() override; // override๋ ์์จ๋ ๋๋๋ฐ ์คํ๋๋ฉด ์๋ ค์ฃผ๋๊น ์ฐ๋๊ฒ ์ข์
};
class Cat :public Animal
{
void makeSound() override;
};
class Cow : public Animal
{
void makeSound() override;
};
#include "Animal.h"
#include <iostream>
using namespace std;
void Dog::makeSound()
{
cout << "๋ฉ!๋ฉ!" << endl;
}
void Cat::makeSound()
{
cout << "์ผ์น~" << endl;
}
void Cow::makeSound()
{
cout << "์๋ฉ~" << endl;
}
#pragma once
#include "Animal.h"
class Zoo
{
public:
void addAnimal(Animal* animal);
void performActions();
~Zoo();
private:
Animal* animals[10]; // ๋๋ฌผ ๊ฐ์ฒด๋ฅผ ์ ์ฅํ๋ ํฌ์ธํฐ ๋ฐฐ์ด
int animalCount = 0;
};
#include "Zoo.h"
#include <iostream>
using namespace std;
void Zoo::addAnimal(Animal* animal)
{
animals[animalCount] = animal;
animalCount++;
}
void Zoo::performActions()
{
for (int i = 0; i < animalCount; i++)
{
animals[i]->makeSound();
}
}
Zoo::~Zoo() // ์๋ฉธ์๋ ๋ฐํ์๋ฃํ ์์จ๋ ๋จ
// Animal.h ์๋ ์๋ฉธ์ ๋ง๋ค์ด์ค์ผ ํจ
{
for (int i = 0; i < animalCount; i++)
{
delete animals[i];
}
}
#include "Animal.h"
#include "Zoo.h"
#include <cstdlib> // ๋๋ค ๋๋ฌผ์ ์์ฑ
#include <ctime> // ๋๋ค ๋๋ฌผ์ ์์ฑ
#include <iostream>
using namespace std;
Animal* createRandomAnimal()
{
int randInt = rand() % 3; // 0, 1, 2 ์ค ํ๋
if (randInt == 0)
{
return new Dog;
}
else if (randInt == 1)
{
return new Cat;
}
else
{
return new Cow;
}
}
int main()
{
srand(time(0));
const int SIZE = 3;
Animal* animals[SIZE] = {}; // nullptr๋ก ์ด๊ธฐํ
// ๊ฐ์์ง, ๊ณ ์์ด, ์ - ์คํ์ ์์ฑ
Dog myDog;
Cat myCat;
Cow myCow;
// ๋ฐฐ์ด์ ๋ฃ๊ธฐ
// ํ์
์ด ptr์ด๊ธฐ ๋๋ฌธ์ ์ฃผ์๊ฐ์ผ๋ก ๋ฐ์์ผ ํจ
/*
animals[0] = &myDog;
animals[1] = &myCat;
animals[2] = &myCow;
for (int i = 0; i < SIZE; i++)
{
animals[i]->makeSound(); //Animal.cpp์์ void๋ก ๋ง๋ค์์: return x -> cout์ผ๋ก ์ถ๋ ฅ ๋ถ๊ฐ๋ฅ
}
*/
// ๋๋ฌผ์ ๋ง๋ ๋ค
Zoo myZoo;
// ๋๋ฌผ ์ถ๊ฐ - ptr๋ก ์ค์ผํ๋ฏ๋ก ์ฃผ์๊ฐ ์ค๋ค
/*
myZoo.addAnimal(&myCow);
myZoo.addAnimal(&myDog);
myZoo.addAnimal(&myCat);
*/
// ๋๋ฌผ ๋๋คํ๊ฒ ์ถ๊ฐ
myZoo.addAnimal(createRandomAnimal());
myZoo.addAnimal(createRandomAnimal());
myZoo.addAnimal(createRandomAnimal());
myZoo.addAnimal(createRandomAnimal());
myZoo.addAnimal(createRandomAnimal());
myZoo.performActions();
return 0;
}