12.2 Classes and class members

주홍영·2022년 3월 15일
0

Learncpp.com

목록 보기
126/199

https://www.learncpp.com/cpp-tutorial/classes-and-class-members/

c++은 char, int, long과 같은 fundamental(근본적인) 데이터 타입을 지원하지만
문제가 복잡해지며 이는 충부하지 않다
그래서 c++은 user-define data type인 struct와 enum 타입에 대해서 배웟을 것이다

enum 과 struct 타입은 바로 strcut 타입이다
struct 타입은 variable만을 저장한다

Classes

oop에서는 우리는 종종 우리가 정의한 타입이 data를 hold하는 것만이 아닌
data를 활용한 function까지 지원하기를 바란다
c++에서는 이를 class가 실현시켜준다

struct DateStruct
{
    int year {};
    int month {};
    int day {};
};

class DateClass
{
public:
    int m_year {};
    int m_month {};
    int m_day {};
};

struct와 마찬가지로 class는 데이터의 형태에 대해서 정의할 뿐이지
저장공간을 차지하고 있지는 않다

Member Functions

class는 data를 가지고 있는 것 뿐만이 아니라 function도 가지고 있을 수 있다
class안에 정의된 함수를 member function이라고 한다 (간혹 method라고도 한다)

A note about structs in C++

sturct도 member function이 지원된다
하지만 우리는 data-only에는 struct를 data and function에는 class를 사용할 것을 권장

profile
청룡동거주민

0개의 댓글