This is a demo of what my teacher gave me on classes:
main.cpp:
Code:
#include "Demo.h"
#include <iostream>
using namespace std;
int main()
{
DateType a, b(5,4,2010), c(32,-1,2023);
cout<<"a: "<<a.getMonth()<<"/"<<a.getDay()<<"/"<<a.getYear()<<endl;
cout<<"b: "<<b.getMonth()<<"/"<<b.getDay()<<"/"<<b.getYear()<<endl;
cout<<"c: "<<c.getMonth()<<"/"<<c.getDay()<<"/"<<c.getYear()<<endl;
}
Demo.h file:
Code:
#include <iostream>
using namespace std;
/*
The starting form of the class from the text book
class DateType
{
public:
void initialize(int newMonth, int newDay, int newYear);
int getYear() const;
int getMonth() const;
int getDay() const;
private:
int year;
int month;
int day;
};
*/
class DateType
{
public:
DateType(int = 1, int = 1, int = 2000); //constructor with values
DateType(const DateType &); //copy constructor
~DateType(); //destructor
void initialize(int newMonth, int newDay, int newYear);
int getYear() const;
int getMonth() const;
int getDay() const;
private:
int year;
int month;
int day;
static int count;
};
int DateType::count = 0;
DateType::DateType(int newM, int newD, int newY)
{
initialize(newM, newD, newY);
count++;
}
DateType::DateType(const DateType &original)
{
year = original.year;
month = original.month;
day = original.day;
// or simply
// *this = original;
// for memberwise copy
count++;
}
DateType::~DateType()
{
cout << "destructor is running. Count = " << --count << endl;
}
void DateType::initialize(int m, int d, int y)
{
month = (m>=1 && m<=12)?m:1;
// note that the following is an insufficient check on legal day
day = (d>=1 && d<=31)?d:1;
year = (y>1900 && y<2012)?y:2000;
}
int DateType::getYear() const
{
return year;
}
int DateType::getMonth() const
{
return month;
}
int DateType::getDay() const
{
return day;
}