上课的时候老师提到了个这个东西,然后给了代码实例,可是无论如何执行main.cpp都会有些错误
Example DATE class – Using METHOD Three
File 1 - Date.h Header file
#pragma once // needed to work class Date { public: void get(); // function header for inputting the date void write(); // function header for outputting the date private: int month; // month (between 1 and 12) int day; // day (between 1 and 31) int year; // year (between 0 and 99) }
File 2 – Date.cpp Implementation file
#include <iostream> #include <iomanip> using namespace std; #include "Date.h" // Include the library/file name of the class in quotes
void Date::get() { char slash; // Allows for inputting in the format MM/DD/YY cin >> month >> slash >> day >> slash >> year; }
void Date::write() { cout << "The date you entered was: " << setw(2) << month << "/" << setw(2) << day << "/" << setw(2) << year << endl; }
File 3 – Driver/Test fileImp.Date.cpp
#include <iostream> #include <iomanip> using namespace std; #include "Date.h" // include DATE class header file name
int main () { Date aDate; // declaring A as a type Date
cout << "Enter a date in the form MM/DD/YY: \n"; aDate.get(); // Function which will input our date aDate.write(); // Function which will output our date return 0;
相关分类