I am having serious trouble with my first lab..

Week 1: Object Oriented C++ Programming - iLab

Lab 1: C++ and Classes Part 1

L A B O V E R V I E W

Scenario/Summary

Program Title: The Days of the week

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.
Set the day - Print the day to the computer monitor (not the printer)
Return the day
We will use this same basic program next week to add additional operations and incorporate additional object oriented concepts.
Output
Below is a sample output.

The Value of the Monday Object is Mon.
The Value of the Tuesday Object is Tues.

Remember, this is a suggested output posted to help you determine what
your program output should look like. Feel free to change it in any way you want. SURPRISE ME!

Deliverables


L A B S T E P S
STEP 1: Create the Project and Main Class
Create a new console application project and name it "Week1Lab_YourName".
Create a new class called DayOfTheWeek. The class should have a data member that can store the day of the week such as Mon for Monday, Tues for Tuesday etc...

STEP 2: Create the member functions
Create the necessary member functions that will perform the required operations outlined in the lab summary above. Call these functions setDay, printDay and getDay.

STEP 3: Create a main() program Write a main program that will instantiate two objects of the class DayOfTheWeek. Use these objects to test the various operations on this class. Use the examples provided in the lectures and in the reading to help you perform the required operations.




"this is what i have come up with thus far. I don't know where to begin, how to start, or what to do...."


// Uche Ehiem Week 1 iLab.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

class DayOfTheWeek
{
public:
void setDay(const string a);
void printDate() const;
void getDate(string&) const;



private:
int x;
};

void DayOfTheWeek::setDay(string a)
{

}

void DayOfTheWeek::printDate() const
{
}

void DayOfTheWeek::getDate(string&) const
{

}

int main()
{
DayOfTheWeek MyDay;
DayOfTheWeek YourDay;

cout << "***** Day Of The Week ***** \n\n";

MyDay.setDay("Wednesday");
YourDay.setDay("Saturday");



system("PAUSE");
return 0;
}
Well, the assignment seems to be really limited as to what is necessary.

In your class you should probably have a string variable or an array or char*'s to store the days of the week.

since your setDay function requires a string to be passed, then your member variable should probably be of type string

your get date function shouldn't be void. It should return the private member variable.

In setDay, you could probably just have your member variable (once you declare it a string) just equal whatever string you passed it.

then in your main, you just neet to output the member variable to the screen.

Pretty simple.
Ok, the problem is, I've got the concept down, but I don't know how to actually code this. This is what i have thus far:

// Uche Ehiem Week 1 iLab.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class DayOfTheWeek
{
public:
DayOfTheWeek();
void setDay(const string a);
void printDate() const;
string getDate(string&) const;



private:
char DayOfWeek [7];
};

DayOfTheWeek()
{
DayOfWeek = 0;
}

void DayOfTheWeek::setDay(string a)
{

}

void DayOfTheWeek::printDate() const
{
}

string DayOfTheWeek::getDate(string&) const
{
return string [x]&;
}


int main()
{
DayOfTheWeek MyDay;
DayOfTheWeek YourDay;

cout << "***** Day Of The Week ***** \n\n";

MyDay.setDay("Wednesday");
YourDay.setDay("Saturday");



system("PAUSE");
return 0;
}

I'm stuck on figuring out a way that I can add the days of the weeks to a specific array number so that I can access them through a pointer
Can you go over this with me, I am still confused. I looked over it all this morning, but couldn't come up with the right concept.
First I would recommend turning your member variable to a string (which would require #include <string> )

Then start with the member functions.

in setDay all you're doing is setting the day. And you passed a string literal to the function. So, in that funtion you would just set DayOfWeek to what you passed to the function (string a). Since you aren't asking the user for the day (yet) you don't need to have predefined arrays with the weekdays. And the function title itself says that all you're doing is setting the member variable DayOfWeek to what was passed to it.

then in getDate, you would just return DayOfWeek. This is because your member variable is private and it allows you to access that information in the main function.

in print date, you would just cout << DayOfWeek; again, this is just letting you access the private information you set with setDate.

Also:
1
2
3
4
string DayOfTheWeek::getDate(string&) const
{
return string [x]&;
}


your string doesn't have a name. You need to declare it in your function header. And why is it referenced? You made the function a const, so you're not going to be able to change the value you passed it. I would make this function with no formal parameters.
Last edited on
Topic archived. No new replies allowed.