4-1
객체
변수들과 참고 자료들로 이루어진 소프트웨어 덩어리
추상화
객체가 현실 세계에 존재하는 것들을 나타나기 위해 필요한 과정
캡슐화
인스턴스 메소드를 통해 인스턴스 변수들을 간접적으로 조절하는 것
예시
class Animal {
private: // 외부에서 접근 불가능한 것들 (생략 가능)
int food;
int weight;
public: // 외부에서 사용가능한 것들
void set_animal(int _food, int _weight){
food = _food;
weight = _weight;
}
void increase_food(int inc){
food += inc;
weight += (inc / 3);
}
void view_stat(){
std::cout << "이 동물의 food : " << food << std::endl;
std::cout << "이 동물의 weight : " << weight << std::endl;
}
}; // 세미콜론!
4-1문제 풀이
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class Date{ | |
int year_; | |
int month_; // 1부터 12까지 | |
int day_; // 1부터 31까지 | |
public: | |
void SetDate(int year, int month, int day); | |
void AddDay(int inc); | |
void AddMonth(int inc); | |
void AddYear(int inc); | |
void ShowDate(); | |
int GetCurrentMonthTotalDays(int year, int month); | |
Date(){ | |
year_ = 2020; | |
month_ = 1; | |
day_ = 1; | |
} | |
Date(int year, int month, int day){ | |
year_ = year; | |
month_ = month; | |
day_ = day; | |
} | |
}; | |
int Date::GetCurrentMonthTotalDays(int year, int month){ | |
static int month_day[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; | |
if(month != 2) return month_day[month-1]; | |
else if(year % 4 == 0 && year % 100 != 0) return 29; | |
else return 28; | |
} | |
void Date::SetDate(int year, int month, int day){ | |
this->year_ = year; | |
this->month_ = month; | |
this->day_ =day; | |
} | |
void Date::AddDay(int inc){ | |
while(true){ | |
int total_days = GetCurrentMonthTotalDays(year_, month_); | |
if(day_ + inc <= total_days){ | |
day_ += inc; | |
return; | |
}else{ | |
// 다음달로 넘어감 | |
inc -= (total_days - day_ + 1); | |
day_ =1; | |
AddMonth(1); | |
} | |
} | |
} | |
void Date::AddMonth(int inc){ | |
AddYear((inc + month_ -1) / 12); | |
month_ += inc % 12; | |
month_ = (month_ == 12) ? 12 : month_ % 12; | |
} | |
void Date::AddYear(int inc){ | |
year_ += inc; | |
} | |
void Date::ShowDate(){ | |
std::cout << "오늘은 "<<year_<<"년 "<<month_<<"월 "<<day_ <<"일 입니다. "<<std::endl; | |
} | |
int main(){ | |
// default constructor | |
Date day; | |
day.SetDate(2011,3,1); | |
day.ShowDate(); | |
day.AddDay(30); | |
day.ShowDate(); | |
day.AddDay(2000); | |
day.ShowDate(); | |
day.SetDate(2012,1,31); | |
day.AddDay(29); | |
day.ShowDate(); | |
day.SetDate(2012, 8, 4); | |
day.AddDay(2500); | |
day.ShowDate(); | |
//constructor | |
Date day2(2021, 3, 1); // 암시적 방법 | |
Date day3 = Date(2021, 4, 1); // 명시적 방법 | |
day2.ShowDate(); | |
day2.AddYear(10); | |
day2.ShowDate(); | |
return 0; | |
} |
4-2
함수 오버로딩
함수의 이름이 같더라도 인자가 다르면 다른 함수라고 판단
생성자
- 객체 생성 시 자동으로 호출되는 함수
- 객체 안 변수들을 초기화시켜줌
- 객체 초기화 역할이기 때문에 반환 값이 없음
디폴트 생성자
클래스 내에 개발자가 어떠한 생성자도 명시적으로 정의하지 않았을 경우 컴파일러가 자동으로 추가해주는 생성자
class Test {
int a;
public:
Test(){ // 디폴트 생성자
a = 1;
}
Test(int a_){
a = a_;
}
}
int main()
{
Test t1();
Test t2 = Test();
Test t3(3); // 암시적 방법 (implicit)
Test t4 = Test(4); // 명시적 방법 (explicit)
}
C++ 11부터 명시적으로 디폴트 생성자를 사용하도록 명시할 수 있음
class Test1 {
public:
Test1() = default; // 디폴트 생성자 정의해라
}
4-2 문제 풀이
TODO 해보기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class Point{ | |
int x, y; | |
public: | |
Point(int pos_x, int pos_y){ | |
x = pos_x; | |
y = pos_y; | |
} | |
int GetX() const {return x;} | |
int GetY() const {return y;} | |
}; | |
class Geometry{ | |
Point* point_array[100]; //점 100개 보관하는 배열 | |
int num_points; | |
public: | |
Geometry(){ | |
num_points = 0; | |
} | |
void AddPoint(const Point &point){ | |
point_array[num_points++] = new Point(point.GetX(), point.GetY()); | |
} | |
void PrintDistance(); //모든 점들 간의 거리 출력 | |
void PrintNumMeets(); //모든 점들을 잇는 직선들 간 교점의 수 출력 | |
}; |
'develop > C++' 카테고리의 다른 글
[씹어먹는 C++ - 4-3, 4] 복사 생성자, 소멸자, const, static (0) | 2021.09.14 |
---|---|
[씹어먹는 C++ - 3] 메모리 할당/해제 (0) | 2021.09.01 |
[씹어먹는 C++ - 2] 참조자 (0) | 2021.09.01 |