티스토리 뷰

반응형

⊙ 문제

 

날짜를 다루는 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
반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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 29 30
글 보관함