출처 : 반크_백제역사 유전지구와 이스탐블 역사지구
과제]
참고풀이]
Date.h)
// Data.h
#include <string>
#ifndef DATE_H
#define DATE_H
class Date {
public:
explicit Date(int = 1, int = 1, int = 1900);
std::string toString() const;
~Date();
private:
int month;
int day;
int year;
};
#endif
Date.cpp)
// Date.cpp
#include <array>
#include <iostream>
#include <sstream>
#include "Date.h"
using namespace std;
Date::Date(int mn, int dy, int yr): month{ mn }, day{ dy }, year{ yr } {
cout << "Date object constructor for date " << toString() << endl;
}
string Date::toString() const {
ostringstream output;
output << month << '/' << day << '/' << year;
return output.str();
}
Date::~Date() {
cout << "Date object destructor for date " << toString() << endl;
}
Employee.h)
// Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
#include "Date.h"
class Employee {
public:
Employee(const std::string&, const std::string&, const Date&, const Date&);
std::string toString() const;
~Employee();
private:
std::string firstName;
std::string lastName;
const Date birthDate;
const Date hireDate;
};
#endif
Employee.cpp)
//Employee.cpp
#include <iostream>
#include <sstream>
#include "Employee.h"
#include "Date.h"
using namespace std;
Employee::Employee(const string& first, const string& last, const Date& dateOfBirth,
const Date& dateOfHire): firstName{ first }, lastName{ last }, birthDate{ dateOfBirth },
hireDate{ dateOfHire } {
cout << "Employee object constructor: " << firstName << ' ' << lastName << endl;
}
string Employee::toString() const {
ostringstream output;
output << lastName << ", " << firstName << " Hired: " << hireDate.toString()
<< " Birthday: " << birthDate.toString();
return output.str();
}
Employee::~Employee() {
cout << "Employee object destructor: " << lastName << ", " << firstName << endl;
}
Main.cpp)
수정전)
// main.cpp
#include <iostream>
#include "Date.h"
#include "Employee.h"
using namespace std;
int main() {
Date Birth{5, 16, 1993 };
Date Hire{ 9, 18, 2008 };
Employee manager{ "wlrma", "dl", Birth, Hire };
cout << "\n" << manager.toString() << endl;
return 0;
}
수정후)
// main.cpp
#include <iostream>
#include "Date.h"
#include "Employee.h"
using namespace std;
int main() {
Date Birth{5, 16, 1993 };
Date Hire{ 9, 18, 2008 };
cout << "Date object constructor for date " << Birth.toString() << endl;
cout << "Date object constructor for date " << Hire.toString() << endl;
Employee manager{ "wlrma", "dl", Birth, Hire };
cout << "\n" << manager.toString() << endl;
return 0;
}
참고풀이 결과]
수정전 결과)
수정후 결과)
대한민국의 아름다운 영토, 독도의 여름
'프로그램 > C++ 1000제' 카테고리의 다른 글
C++ 115제] 인하대 과제4 (1) | 2024.10.06 |
---|---|
C++ 114제] 인하대 과제 3 (1) | 2024.10.01 |
C++ 112제] 어서와! C++은 처음이지! CHAPTER 09. PROGRAMMING EXEPCISE 2. p393 (0) | 2024.08.11 |
C++ 111제] 두근두근 파이썬(개정판) CHAPTER 7 연습문제5 p233 (0) | 2024.02.11 |
C++ 110제] 어서와 C++는 처음이지! CHAPTER 04 p185 도전문제 (1) | 2022.11.04 |
댓글