Can anyone help me with this constructor problem?

In main:

instantiate one date object (date1) using the default constructor

just to see that the constructor works, print the month, day, and year of date1 using the getters

get input from the user and use the setters to set the values of date1

get input from the user and use the constructor with three arguments to instantiate date2

print both objects using printDate

print a message to say if the two months are the same (testing the sameMonth function)

I cannot figure out how to work out the BOLD part in the question. Here is my main.cpp code so far.

#include "Date.h" 
#include <iostream>
using namespace std; 

int main()
{
    Date date1;
    string Date_month;
    string Date_month2 = " ";
    int inDay2 = 0;
    int inDay;
    int inYear;
    int inYear2 = 0;

    cout << "Test to see constructor works:" << endl; 
    date1.printDate();
    cout << endl; 


    cout << "********************************************************" << endl; 
    // For date1
    cout << "Enter the month: ";
    getline(cin, Date_month, '\n');
    date1.setMonth(Date_month);

    cout << "Enter the day: ";
    cin >> inDay;
    date1.setDay(inDay);

    cout << "Enter the year: ";
    cin >> inYear;
    date1.setYear(inYear);

    date1.printDate(); 


    // For date2
    Date date2("September", 23, 2012);
    cout << endl; 
    cout << "Enter the second month: " << endl;
    getline(cin, Date_month2, '\n');
    date1.setMonth(Date_month);

    cout << "Enter the second day: ";
    cin >> inDay2;
    date2.setDay(inDay2);

    cout << "Enter the second year: ";
    cin >> inYear2;
    date1.setYear(inYear2);

    date2.printDate();
}

Here is my Date.h header file.

#include <iostream>
using namespace std;


class Date
{
    private:
        string month;
        int day;
        int year;
    public:
        Date();
        Date(string aMonth, int aDay, int aYear);
        void setMonth(string month);
        void setDay(int aDay);
        void setYear(int aYear);
        string getMonth();
        int getDay();
        int getYear();
        void printDate();
        bool sameMonth(string month, string aMonth);
};

Uhh you're calling the constructor before getting user input. The question wants you to get user input first THEN use that input to construct the object.

Thus get the user input, pass those values to the constructor. Also you might have had faster answers if you also posted the .cpp file associated with that header as I had to write it myself. I won't post it yet though until I am absolutely sure that you've tried it yourself. I've posted the main example though but as said above, I will hold off on posting the class itself.

According to the instructions your teacher gave you, you need to do something like this (I post this only because you tried, but I'd advise you to read a bit more carefully the requirements):

#include <iostream>
#include "Date.hpp"

using namespace std;

int main()
{
    string Month = string();
    int Day = 0, Year = 0;
    Date Date1;

    cout<<"Testing Constructor to see if it works..\n"<<endl;
    cout<<"Month: "<<Date1.getMonth()<<endl;
    cout<<"Day: "<<Date1.getDay()<<endl;
    cout<<"Year: "<<Date1.getYear()<<endl;

    cout << "\n********************************************************\n" << endl;

    cout<<"Enter The Month: ";
    cin>>Month;
    cin.ignore();

    cout<<"\nEnter The Day: ";
    cin>>Day;
    cin.ignore();

    cout<<"\nEnter The Year: ";
    cin>>Year;
    cin.ignore();

    Date1.setMonth(Month);
    Date1.setDay(Day);
    Date1.setYear(Year);
    Date1.printDate();

    Month.clear(); Day = 0; Year = 0;

    cout << "\n********************************************************\n" << endl;

    cout<<"Enter The Month: ";
    cin>>Month;
    cin.ignore();

    cout<<"\nEnter The Day: ";
    cin>>Day;
    cin.ignore();

    cout<<"\nEnter The Year: ";
    cin>>Year;
    cin.ignore();

    Date Date2(Month, Day, Year);
    Date2.printDate();

    cout << "\n********************************************************\n" << endl;


    return 0;
}

Thanks for the reply, I should clarify on the question though.

Here is the full original problem:

Create a "Date" class that contains:

three private data members:
month
day
year
(I leave it to you to decide the type)

"setters" and "getters" for each of the data (6 functions in total)
The idea of the "setter" would be to provide error checking. For simplicity, you do not have to provide any

one default constructor (no arguments)

one constructor with three arguments: month, day, and year

a printDate function. This function will have no arguments and no return type

a sameMonth function. This function will have one Date argument and a boolean return type

In main:

instantiate one date object (date1) using the default constructor

just to see that the constructor works, print the month, day, and year of date1 using the getters

get input from the user and use the setters to set the values of date1

get input from the user and use the constructor with three arguments to instantiate date2

print both objects using printDate

print a message to say if the two months are the same (testing the sameMonth function)
Your code should be in three files:

Date.h
contains the class definition

Date.cpp
includes "date.h"
contains the functions for the class

main.cpp
includes "date.h"
tests the class

If you are having trouble compiling your class, check for these common errors:

You have forgotten the semi-colon(;) after the closing curly bracket (}) for the class definition (in the date.h file)

You have forgotten the scope resolution as in: void Date::setYear(int y) (in the date.cpp file)

You forgot the () after the function name. For example, you should write: cout << getYear(); (in main.cpp)

I already posted my main.cpp and header file. Here is the third class.cpp file.

#include <iostream>
#include "Date.h"
using namespace std;

Date::Date()
{
    month = "May";
    day = 12;
    year = 1997;
}

Date::Date(string aMonth, int aDay, int aYear)
{
    month = aMonth;
    day = aDay;
    year = aYear;
}

void Date::setMonth(string aMonth)
{
    month = aMonth; 
}

void Date::setDay(int aDay)
{
    day = aDay;
}

void Date::setYear(int aYear)
{
    year = aYear; 
}

string Date::getMonth()
{
    return month; 
}

int Date::getDay()
{
    return day;
}

int Date::getYear()
{
    return year; 
}

void Date::printDate()
{
    //cout << "Month is: " << month << endl;
    //cout << "Day is " << day << endl;
    //cout << "Year is " << year << endl;
    cout << "The date is: " << month << " " << day << ", " << year << endl; 
}

bool sameMonth(Date)
{
}

Am I still in the total wrong direction? An example problem that was given to follow is posted here. http://www.cs.uregina.ca/Links/class-info/115/04-abstractD/StudentClass.cpp

I am trying to follow that as closely as possible.

Ok thank you triumphost I realize one of my original mistakes. I now have the program getting user input to call the constructor.

// For date2
    cout << "Enter the second month: ";
    cin >> Date_month;
    cin.ignore();

    cout << "Enter the second day: ";
    cin >> inDay;
    cin.ignore();

    cout << "Enter the second year";
    cin >> inYear;
    cin.ignore();

    Date date2(Date_month, inDay, inYear);
    date2.printDate();

Now i am just wondering how to declare a function in my class "Date" that has one "Date" argument and returns a bool type. This function will be used to compare the first and second months entered in the program. My question is; how do I declare this sameMonth function in my class? Thanks!

Ahh that's much much better now. I can tell you have shown effort so I'll show you what they meant.

Now the instructions say that it wants a boolean function that takes a "Date" argument.
By that, the teacher means:

bool sameMonth(Date date)
{
    //.....  Compare the Months..
}

In the below code I'm about to post, you'll see something like:

Constructor(Params) : Member(Param1), Member2(Param2) {}

//That's essentially the same as:

Constructor(Params)
{
    Member(Param1);
    Member2(Param2);
}

Now that I see you have 99% of it done, here's how I implemented it.. now there's a couple things different but it's exactly the same as yours:

#include <iostream>
#include <windows.h>

class Date
{
    private:
        std::string Month;
        int Day;
        int Year;

    public:
        Date();
        Date(std::string month, int day, int year);
        void setMonth(std::string month);
        void setDay(int day);
        void setYear(int year);
        std::string getMonth();
        int getDay();
        int getYear();
        void printDate();
        bool sameMonth(const Date &date);
};

Date::Date() : Month("January"), Day(1), Year(2012) {}

Date::Date(std::string month, int day, int year) : Month(month), Day(day), Year(year) {}

void Date::setMonth(std::string month) {Month = month;}

void Date::setDay(int day) {Day = day;}

void Date::setYear(int year) {Year = year;}

std::string Date::getMonth() {return Month;}

int Date::getDay() {return Day;}

int Date::getYear() {return Year;}

void Date::printDate(){std::cout<<"\nThe Date is: "<<Month<<" "<<Day<<", "<<Year<<std::endl;}

bool Date::sameMonth(const Date &date) {return !_stricmp(date.Month.c_str(), Month.c_str());}

Something like the below should suffice:

int main()
{
    Date Date1("September", 30, 2012);
    Date Date2("September", 29, 2012);

    std::cout<<Date1.sameMonth(Date2);
}

Thanks!! Finally figured it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.