C++

명품 C++ 실습문제 3장 2번

Hs’s Coding vlog 2023. 1. 3. 18:55
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Date{
public:

char *token;
int Year;
int Month;
int days;
char str[100];

Date();//기본생성자
Date(int Y,int M,int D){
    Year = Y , Month =M, days=D;
};//매개생성자 자동 인라인 함수

Date(string s);//매개생성자 문장받는 생성자
    
int getYear();//멤버함수 선언
int getMonth();
int getDay();
void show();

};

Date::Date(){//기본생성자 구현

}

Date::Date(string s){
    strcpy(str,s.c_str());

    token = strtok(str,"/");//delim은 문자열인것을 명심해라
    Year = stoi(token);
    token = strtok(NULL,"/");//delim은 문자열인것을 명심해라
    Month = stoi(token);
    token = strtok(NULL,"/");//delim은 문자열인것을 명심해라
    days = stoi(token);


}

int Date::getYear(){
    return Year;
}
int Date::getMonth(){
    return Month;
}
int Date::getDay(){
    return days;
}

void Date::show(){
   
    cout << Year <<"년"<< Month <<"월"<< days <<"일"<<endl;
}


int main(){
    Date birth(2014,3,20);
    Date independenceday("1945/8/15");
    independenceday.show();

    cout << birth.getYear() << ',' << birth.getMonth() << ',' << birth.getDay() << endl;
}

'C++' 카테고리의 다른 글

명품 C++ 실습문제 3장 6번  (0) 2023.01.03
명품 C++ 실습문제 3장 4번  (0) 2023.01.03
명품 C++ 실습문제 3장 1번  (0) 2023.01.03
명품 C++ 실습문제 2장 15번  (0) 2023.01.03
명품 C++ 실습문제 2장 16번  (0) 2023.01.03