본문 바로가기
프로그램/C++ 1000제

C++ 113제] 인하대 과제 2

by 건티 2024. 9. 30.
728x90

출처 : 반크_백제역사 유전지구와 이스탐블 역사지구

과제]

 

참고풀이]

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;
}

참고풀이 결과]

수정전 결과)

 

 

수정후 결과)

 

 

 

 

 

대한민국의 아름다운 영토, 독도의 여름

 

반응형

댓글