Hey, so i wrote this DATE class and I'm still new to programming and really just do not understand the concept of Destructors and constructors. I'm supposed to be able to write a destructor and a copy constructor for the following code. I need some help.
class Date
{
private:
int month;
int day;
int year;
public:
Date()
{
month=1;
day=1;
year=1970;}
Date(int m, int y)
{
month=m;
day=1;
year=y;
}
Date(int m, int d, int y)
{
month=m;
day=d;
year=y;
}
int getMonth()
{return month;}
int getDay()
{return day;}
int getYear()
{return year;}
void setmonth(int m)
{month=m;}
void setDay(int d)
{day=d;}
void setYear(int y)
{year=y;}
void printDate()
{
cout<<month<<"/"<<day<<"/"<<year;
}
};
You need not to define explicitly the destructor for this class as and the copy constructor. You may be satisfied with the destructor and the copy constructor implicitly defined by the compiler.
class Date
{
int month;
int day;
int year;
public:
Date()
{
month = 1;
day = 1;
year = 1970;
}
Date(int m, int y)
{
month = m;
day = 1;
year = y;
}
Date(int m, int d, int y)
{
month = m;
day = d;
year = y;
}
int getMonth() { return month; }
int getDay() { return day; }
int getYear() {return year;}
void setmonth(int m) { month = m; }
void setDay(int d) { day = d; }
void setYear(int y) { year = y; }
void printDate()
{
std::cout << month << "/" << day << "/" << year;
}
};
You don't need to put private: unless you put the private stuff after the public stuff, as the default for a class is private.
Vlad is correct, you don't need to define a copy constructor because the default one will do exactly what you would need it to do (copy the fields as they are). Same goes with the default destructor. unless you wish to have the class say something when it gets destroyed. in that case: