728x90
- C++ ์ฃผ์ ๊ธฐ๋ฅ ( ์ถ์ํ, ์ ๋ณด ์๋, ์บก์ํ)
#include <iostream>
using namespace std;
// C++ ์ฃผ์ ๊ธฐ๋ฅ
// 1. ์ถ์ํ(abstracton)
// : ์ฌ๋ฌผ์ ์ฌ์ค์ ์ผ๋ก ํํํ๋ ๊ฐ๋
(๊ตฌ์ํ)๊ฐ ์๋
// ๋ถํ์ํ ๋ถ๋ถ์ ์ ๊ฑฐํ๊ณ ๊ณตํต๋ ํน์ง๋ง์ ์ถ์ถํ๋ ๊ฒ
// ex) ์ค๋งํธํฐ -> User : ๋ด๋ถ ์ค๊ณ ๋ชจ๋ฅด์ง๋ง, ์ ์ฌ์ฉํจ (์ธ๋ถ ์ธํฐํ์ด์ค - ๋ด๋ถ ๊ตฌํ)
// 2. ๋ฐ์ดํฐ ์๋(Data hiding)
// : ๊ฐ์ฒด์ ์์ฑ์ ์จ๊ธฐ๊ณ ๊ณต๊ฐ๋ ์ธํฐํ์ด์ค๋ง์ ํตํด์ ์์ ํ ์ ์๋๋ก ํจ
// 3. ์บก์ํ(Capsulation)
// : ๊ฐ์ฒด์ ์์ฑ๊ณผ ๊ธฐ๋ฅ์ ํ๋๋ก ๋ฌถ๊ณ , ์ธ๋ถ ๊ตฌํ์ ํด๋์ค ์์ผ๋ก ์จ๊ธฐ๋ ๊ฒ
// ์ ๋์ด ์๋ ํด๋์ค๋ ์ฌํ์ฉ๋๊ฐ ๋งค์ฐ ๋๋ค
// ์๋๋ ์บก์ํ๊ฐ ์๋ชป๋ ์์ ์ด๋ค.
class File {
public:
void load()
{
cout << "mp3 ํ์ผ์ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ฆฐ๋ค." << endl;
}
};
class PowerDevice {
public:
void PowerUp()
{
cout << "mp3 play๋ฅผ ์ํ ํ์๋ฅผ ์ฌ๋ฆฐ๋ค." << endl;
}
};
class Memory {
public:
void expand()
{
cout << "mp3 play๋ฅผ ์ํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋๋ฆฝ๋๋ค." << endl;
}
};
class Mp3 {
public:
void play()
{
cout << "mp3๋ฅผ playํฉ๋๋ค" << endl;
}
};
int main()
{
PowerDevice pd;
Memory m;
File f;
Mp3 mp;
pd.PowerUp();
m.expand();
f.load();
mp.play();
}
// User๋ mp3๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ์ฌ์ ๊ธฐ๋ฅ๋ง์ ์ฌ์ฉํ๋ฉด ๋๋ค.
// mp3์ ์ธ๋ถ๊ตฌํ์ ์จ๊ธฐ๋๋ก ํ๋ ๊ฒ์ด ์บก์ํ ์๋์ ์์ ์ฐธ๊ณ ํ์
#include <iostream>
using namespace std;
// mp3์ play๊ธฐ๋ฅ๋ง์ ์ฌ์ฉํจ์ผ๋ก
// ์ด mp3 ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ ์ ์ ๋ ์ธ๋ถ ๊ตฌํ์ ๋ํด ๋ชฐ๋ผ๋ ๋จ
class File {
public:
void load()
{
cout << "mp3 ํ์ผ์ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ฆฐ๋ค." << endl;
}
};
class PowerDevice {
public:
void PowerUp()
{
cout << "mp3 play๋ฅผ ์ํ ํ์๋ฅผ ์ฌ๋ฆฐ๋ค." << endl;
}
};
class Memory {
public:
void expand()
{
cout << "mp3 play๋ฅผ ์ํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋๋ฆฝ๋๋ค." << endl;
}
};
class Mp3 {
private:
File f;
PowerDevice pd;
Memory m;
public:
void play()
{
pd.PowerUp();
m.expand();
f.load();
cout << "mp3 play" << endl;
}
};
int main()
{
Mp3 mp;
mp.play();
}
- ์์ฑ์
#include <iostream>
using namespace std;
#define NAME_LEN 20
#define SEX_LEN 10
#define JOB_LEN 20
#define CHARACTER_LEN 20
class Person {
private:
char name[NAME_LEN];
char sex[SEX_LEN];
char job[JOB_LEN];
char character[CHARACTER_LEN];
int age;
bool marriageStatus;
public:
// ๋ํดํธ ์์ฑ์
Person()
{
cout << "๋ํดํธ ์์ฑ์ : Person::Person()" << endl;
// ๋ณ์ ์ด๊ธฐํ
}
// ์ ๋ฒ ์๊ฐ์ ํ๋ constructor์ ๊ฐ์ ๊ธฐ๋ฅ์ ์ํํ๋ ์์ฑ์
// ๋ฉค๋ฒ ์ด๊ธฐํ๋ฅผ ์ด์ฉํ ์์ฑ์
Person(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* dest);
void write();
void read();
};
int main()
{
// Person player1, player2;
//player1.constructor("์์ฌ", "์ฌ์ฑ", "์ฝ๋ฉ์ฒ์ฌ", "์จ์", 21, true);
// player2.constructor("์ข
๋ช
", "๋จ์ฑ", "์์์", "์ฑ์ค", 23, false);
Person player1("์์ฌ", "์ฌ์ฑ", "์ฝ๋ฉ์ฒ์ฌ", "์จ์", 21, true);
Person player2("์ข
๋ช
", "๋จ์ฑ", "์์์", "์ฑ์ค", 23, false);
player1.introduce();
player2.introduce();
player1.eat("๋ง๋ผํ");
}
Person::Person(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;
cout << "์์ฑ์ : Person::Person(~)" << endl;
}
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* dest)
{
cout << name << "์(๋) " << dest << "์ผ๋ก ์ด์ ํ๋ค" << endl;
}
void Person::write()
{
cout << name << "์(๋) ์ฑ
์ ์ด๋ค" << endl;
}
void Person::read()
{
cout << name << "์(๋) ์ฑ
์ ์ฝ๋๋ค" << endl;
}
// 01) ์์ฑ์ ์ ์ธ
// - ํด๋น ํด๋์ค์ ์ด๋ฆ๊ณผ ๊ฐ์์ผ ํ๋ฉฐ, ๋ฐํํ์ด ์์ด์ผ ํ๋ค
// 02) ์์ฑ์ ํธ์ถํ๋ ๊ณณ์ ์ด๋์ธ๊ฐ?
// - ๊ฐ์ฒด ์์ฑ์ ์ํ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ ํ ๋น๊ณผ ๋์์ ์์ฑ์๊ฐ ํธ์ถ๋๋ค.
// - ํ์ฌ์ ์ฝ๋์์ ๊ฐ์ฒด๋ฅผ ์์ฑ๊ณผ ๋์์ ์ด๊ธฐํ ํ๊ณ ์๋ค.
// 03) ๊ฐ์ ์ด๋ฆ์ผ๋ก ์ฌ๋ฌ ๊ฐ ๋ง๋ค ์ ์์ผ๋ฉฐ (ํจ์ ์ค๋ฒ๋ก๋ฉ)
// 04) ์๋ต ๊ฐ๋ฅํ๋ค (๋ํดํธ ์์ฑ์ ์๋ ์์ฑ)
// -> ๋ํดํธ ์์ฑ์(๊ธฐ๋ณธ ์์ฑ์)
// ์ฐ๋ฆฌ๊ฐ ์์ฑํ๋ ์ฝ๋์์ ์์ฑ์๋ฅผ ์ง์ฐ๋ฉด? -> ๋์๊ฐ๊ธด ํ๋ ์ฐ๋ ๊ธฐ ๊ฐ.
- ์๋ฉธ์
#include <iostream>
using namespace std;
// ์๋ฉธ์(deconstructor)
// ๊ฐ์ฒด๊ฐ ์๋ฉธํ ๋ ์๋ ํธ์ถ
// ๋์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํด์ ํ๋ ์ฉ๋๋ก ์ฌ์ฉ
// ์๋ต ๊ฐ๋ฅ
// ๋์ ๋ฉ๋ชจ๋ฆฌ
// c์ธ์ด - malloc(), free(0
// c++ - new, delete
// strcpy_s
// - ์ฒซ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ ๋ฉ๋ชจ๋ฆฌ์
// ๋ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ ๊ธธ์ด๋งํผ
// ์ธ ๋ฒ์งธ ๋งค๊ฐ๋ณ์์ ๋ด์ฉ์ ๋ณต์ฌ
class Person {
private:
char* name;
public:
Person(const char* name) //์์ฑ์
{
this->name = new char[strlen(name) + 1]; // ๋์ ํ ๋น
strcpy_s(this->name, strlen(name) + 1, name);
cout << "Person::Person(name) ์์ฑ์ ์๋ฃ" << endl;
}
Person()
{
cout << "Person::Person() ์์ฑ์ ์๋ฃ" << endl;
}
~Person() // ์๋ฉธ์
{
delete[] name;
cout << "Person::~Person ์๋ฉธ์ ์๋ฃ" << endl;
}
void introduce();
};
int main()
{
Person person("๋ฏธ์");
person.introduce();
}
void Person::introduce()
{
cout << "์ด๋ฆ: " << name << endl;
}
- ๋ณต์ฌ ์์ฑ์
#include <iostream>
using namespace std;
// ๋ณต์ฌ ์์ฑ์ (๋ณต์ฌํ ๋ ํธ์ถ๋๋ ์์ฑ์)
// - ๋จผ์ ์์์ผ ๊ฐ์ฒด๋ฅผ ์ด์ฉํด์ ์๋ก์ด ๊ฐ์ฒด ์ด๊ธฐํํ๋ ๊ฒฝ์ฐ
// - ๋จผ์ ์์ฑํ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ์ ๋ฌํ๋ ๊ฒฝ์ฐ
// - ํจ์ ๋ด์์ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ ๊ฒฝ์ฐ
// ์๋์ ์ฝ๋์์ ์๋ฉธ์ 2๋ฒ ํธ์ถ -> ์ด์
class Person {
private:
int age;
int num;
public:
Person(int age) : age(age) //์์ฑ์
{
cout << "Person::Person(age) ์์ฑ์ ์๋ฃ" << endl;
}
Person()
{
cout << "Person::Person() ์์ฑ์ ์๋ฃ" << endl;
}
~Person() // ์๋ฉธ์
{
cout << "Person::~Person ์๋ฉธ์ ์๋ฃ" << endl;
}
void introduce();
};
int main()
{
Person person1(23);
Person person2(person1); // ๋ณต์ฌ์์ฑ์ ํธ์ถ
// person2 = person1;
person1.introduce();
person2.introduce();
}
void Person::introduce()
{
cout << "๋์ด : " << age << endl;
}
#include <iostream>
using namespace std;
// ๋ํดํธ ๋ณต์ฌ ์์ฑ์ -> ๋ฌธ์ ์กด์ฌ
// ์์ ๋ณต์ฌ vs ๊น์ ๋ณต์ฌ
// ๋ํดํธ ๋ณต์ฌ ์์ฑ์๋ ๋ฉค๋ฒ ๋ณ์์ ๊ฐ์ ๋ณต์ฌํ๊ธฐ์,
// ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๊ฐ์ ๋ณต์ฌํ๊ฒ ๋จ -> ๋ฌธ์ ๋ฐ์
// ์๋์ ์ฝ๋ ๋ฐํ์ ์ค๋ฅ ๋ฐ์
// - ๋ณต์ฌ ์์ฑ์ ํธ์ถ์ ์ํด ๋ฉค๋ฒ ๋ณ์ ๊ฐ ๋ณต์ฌ์ธ ์์ ๋ณต์ฌ๊ฐ ์งํ
// - person1 ๊ฐ์ฒด์ name ํฌ์ธํฐ์ ๊ฐ์ธ '์ฃผ์1'์ด person์ name ํฌ์ธํฐ ๊ฐ์ผ๋ก ๋ณต์ฌ
// - ์ฆ ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๊ฐ๋ฅดํค๊ฒ ๋จ
// - ์๋ฉธ์๊ฐ ํธ์ถ๋จ์ ๋ฐ๋ผ (์๋ฉธ์๋ ์ญ์์ผ๋ก ํธ์ถ๋จ) person2 ๊ฐ์ฒด ์๋ฉธ
// - ์ด์ด person1์ ์๋ฉธ์ ํธ์ถ๋๋ ค๋ ์ฐฐ๋ delete๋์ด์ผ ํ๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ด ์์ด ์ค๋ฅ
// => ๊น์ ๋ณต์ฌ์ ํ์
// -> ๋ณต์ฌ ์์ฑ์์ ์ฌ์ ์ ํ์
// -> ํฌ์ธํฐ์ ๊ฐ๋ง์ ๋ณต์ฌํ๋๊ฒ ์๋,
// ํฌ์ธํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๋ฉ๋ชจ๋ฆฌ์ ๋ด์ฉ๊น์ง ๋ณต์ฌํด์ผ ํจ
// => ์ฃผ์ ํ๊ณ ์คํ
class Person {
private:
char* name;
public:
Person(const char* name) //์์ฑ์
{
this->name = new char[strlen(name) + 1];
strcpy_s(this->name, strlen(name) + 1, name);
cout << "Person::Person(name) ์์ฑ์ ์๋ฃ" << endl;
}
Person()
{
cout << "Person::Person() ์์ฑ์ ์๋ฃ" << endl;
}
~Person() // ์๋ฉธ์
{
delete[] name;
cout << "Person::~Person ์๋ฉธ์ ์๋ฃ" << endl;
}
/*
Person(const Person& p)
{
this->name = new char[strlen(p.name) + 1]; // nameํฌ์ธํฐ ๊ฐ์ ์ํ ๋ฉ๋ชจ๋ฆฌ ์๋ก ํ ๋น
strcpy_s(this->name, strlen(p.name) + 1, p.name); // ๋ฉ๋ชจ๋ฆฌ์ ๋ณต์ฌ
cout << "Person::Person(cosnt Person&) ๋ณต์ฌ์์ฑ์ ์๋ฃ" << endl;
}
*/
void introduce();
};
int main()
{
Person person1("์ฃผ๋
ธ");
Person person2(person1); // ๋ณต์ฌ์์ฑ์ ํธ์ถ
// person2 = person1;
person1.introduce();
person2.introduce();
}
void Person::introduce()
{
cout << "์ด๋ฆ : " << name << endl;
}
'๐ค Study > C++ & C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
05. ์์๊ณผ ํฌํจ (0) | 2024.04.11 |
---|---|
04. ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ / ์ด๋ ์์ฑ์&์ด๋ ๋์ ์ฐ์ฐ์ / ๋์ ๋ฉ๋ชจ๋ฆฌ (1) | 2024.03.27 |
02. ์ฐ์ฐ์ / ํจ์ / ํด๋์ค (0) | 2024.03.13 |
01. ๊ฐ์ฒด์ ๊ธฐ๋ณธ ๋ฌธ๋ฒ (0) | 2024.03.12 |