adding the date class files
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
# Overview
|
||||
|
||||
In this lab, you will be writing your own C++ classes. We have not covered C++ classes in the lecture, but according to last Friday's poll, everyone is this class has either experience in Java or experience in Python, so the concept of class is not new to you.
|
||||
In this lab, you will be writing your own C++ classes. We have not covered C++ classes in the lecture, but according to last Friday's poll, everyone is this class has either experience in Java or experience in Python, so the concept of class is not new to you. To get familiar with the C++ syntax on classes, you are recommended to quickly review the following 3 files:
|
||||
|
||||
[date.h](../../lectures/03_classes_I/date.h)
|
||||
[date.cpp](../../lectures/03_classes_I/date.cpp)
|
||||
[date_main.cpp](../../lectures/03_classes_I/date_main.cpp)
|
||||
|
||||
## Checkpoint 1
|
||||
*estimate: 15-25 minutes*
|
||||
@@ -110,7 +114,7 @@ program. Yes, please show us you can compile from the terminal with g++, even if
|
||||
use Visual Studio or another IDE for the rest of the semester.
|
||||
|
||||
## Checkpoint 3
|
||||
*estimate: 30-45 minutes*
|
||||
*estimate: 20-30 minutes*
|
||||
|
||||
Create and test a few more member functions. This will require modifications to all three of the files. You should uncomment the provided tests in main.cpp as you work, and add your own tests.
|
||||
- *setHour, setMinute, setSecond*. Each should take a single integer argument and change the appropriate
|
||||
|
||||
86
lectures/03_classes_I/date.cpp
Normal file
86
lectures/03_classes_I/date.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
// File: date.cpp
|
||||
// Purpose: Implementation file for the Date class.
|
||||
|
||||
#include <iostream>
|
||||
#include "date.h"
|
||||
|
||||
// array to figure out the number of days, it's used by the auxiliary function daysInMonth
|
||||
const int DaysInMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
|
||||
Date::Date() { //default constructor
|
||||
day = 1;
|
||||
month = 1;
|
||||
year = 1900;
|
||||
}
|
||||
|
||||
Date::Date(int aMonth, int aDay, int aYear) { // construct from month, day, & year
|
||||
month = aMonth;
|
||||
day = aDay;
|
||||
year = aYear;
|
||||
}
|
||||
|
||||
|
||||
int Date::getDay() const {
|
||||
return day;
|
||||
}
|
||||
|
||||
int Date::getMonth() const {
|
||||
return month;
|
||||
}
|
||||
|
||||
int Date::getYear() const {
|
||||
return year;
|
||||
}
|
||||
|
||||
void Date::setDay(int d) {
|
||||
day = d;
|
||||
}
|
||||
|
||||
void Date::setMonth(int m) {
|
||||
month = m;
|
||||
}
|
||||
|
||||
void Date::setYear(int y) {
|
||||
year = y;
|
||||
}
|
||||
|
||||
void Date::increment() {
|
||||
if (!isLastDayInMonth()) {
|
||||
day++;
|
||||
} else {
|
||||
day = 1;
|
||||
if (month == 12) { // December
|
||||
month = 1;
|
||||
year++;
|
||||
} else {
|
||||
month++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Date::isEqual(const Date& date2) const {
|
||||
return day == date2.day && month == date2.month && year == date2.year;
|
||||
}
|
||||
|
||||
bool Date::isLeapYear() const {
|
||||
return (year%4 ==0 && year % 100 != 0) || year%400 == 0;
|
||||
}
|
||||
|
||||
int Date::lastDayInMonth() const {
|
||||
if (month == 2 && isLeapYear())
|
||||
return 29;
|
||||
else
|
||||
return DaysInMonth[ month ];
|
||||
}
|
||||
|
||||
bool Date::isLastDayInMonth() const {
|
||||
return day == lastDayInMonth(); // uses member function
|
||||
}
|
||||
|
||||
void Date::print() const {
|
||||
std::cout << month << "/" << day << "/" << year;
|
||||
}
|
||||
|
||||
bool sameDay(const Date& date1, const Date& date2) {
|
||||
return date1.getDay() == date2.getDay() && date1.getMonth() == date2.getMonth();
|
||||
}
|
||||
36
lectures/03_classes_I/date.h
Normal file
36
lectures/03_classes_I/date.h
Normal file
@@ -0,0 +1,36 @@
|
||||
// File: date.h
|
||||
// Purpose: Header file with declaration of the Date class, including
|
||||
// member functions and private member variables.
|
||||
|
||||
class Date {
|
||||
public:
|
||||
Date();
|
||||
Date(int aMonth, int aDay, int aYear);
|
||||
|
||||
// ACCESSORS
|
||||
int getDay() const;
|
||||
int getMonth() const;
|
||||
int getYear() const;
|
||||
|
||||
// MODIFIERS
|
||||
void setDay(int aDay);
|
||||
void setMonth(int aMonth);
|
||||
void setYear(int aYear);
|
||||
void increment();
|
||||
|
||||
// other member functions that operate on date objects
|
||||
bool isEqual(const Date& date2) const; // same day, month, & year?
|
||||
bool isLeapYear() const;
|
||||
int lastDayInMonth() const;
|
||||
bool isLastDayInMonth() const;
|
||||
void print() const; // output as month/day/year
|
||||
|
||||
private: // REPRESENTATION (member variables)
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
};
|
||||
|
||||
// prototypes for other functions that operate on class objects are often
|
||||
// included in the header file, but outside of the class declaration
|
||||
bool sameDay(const Date &date1, const Date &date2); // same day & month?
|
||||
29
lectures/03_classes_I/date_main.cpp
Normal file
29
lectures/03_classes_I/date_main.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
// Program: date_main.cpp
|
||||
// Purpose: Demonstrate use of the Date class.
|
||||
|
||||
#include <iostream>
|
||||
#include "date.h"
|
||||
|
||||
int main() {
|
||||
std::cout << "Please enter today's date.\n"
|
||||
<< "Provide the month, day and year: ";
|
||||
int month, day, year;
|
||||
std::cin >> month >> day >> year;
|
||||
Date today(month, day, year);
|
||||
|
||||
//Let's show what happens if we try to use today.month (private member variable)
|
||||
Date tomorrow(today.getMonth(), today.getDay(), today.getYear());
|
||||
tomorrow.increment();
|
||||
|
||||
std::cout << "Tomorrow is ";
|
||||
tomorrow.print();
|
||||
std::cout << std::endl;
|
||||
|
||||
Date Sallys_Birthday(9,29,1995);
|
||||
if (sameDay(tomorrow, Sallys_Birthday)) {
|
||||
std::cout << "Hey, tomorrow is Sally's birthday!\n";
|
||||
}
|
||||
|
||||
std::cout << "The last day in this month is " << today.lastDayInMonth() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user