How do I rewrite my code into classes?

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ifstream Main;
	char Name[7][4] = { {"Sun"},{"Mon"},{"Tue"},{"Wed"},{"Thu"},{"Fri"},{"Sat"} };
	char Day[4];
	char BeforeDay[4];
	char AfterDay[4];
	int Dayno = 6;

	for (int i = 0; i < 3; i++)
	{
		Day[i] = Name[Dayno][i];
	}
	Day[3] = '\0';

	for (int i = 0; i < 3; i++)
	{
		if ((Dayno - 1) >= 0)
		{
			BeforeDay[i] = Name[Dayno - 1][i];
		}
		else
		{
			BeforeDay[0] = 'S';
			BeforeDay[1] = 'a';
			BeforeDay[2] = 't';
			BeforeDay[3] = '\0';
			break;
		}
	}
	BeforeDay[3] = '\0';

	for (int i = 0; i < 3; i++)
	{
		if ((Dayno + 1) <= 6)
		{
			AfterDay[i] = Name[Dayno + 1][i];
		}
		else
		{
			AfterDay[0] = 'S';
			AfterDay[1] = 'u';
			AfterDay[2] = 'n';
			AfterDay[3] = '\0';
		}
	}
	AfterDay[3] = '\0';

	cout << "Today is: " << Day<< endl;
	cout << "So Yesterday was: " << BeforeDay << endl;
	cout << "And Tomorrow will be: " << AfterDay << endl;

	return 0;
}


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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>

struct week_day
{
    enum day { SUN=0, MON, TUE, WED, THU, FRI, SAT } ; 

    week_day() = default ;
    week_day( day d ) noexcept : today(d) {}

    const std::string& to_string() const noexcept
    {
        static const std::string day_names[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" } ;
        return day_names[today] ;
    }

    week_day previous() const noexcept
    {
        if( today == SUN ) return week_day(SAT) ;
        else return week_day( day(today-1) ) ;
    }

    week_day next() const noexcept
    {
        if( today == SAT ) return week_day(SUN) ;
        else return week_day( day(today+1) ) ;
    }

    private: day today = day::SUN ;

    friend std::ostream& operator<< ( std::ostream& stm, week_day wd ) { return stm << wd.to_string() << " (" << wd.today << ')' ; }
};

int main()
{
    const week_day today( week_day::MON ) ;
    std::cout << "         today is: " << today << '\n'
              << "    yesterday was: " << today.previous() << '\n'
              << "tomorrow would be: " << today.next() << '\n' ;
}

http://coliru.stacked-crooked.com/a/b72154f4348ca10f
The first question is why are you using c-style strings in a C++ program instead of std::string?
Hello. Take a look at this atternative which works as expected, showing a way to play with arrays :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>

namespace utilities {

    const std::string allDays[7] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

    static int 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];
        else
            return utilities::allDays[utilities::allDays->size()];
    }

    std::string _tomorrow(int x)
    {
        if (x < utilities::allDays->size())
            return utilities::allDays[x + 1];
        else
            return 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
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct daything
{
  char Name[7][4] = { {"Sun"},{"Mon"},{"Tue"},{"Wed"},{"Thu"},{"Fri"},{"Sat"} };	
  string getCurr(unsigned int d) {return Name[d%7];}
  string getPrev(unsigned int d) {if(d ==0) d+= 7;  return Name[(d-1)%7];} 
  string getNext(unsigned int d) {return Name[(d+1)%7];}
};
int main()
{	
  daything d;
  int i;
  cout << "what day #? >\n";
  cin >> i;
	 cout << "before:"s + d.getPrev(i) << endl;
	 cout << "today:"s + d.getCurr(i) << endl;
	 cout << "after:"s + d.getNext(i) << endl;
}
Last edited on
Geckoo/jonnin, why even have giveMeTheDay/daything be a class? It has no state. Those could just be functions.
Last edited on
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.
Last edited on
Herb Sutter described Interface Principle in http://www.gotw.ca/publications/mill02.htm
That is, (some) standalone functions are "OOP".


@Geckoo: Your utilities::allDays is a global (constant). How would it fare if the same program should handle dates of multiple locales?
@keskiverto - I could fix it this way : works only with english date format :)

More seriously, it's an interesting question. I have to think about it ++
Last edited on
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.
Last edited on
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?
Last edited on
Topic archived. No new replies allowed.