So I recently started learning Object Oriented Programming, and my professor gave me some questions to work on my own. One of these questions involve creating a code which will ask for the current day (for eg, Mon, or Sun) and then will display that information, along with the day that comes before and the day that comes after. While I do have general understanding of classes and how they work, I cannot for the life of me figure out how to translate the logic for this code into a class. I have managed to run it using the main source file, however.
The question states that I have to create functions in the class to figure out what the days are, but I don't understand how to reference the 2d array into the classes and where to keep it. Does it need to be an attribute in the class? If so, then how do I initialize it?
#include <iostream>
#include <string>
namespace utilities {
const std::string allDays[7] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
staticint selectDay()
{
int x;
std::cout << "What is today? Select a number :\n";
for (size_t d = 0; d <= allDays->size(); ++d)
std::cout << "(" << d + 1 << ") " << allDays[d] << std::endl;
while (1)
{
std::cin >> x;
if (!std::cin.fail() && x >= 1 && x <= 7)
break;
std::cin.clear();
std::cin.ignore();
std::cout << "Bad entry. Enter a number between 1 and 7 : ";
}
return x - 1;
}
};
struct giveMeTheDay {
std::string _today(int x) { return utilities::allDays[x]; }
std::string _yesterday(int x)
{
if (x > 0)
return utilities::allDays[x - 1];
elsereturn utilities::allDays[utilities::allDays->size()];
}
std::string _tomorrow(int x)
{
if (x < utilities::allDays->size())
return utilities::allDays[x + 1];
elsereturn utilities::allDays[0];
}
};
int main()
{
int d = utilities::selectDay();
giveMeTheDay day;
std::cout << "Yesterday was " << day._yesterday(d) << std::endl;
std::cout << "Today is " << day._today(d) << std::endl;
std::cout << "Tomorrow will be " << day._tomorrow(d) << std::endl;
return 0;
}
What is today? Select a number :
(1) Monday
(2) Tuesday
(3) Wednesday
(4) Thursday
(5) Friday
(6) Saturday
(7) Sunday
7
Yesterday was Saturday
Today is Sunday
Tomorrow will be Monday
didn't ensure sane input, so negative values, non integer input, etc may go nuts. May act up at extremes, eg 2^32-1 may confuse it, but it should get you started.
he asked for it in a class. OSO design, objects for the sake of objects, which a lot of people follow religiously.
I hinted about it in another thread but a large issue with teaching OOP is the bad examples and poor teaching of why you use objects and what for. So many of them are like this, where you do it because you were told to, rather than because it makes any sort of sense.
Not sure I agree with Herb here.
I mean, a function that computes something a little off the usual, lets say a sum of products of the contents of a vector like is seen in AI routines .. it mentions and is supplied with a vector, or a specialization of vector at least, but its in no way meant to be a part of it: a vector is just a container, and this function is just a consumer of the object. Its not even an interface to the object, its just a user that could have gotten along with &vector[0] + vector.size() being passed in instead, C style. He has a great point, but I think its overreaching a bit. Its a useful read, even if there are places its not really a good fit.
Clearly your vector isnt in the same header/namespace as std vector, but its no stretch that your own objects could have helper functions living in their header that are not really a part of it, just riding along in the file because programmers. If you want to argue that programmers shouldn't do lazy things, its a whole new discussion maybe. Lots of code does have the occasional one liner that does not belong hanging out in a H file, though.
In my opinion, what Herb calls the "interface principle" is really about the syntactic distinction between object.function() and function(object). The former notation shouldn't exist at all.
Syntactic uniformity is a prerequisite for polymorphism. So when C++ introduced function overloading and templates and subtyping and dynamic dispatch, why did it also introduce idiosyncratic syntax that makes programs that exploit those things less flexible?