9 Replies - 852 Views - Last Post: 15 March 2010 - 01:08 AM Rate Topic: -----

#1 Guest_Dboy*


Reputation:

Object oriented

Posted 14 March 2010 - 11:27 PM

Write a program that contains a class that implements the days of the week. The program should be able to perform the following on an object of the class.

1. Set the day
2. Print the day
3. Return the day
4. Return the next day
5. Return the previous day
6. Calculate and return the day by adding a certain amount of days to the current day. For example if you add 5 days to Saturday, the day to be returned is Thursday. Likewise, if we add 12 days to Wednesday, the day returned will be Monday.


I have most of the code, but I don't know how to define the add and subtract classes and that is the only problem that I have!

THe only reason my code doesn't work is because of this particular area that I need help with:

void addone day

{
}

void minusone day
{
}

I have declared them and the rest of my code is fine, it is just that I cannot get this right! can someone help me?
#include <iostream>
#include <string>

using namespace std;

class DayOfTheWeek 
{
public:
        void setDay(string );
                // setDay(string) takes a string parameter 
                // and stores the value in the day attribute.
        void printDay() const;
                // printDay() prints the value of the day attribute
                // on console output (cout).
        string getDay() const;
                // returns the value of the day attribute.
        void plusOneDay();

        void minusOneDay();

        void addDays();
        
private:
        string day; // This is where the value of the day attribute is stored.
};

string DayOfTheWeek::getDay() const 
{
        return day; 
}
void DayOfTheWeek::setDay(string newDay)
{
        day = newDay; 
}
void AddDays(int ds,int dayname,int day)
{
day = (day + ds) % 7;
cout << dayname;
} 


void DayOfTheWeek::plusOneDay()
{
        
}
void DayOfTheWeek::minusOneDay()
{
}
void DayOfTheWeek::printDay() const 
{
        cout << day; 
}
int main()
{
        
        DayOfTheWeek monday;
        DayOfTheWeek tuesday;
        DayOfTheWeek wednesday;
        DayOfTheWeek thursday;
        DayOfTheWeek friday;
        DayOfTheWeek saturday;
        DayOfTheWeek sunday;


        // Set the values of the objects
        monday.setDay("Monday");
        tuesday.setDay("Tuesday");
        wednesday.setDay("Wednesday");
        thursday.setDay("Thursday");
        friday.setDay("Friday");
        saturday.setDay("Saturday");
        sunday.setDay("Sunday");

        // Get the value of the monday object and print it out
        string currentDay = monday.getDay();
        cout << "The value of the monday object is " << currentDay << "." << endl;

        

        // Print out the value of the tuesday object
        cout << "The value of the tuesday object is ";
        tuesday.printDay();
        cout << "." << endl;;

        // We're finished
        return 0;
}



Is This A Good Question/Topic? 0

Replies To: Object oriented

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Object oriented

Posted 14 March 2010 - 11:56 PM

I would have done this in a slightly different way. If you change the setDay method to use the day of week number as a parameter instead of the string, then the plus and minus a day becomes a little easier. The best solution for this would be to have the DayOfWeek class derive from a base class as show below.

#include <iostream>
#include <string>

using namespace std;

class baseDayOfTheWeek {
	protected:
		int dayNumber;
		static string dayOfWeekText[7];
};

string baseDayOfTheWeek::dayOfWeekText[7] = { "Monday", "Tuesday", "Wednesday",
                                              "Thursday", "Friday", "Saturday",
                                              "Sunday" };

class DayOfTheWeek : protected baseDayOfTheWeek {
	public:
        void setDay(int);
                // setDay(string) takes a string parameter 
                // and stores the value in the day attribute.
        void printDay() const;
                // printDay() prints the value of the day attribute
                // on console output (cout).
        string getDay() const;
                // returns the value of the day attribute.
        void plusOneDay();

        void minusOneDay();

        void addDays();
};

string DayOfTheWeek::getDay() const {
    return dayOfWeekText[dayNumber]; 
}
void DayOfTheWeek::setDay(int dayNo) {
    dayNumber = dayNo;
}
void AddDays(int ds,int dayname,int day) {
	day = (day + ds) % 7;

	cout << dayname;
} 

void DayOfTheWeek::plusOneDay() {
	dayNumber++;      
}
void DayOfTheWeek::minusOneDay() {
	dayNumber++;      
}

void DayOfTheWeek::printDay() const {
        cout << dayOfWeekText[dayNumber]; 
}

int main() {
        
        DayOfTheWeek monday;
        DayOfTheWeek tuesday;
        DayOfTheWeek wednesday;
        DayOfTheWeek thursday;
        DayOfTheWeek friday;
        DayOfTheWeek saturday;
        DayOfTheWeek sunday;


        // Set the values of the objects
        monday.setDay(0);
        tuesday.setDay(1);
        wednesday.setDay(2);
        thursday.setDay(3);
        friday.setDay(4);
        saturday.setDay(5);
        sunday.setDay(6);

        // Get the value of the monday object and print it out
        string currentDay = monday.getDay();
        cout << "The value of the monday object is " << currentDay << "." << endl;

        // Print out the value of the tuesday object
        cout << "The value of the tuesday object is ";
        tuesday.printDay();
        cout << "." << endl;;

        // We're finished
        return 0;
}



I will leave the rest to you :sigh:
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Dboy*


Reputation:

Re: Object oriented

Posted 15 March 2010 - 12:11 AM

I still don't get how to do this part!

void DayOfTheWeek::plusOneDay() {
        dayNumber++;      
}
void DayOfTheWeek::minusOneDay() {
        dayNumber++;      
}


Was This Post Helpful? 0

#4 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Object oriented

Posted 15 March 2010 - 12:45 AM

View PostDboy, on 15 March 2010 - 06:11 AM, said:

I still don't get how to do this part!

void DayOfTheWeek::plusOneDay() {
        dayNumber++;      
}
void DayOfTheWeek::minusOneDay() {
        dayNumber++;      
}



I think you will need to return a string, so you would have:

string& DayOfTheWeek::plusOneDay() {
    int day = dayNumber + 1;
    if ( day > 6 ) day = 0;
    return dayOfWeekText[day];
}
string& DayOfTheWeek::minusOneDay() {
    int day = dayNumber - 1;     
    if ( day < 0 ) day = 6;
    return dayOfWeekText[day];
}



Don't forget to chage the class declarations.

This post has been edited by Martyn.Rae: 15 March 2010 - 12:46 AM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_Dboy*


Reputation:

Re: Object oriented

Posted 15 March 2010 - 12:53 AM

This worked better

void DayOfTheWeek::plusOneDay() {
        dayNumber++; 
		int day = dayNumber + 1;
        if ( day > 6 ) day = 0;
		return DayOfTheWeek;

}
void DayOfTheWeek::minusOneDay() {
        dayNumber++;      
    int day = dayNumber - 1;     
    if ( day < 0 ) day = 6;
    return DayOfTheWeek;
}

but what should I return each function as? because " return DayOfTheWeek" doesn't work
Was This Post Helpful? 0

#6 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: Object oriented

Posted 15 March 2010 - 12:57 AM

Right now the returnvalue of your minusoneday and plusoneday are defined to return nothing: they should return 'void'. So return DayOfTheWeek; (an object) is not allowed/expected. Returning this whole object is also a bit too much; Martyn suggested to let those methods return a string.
Take a look at his code, and you will see how to make your methods return something useful!
EDIT: Also, your implementation changes the internal dayNumber, so subsequent calls to your methods will give a different result (each time another day later or sooner!)

This post has been edited by Ferencn: 15 March 2010 - 01:01 AM

Was This Post Helpful? 0
  • +
  • -

#7 Guest_Guest*


Reputation:

Re: Object oriented

Posted 15 March 2010 - 01:01 AM

But I get error codes when I use marty's code
is it another way?
Was This Post Helpful? 0

#8 Guest_Guest*


Reputation:

Re: Object oriented

Posted 15 March 2010 - 01:03 AM

When I use marty's code, these are the errors I get!
1>z:\d03141392\documents\visual studio 2005\projects\dd\dd\add.cpp(55) : error C2556: 'std::string &DayOfTheWeek::minusOneDay(void)' : overloaded function differs only by return type from 'void DayOfTheWeek::minusOneDay(void)'
1>        z:\d03141392\documents\visual studio 2005\projects\dd\dd\add.cpp(33) : see declaration of 'DayOfTheWeek::minusOneDay'
1>z:\d03141392\documents\visual studio 2005\projects\dd\dd\add.cpp(55) : error C2040: 'DayOfTheWeek::minusOneDay' : 'std::string &(void)' differs in levels of indirection from 'void (void)'
1>Build log was saved at "file://\\ilabss\home$\D03141392\Documents\Visual Studio 2005\Projects\Dd\Dd\Debug\BuildLog.htm"
1>Dd - 4 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========


Was This Post Helpful? 0

#9 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: Object oriented

Posted 15 March 2010 - 01:04 AM

View PostGuest, on 15 March 2010 - 08:01 AM, said:

But I get error codes when I use marty's code
is it another way?


Read carefully what Martyn wrote, below the code snippet:
"Don't forget to chage the class declarations."

You must tell the compiler that the plusOneDay and minusOneDay methods return a 'string&' instead of a 'void'.
This must also be changed in the class declaration.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Guest*


Reputation:

Re: Object oriented

Posted 15 March 2010 - 01:08 AM

Thank you! It worked!
Was This Post Helpful? 0

Page 1 of 1