티스토리 뷰
반응형
⊙ 문제
날짜를 다루는 Date 클래스를 작성하고자 한다. Date를 이용하는 main()과 실행 결과는 다음과 같다. 클래스 Date를 작성하여 아래 프로그램에 추가하라.
#include <iostream>
using namespace std;
int main() {
Date birth(2014,3,20);
Date independenceDay("1945/8/15");
independenceDay.show();
cout << birth.getYear() <<"," << birth.getMonth() << ',' << birth.getDay() <<'\n';
}
⊙ 문제 접근 과정
Date class를 만들어주자
⊙ stoi() 함수
<string> 헤더 파일에 있다. string의 문자열을 숫자로 변환할 수 있다.
⊙ 문제 풀이
#include <iostream>
#include <string>
using namespace std;
class Date {
int year,month,day;
string s;
public:
Date(int a, int b, int c) { year = a;
month = b;
day = c;}
Date(string s) {year = stoi(s.substr(0,4));
month = stoi(s.substr(5,1)); day =stoi(s.substr(7,2));}
void show();
int getYear() {return year;}
int getMonth(){return month;}
int getDay(){return day;}
};
void Date::show() {
cout << year << "년" << month << "월" <<day<<"일\n";
}
int main() {
Date birth(2014,3,20);
Date independenceDay("1945/8/15");
independenceDay.show();
cout << birth.getYear() <<"," << birth.getMonth() << ',' << birth.getDay() <<'\n';
}
⊙ 결과
⊙ 마무리
NONE
좋아요는 로그인하지 않아도 누를 수 있습니다!
728x90
반응형
'명품 C++ Programming' 카테고리의 다른 글
[명품 C++ Programming] 3장 1번 - C++[CPP] (0) | 2021.05.25 |
---|---|
[명품 C++ Programming] 2장 실습문제 (1~16번) - C++[CPP] (0) | 2021.04.17 |
[명품 C++ Programming] 1장 실습문제 (1~4번) - C++[CPP] (0) | 2021.04.17 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- java
- OS
- 파이썬
- C++
- 백준
- Web
- py
- 프로그래머스
- Python
- BFS
- CPP
- 자바
- 쉽게배우는
- 운영체제
- 연습문제
- JS
- 쉽게배우는자바프로그래밍
- 자바스크립트
- 알고리즘
- 문자열
- 답
- 구현
- 정리
- 정렬
- 우종정
- 해답
- 그리디
- 풀이
- 정답
- 쉽게 배우는 자바 프로그래밍
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함