3. Class
by JiwonDevC1. ๊ตฌํ๋ถ์ ์ ์ธ๋ถ
Cpp์์ ํด๋์ค๋ฅผ ๊ตฌํํ ๋์๋ ๊ตฌํ๋ถ(.cpp)์ ์ ์ธ๋ถ(.h)๋ฅผ ๋ค๋ฅธ ํ์ผ๋ก ์์ฑํ๋ค.
#include "Person.h"
int main()
{
Person p1;
p1.SetPerson("ํ๊ธธ๋",20,true);
p1.ShowPerson();
return 0;
}
/* Person.h */
//์ฌ๋ฌ๋ฒ include ๋๋๊ฑธ ๋ฐฉ์งํ๊ธฐ ์ํ ์ ์ฒ๋ฆฌ๋ฌธ์ด๋ค.
#ifndef PROJECT_PERSON_H // ๋ณดํต '์์ด_๋๋ฌธ์_H' ๋ก ์ด๋ฆ์ ์ง๋๋ค.
#define PROJECT_PERSON_H
class Person
{
private: // ์ ๊ทผ ์ ํ์(๋น๊ณต๊ฐ)
char* m_name;
int m_age = 0; // c++11 ๋ถํฐ๋ ๊ธฐ๋ณธ๊ฐ ์ค์ (์์ฑ์๊ฐ ์์ ๋)๋ ๊ฐ๋ฅํ๋ค.
bool m_gender;
public: //์ ๊ทผ ์ ํ์(๊ณต๊ฐ)
Person(); // ๋ํดํธ ์์ฑ์
~Person(); //์๋ฉธ์
void SetPerson(char* name, int age, bool gender);
//ํจ์๋ public ์ ์ธ ๊ฐ๋ฅ
void ShowPerson();
};
#endif // ifndef ์ข
๋ฃ
์ฐธ๊ณ ๋ก ๊ตฌํ๋ถ(.cpp)์์๋ ๊ผญ ํ ํด๋์ค๋ก ๋ด์ฉ์ ์ ์ ํ์๋ ์๋ค.
๋ฐ๋ก ๋๋ ์ Class:: ์ผ๋ก ๋ฉ์๋ ํ๋ํ๋ ๋ฐ๋ก ์ ์ด์ค๋ ๋๋ค.
/* Person.cpp */
#include "Person.h" // ์ ์ธ๋ถ๋ฅผ include ํ๋ค.
Person::Person(){
}
Person::~Person(){
// delete[] ๋ฐฐ์ด์ด๋ฆ
}
void Person::SetPerson(char* name, int age, bool gender){
this->m_name = name; // ์ฐธ๊ณ ๋ก ์ง์ญ๋ณ์์ ์ด๋ฆ์ด ๋ค๋ฅด๋ค๋ฉด this๋ฅผ ์๋ตํด๋ ๋๋ค.
this->m_age = age;
this->m_gender = gender;
}
void Person::ShowPerson(){
printf("%s, %d, ", this->m_name, this->m_age);
if(this->m_gender == true)
printf("๋จ์ฑ\n");
else
pring("์ฌ์ฑ\n);
}
2. ํด๋์ค ๊ฐ์ฒด ์์ฑ
Class c; ์ Class*c = new Class(); ์ ์ฐจ์ด
๋ค๋ฅธ ์ธ์ด๋ฅผ ํ๋ค์ค๋ฉด ํท๊ฐ๋ฆด ์๋ ์๋๋ฐ, Cpp์์๋ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋๋ฐ new ํค์๋๊ฐ ํ์์๋ค.
์ผ๋ฐ ์๋ฃํ์ฒ๋ผ ์ธ์คํด์ค๋ฅผ ์์ฑํด์ฃผ๋ฉด ๋๋ค. ์ด๋ ์ผ๋ฐ ์๋ฃํ์ฒ๋ผ ์คํ ๋ฉ๋ชจ๋ฆฌ์ ํ ๋น๋๋ค.
์ผ๋ฐ ์๋ฃํ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ณ์์ ์์ฑ๋ฒ์(Scope)๋ฅผ ๋ฒ์ด๋๊ฒ๋๋ฉด ์๋์ผ๋ก ๋ฉ๋ชจ๋ฆฌ์์ ํ ๋น ํด์ ๋๋ค.
MyClass name; // ํ ๋น์๋ฃ.
name.method() // ๋ฐ๋ก ์ฌ์ฉ
๊ทธ๋ผ์๋ ๋ถ๊ตฌํ๊ณ new ํค์๋๊ฐ ์กด์ฌํ๋ ์ด์ ๋, ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ ๋์ ํ ๋น์ ์ฌ์ฉ๋๋ค.
๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋์ ํ ๋น ํ ๊ฒฝ์ฐ ์๋ฐ์ฒ๋ผ ํ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ๋๊ณ new ์ฐ์ฐ์์ ์ํด ์์ฑ์๊ฐ ์๋์ผ๋ก ํธ์ถ๋๋ค.
์ด์ฐ๋ณด๋ฉด ๋น์ฐํ ๊ธฐ๋ฅ์ธ ๊ฒ ๊ฐ์ง๋ง, ๊ธฐ์กด์ malloc์ ์ฌ์ฉํ๋ C์ธ์ด์์๋ ํด๋์ค๊ฐ ์์ผ๋ ์ด๋ฐ ๊ธฐ๋ฅ๋ ์์๋ค.
Student *kim;
kim = new Student();
kim->showData(); // ํฌ์ธํฐ๋ฅผ ์ด์ฉํด ํด๋์ค ์ ๊ทผ (->)
์ฌ๊ธฐ์ ๊ฐ๊ฐ์ CPP์ ์ฅ์ . ์ฐ๋ฆฌ๊ฐ ์ง์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ด๋ฆฌํ๋ ์ธ์ด๋ผ๋ ๊ฑธ ์๊ธฐ์์ผ์ค๋ค.
ํ ๋น๋ ํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํด์ (delete, free) ์์ผ์ฃผ์ง ์์ผ๋ฉด ๋ฉ๋ชจ๋ฆฌ๋ ๊ณ์ํด์ ๋จ์์๊ฒ๋๋ค.
๋ฌผ๋ก ์ต๊ทผ์ GCC์๋ ๊ฐ๋น์ง ์ปฌ๋ ํฐ๊ฐ ์ค์น๋์ด์์ด์ ์ด๋์ ๋๋ ํด๊ฒฐํด์ฃผ๊ธดํ์ง๋ง ํ์ค์ผ๋ก ์ ๊ณตํด์ฃผ๋ ์ธ์ด์ ๊ธฐ๋ฅ์ด ์๋๋ฏ๋ก ๋ฉ๋ชจ๋ฆฌ ๋์๋ฐฉ์ง๋ฅผ ์ํด ์ง์ ๊ด๋ฆฌํด์ฃผ๋ ๊ฒ์ด ์ข๋ค.
3. ์์ฑ์ ์ค๋ฒ๋ผ์ด๋ฉ
class Rectangle {
private: // ๋ช
์ํ์ง์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ์ private์
๋๋ค.
int length;
int breadth;
public:
Rectangle() { // ์์ฑ์
length = 0;
breadth = 0;
}
Rectangle(int l, int b) { // ์์ฑ์ ์ค๋ฒ๋ผ์ด๋ฉ
length = l;
breadth = b;
}
~Rectangle(){ // ์๋ฉธ์
cout << "Destructor";
}
};
3-1 ์์ฑ์ ์์
๊ธฐ์กด์ ์์ฑ์๋ฅผ ์ฌ์ฌ์ฉ ํ๊ณ ์ถ๋ค๋ฉด, new_Init { old_Init() } ์ฒ๋ผ ์ง์ ํธ์ถํ์ง๋ง๊ณ ์๋์ ๊ฐ์ด ์ฌ์ฉํ์.
Class_init() {
// Code A
}
Class_init(int value) : Class_init() {// ๋ค๋ฅธ ์์ฑ์๋ฅผ ์์ํ๋ค.
// Code B
// ์ฌ๊ธฐ์ Class_init()๋ฅผ ์ง์ ํธ์ถํ๋ ๊ฑด ๊ถ์ฅํ๋ ๋ฐฉ๋ฒ์ด ์๋๋ค.
}
4. ์๋ฉธ์
๊ฐ์ฒด๊ฐ ์๋ฉธ๋ ๋ ์๋์ผ๋ก ์คํ๋๋ ํด๋์ค์ ๋ฉค๋ฒ ํจ์์ด๋ค.
๊ธฐ๋ณธ ์๋ฃํ ๊ฐ์ด๋ฉด ํฌ๊ฒ ํ์ ์์ง๋ง, ๋์ ๋ฉ๋ชจ๋ฆฌ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ฑ ๋ฆฌ์์ค๋ฅผ ๊ด๋ฆฌํ๊ธฐ์ ํธํ๋ค.
์ฐธ๊ณ ๋ก C์ธ์ด์์๋ free() ๋ฅผ ์ฌ์ฉํ๋ค๋ฉด, CPP์์๋ delete ๋ผ๋ ํค์๋๊ฐ ์ ๊ณต๋๋ค.
delete๋ฅผ ์ ์ธํ๊ฒ๋๋ฉด ํด๋น ํด๋์ค๋ ์๋ฃํ์ ์๋ฉธ์๊ฐ ํธ์ถ๋๋ค.
class MyArray {
private:
int* Array;
int Length;
public:
MyArray(int length) { // ์์ฑ์
this->Array = new int[static_cast<size_t>(length)]{};
this->Length = length;
}
~MyArray(){ // ์๋ฉธ์
delete[] Array;
// ๋ฉ๋ชจ๋ฆฌ๊ฐ ๊ทธ๋ฃน ํ์(๋ฆฌ์คํธ)์ด๋ผ๋ฉด delete[] ํค์๋๋ฅผ ์ฌ์ฉํ์.
}
};
5. ํด๋์ค ๋ฉค๋ฒ ์ด๊ธฐํ
CPP์์๋ ํด๋์ค ์ด๊ธฐํ๋ฅผ ํ ๋๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์๋ผ๋ ๋ฐฉ๋ฒ์ด ์กด์ฌํ๋ค.
์ฐธ๊ณ ๋ก int x = 5; ๋ฅผ int x { 5 }; ๋ก ์ด๊ธฐํ ํด๋ ๋๋ค. ์ด๋ ์ฌ๋ฌ ๋ฉค๋ฒ๋ฅผ ๋์์ ์ด๊ธฐํ ํ ๋ ์ฌ์ฉํ๋ค.
#include <iostream>
class Rectangle {
private:
double m_Length = 1.0; // ๋๋ m_Length { 1.0 };
double m_Width = 1.0;
public:
Rectangle(double length, double width) : m_Length(length), m_Width(width){
// ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํ์ง ์๋ ์ด๊ธฐํ. + ์ด๋ ๊ฒํ๋ฉด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ ์๋ ์ ์๋ค.
}
Rectangle(double length) : m_Length(length){
// m_Length ๋ณ์๋ ๊ฐ์ด ์ด๊ธฐํ๋๋ค. // m_Width ๋ณ์๋ ๊ธฐ๋ณธ๊ฐ 1.0
}
};
int main() {
int x = {3};
Rectangle y{ 2.0, 3.0}; // length = 2 , width = 3
Rectangle z{2.0}; // length =2 , width = ๊ธฐ๋ณธ๊ฐ (๊ธฐ๋ณธ๊ฐ์ ์์ ์๋ค๋ฉด ์ฐ๋ ๊ธฐ๊ฐ)
return 0;
}
6. Class Chaining
ํด๋์ค์ this ํฌ์ธํฐ๋ฅผ ์์ฉํ๋ฉด Number.Add(5).Add(5).Mul(6) ์ด๋ฐ์์ ๊ตฌํ๋ ๊ฐ๋ฅํ๋ค.
class Calc {
private:
int m_Value = 0;
public:
Calc& Add(int value) {
m_Value += value; return *this;
// ํท๊ฐ๋ฆฌ์ง๋ง์. this ์์ฒด๋ 4๋ฐ์ดํธ ํฌ์ธํฐ ์ฃผ์๊ฐ์ด๋ค.
// Calc ํด๋์ค๋ฅผ ๋ฐํํ ๋ ค๋ฉด ๊ฐ(*this)์ ๋ฐํํด์ผ ํ๋ค
}
Calc& Sub(int value) {
m_Value -= value; return *this;
}
Calc& Mul(int value) {
m_Value *= value; return *this;
}
int GetValue() { return m_Value; }
}; // ์ธ๋ฏธ์ฝ๋ก ๊น๋จน์ง ๋ง์.
int main() {
Calc calc;
calc.Add(5).Sub(3).Mul(4);
cout << calc.GetValue() << endl; // (+5-3)*4 = 8
return 0;
}
7. Template Method
CPP์์ ๋ค์ํ ์๋ฃํ์ ๋ํ ์ค๋ฒ๋ก๋ฉ์ ๊ตฌํํ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด๋ค.
์๋์ ๊ฐ์ด ์ค๋ฒ๋ก๋ฉ์ ๊ตฌํํ๋ค๊ณ ์ณค์ ๋
int sum(int a, int b){
return a + b;
}
double sum(double a, double b){
return a + b;
}
ํ ํ๋ฆฟ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ด๋ ๊ฒ ๋ง๋ค ์ ์๋ค.
// typename T ๊ณผ class T ์ ๊ณต //
template <typename T>
T sum(T a, T b){ // sum<int>(a,b) ์ฒ๋ผ ์ฌ์ฉ
return a + b;
}
template <class T1, class T2>
void printAll(T1 a, T2 b){
// printAll(m1,m2) ์ฒ๋ผ ์ฌ์ฉ
// ํด๋์ค์ ๊ฒฝ์ฐ ์ปดํ์ผ๋ฌ๊ฐ ์๋ฃํ์ ํ๋จ ๊ฐ๋ฅํ๊ธฐ์ ์๋ต๊ฐ๋ฅ.
cout << "T1 : " << a << endl;
cout << "T2 : " << b << endl;
cout << "T1 + T2 : " << a + b << endl;
}
7-1 ๋ฉ์๋๊ฐ ์๋ฃํ์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ๋์ ํ๋๊ฒฝ์ฐ
ํด๋น ํจ์์ ํ ํ๋ฆฟ ์ํ์ ์ ์ธ ํ ํ, ๋ฐ์ ๊ตฌํ๋ถ๋ฅผ template<> ๋ก ๋ฐ๋ก ์ ์ด์ฃผ๋ฉด ๋๋ค.
template <class T> // ์ํ
T sum(T a, T b){
return a + b;
}
template<>
char * sum<char*> (char* s1, char* s2){ // charํ
char * str= "[char *]๋ฌธ์์ด์ ๋ํ ์ ์์ต๋๋ค.";
cout << "s1 : " << s1 << endl;
cout << "s2 : " << s2 << endl;
return str;
}
template<>
const char * sum<const char*> (const char * s1,const char *s2){ // const char ํ
char * str= "[const char *]๋ฌธ์์ด์ ๋ํ ์ ์์ต๋๋ค.";
cout << "const s1 : " << s1 << endl;
cout << "const s2 : " << s2 << endl;
return str;
}
template<>
string sum<string>(string s1, string s2){ // string ํ
string str = "[string]๋ฌธ์์ด์ ๋ํ ์ ์์ต๋๋ค.";
cout << "string s1 : " << s1 << endl;
cout << "string s2 : " << s2 << endl;
return str;
}
7-2 ํด๋์ค์์ ํ ํ๋ฆฟ ๋ฉ์๋ ์ฌ์ฉ
๋ด๋ถ์์ ์ฌ์ฉํ ๋๋ ๋๊ฐ์ด ์ฌ์ฉํ๋ฉด ๋๊ณ , ์ธ๋ถ์ ๋ฉ์๋๋ณ๋ก ๋ฐ๋ก ๊ตฌํํ ๋ ๊ฐ๊ฐ ์ ์ด์ค์ผ ํ๋ค.
template <class T> // ํด๋์ค ๋ด๋ถ์์ ํ
ํ๋ฆฟ ์ฌ์ฉ
class Person{
private:
string name;
T height;
public:
Person(string name, T height):name(name), height(height){}
void printAll(){
cout << "name : " << name << endl;
cout << "number : " << height << endl;
};
void setName(string name){
this->name = name;
}
void setNumber(T height){
this->height = height;
}
};
//ํด๋์ค ์ธ๋ถ์์ ํด๋น ๋ฉ์๋๋ฅผ ๊ตฌํํ ๋ (ํค๋ํ์ผ ๋ถ๋ฆฌ)
template <class T>
class Person{
private:
string name;
T height;
public:
Person(string name, T height);
void printAll();
void setName(string name);
void setNumber(T height);
}; // ์ ์ธ๋ถ ๋ (.h)
//๊ตฌํ๋ถ ์์ (.cpp)
template <class T> // :: ์ฒ๋ผ, ๊ฐ ๋ฉ์๋๋ง๋ค ์๋กญ๊ฒ ์ ์ด์ฃผ์ด์ผ ํจ.
Person<T>::Person(string name, T height):name(name), height(height){}
template <class T>
void Person<T>::printAll(){
cout << "name : " << name << endl;
cout << "number : " << height << endl;
};
// ... ์ดํ ์๋ต
'๐ฑBackend > CPP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
4. STL (0) | 2020.12.29 |
---|---|
3. ์์ธ์ฒ๋ฆฌ (0) | 2020.12.29 |
2. Cpp์ ๋ ํนํ ์ฌ์ฉ๋ฒ (0) | 2020.12.26 |
1. Hello World (0) | 2020.12.26 |
๋ธ๋ก๊ทธ์ ์ ๋ณด
JiwonDev
JiwonDev