I have written this code and it compiles and works just fine but I would like to put the string day[7]; as private since the days of the week will never change and I am required to have at least one private member in my code. When I simply move it the code compiles but errors out like crazy! Can anyone help me? I hesitate to post my whole code as I would like my work to remain my work so here is the first part without the main function(even though what I am posting is the most important part).

#include<iostream>
#include<string>
using namespace std;

class dayOfTheWeek
{
public:
	string day[7];
	
	dayOfTheWeek();//constructor
	~dayOfTheWeek();//destructor
	
	
	int dayNum;
	int tempDay;
	void getDay(int &day);
	void setDay(int _day);
	void printDay();
	
	void plusOneDay();
	void minusOneDay();
	void addDays(int _changeDay);
private:
	

};

dayOfTheWeek::dayOfTheWeek()
{
	dayNum=0;
	day[0]="Sunday";
	day[1]="Monday";
	day[2]="Tuesday";
	day[3]="Wednesday";
	day[4]="Thursday";
	day[5]="Friday";
	day[6]="Saturday";
	
};
dayOfTheWeek::~dayOfTheWeek()
{
}


void dayOfTheWeek::printDay()
{
	cout << "Today is: " << day[dayNum]  << endl << endl;
};

void dayOfTheWeek::setDay(int _day)
{
	dayNum=_day;
};

void dayOfTheWeek::getDay(int &day)
{
	day=dayNum;
};

void dayOfTheWeek::plusOneDay()
{
	dayNum++;
};

void dayOfTheWeek::minusOneDay()
{
	dayNum--;
};

void dayOfTheWeek::addDays(int _changeDay)
{
	tempDay =(dayNum + _changeDay);
	dayNum =(tempDay%7);
};

I would like to put the string day[7]; as private since the days of the week will never change

Given the above code, you might have the string day[7] as a private member variable. So you must be doing something illegal in the code that you did not post.

Furthermore, why not make that a private static const member variable, so you'd have

class dayOfTheWeek
{
public:
    // <snip>
private:
    static const std::string days[7];
};

// and outside the class declaration ...
const std::string dayOfTheWeek::days[7] = 
{
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
};

If you don't make it a static member variable, then every instance of the dayOfTheWeek class has an unnecessary copy of that data (wasting memory).

Edited 6 Years Ago by mitrmkar: 7

Comments
Good post

Also, your method definitions, e.g.,

void dayOfTheWeek::printDay()
{
	cout << "Today is: " << day[dayNum]  << endl << endl;
};

and onward should not end with a semicolon after the brace (you do still need the ones at the end of your class declaration and your const array).

Also, your method definitions, e.g.,

void dayOfTheWeek::printDay()
{
	cout << "Today is: " << day[dayNum]  << endl << endl;
};

and onward should not end with a semicolon after the brace (you do still need the ones at the end of your class declaration and your const array).

That should not matter.

That should not matter.

That's my bad. I guess I had never seen it done that way. Apologies to the OP.

That's my bad. I guess I had never seen it done that way. Apologies to the OP.

With e.g. GCC's -pedantic/-pedantic-errors options switched on, the code won't compile. So no reason for apologies :).

Edited 6 Years Ago by mitrmkar: link

This article has been dead for over six months. Start a new discussion instead.