Working with classes and Constructors

Hello Fellas I was working with a C++ programming book and I've been working through the chapters and i came across an assignment i decided to tackle. For some reason i keep having problems, I had been working on this assignment for a while and the reason i chose this assignment is because i've seen a lot of posts online talking about this assignment and I was wondering if you guys could explain to me what i am doing wrong. Thanks in advance.

Here is the Implementation File and what i'm trying to accomplish is this: creating a class that is dayType and be able to print the day, set the day, return the next and previous days and have constructors to be able to do so and then test it with the main method to show everything as usual.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
using namespace std;		

class dayType{			//dayType class

public:
	dayType();
	dayType(int d);			//constructors

	void setDay(int setd);
	void printDay() const;
	int getDay() const;			//abstract methods
	int nextDay();
	int prevDay();

private:
	int day;				//private variable in class
	

};

dayType::dayType(){}

dayType::dayType(int d){		//constructor definitions
	d=day;
	
	
}


void dayType::setDay(int setd){		//setDay definition
	setd=day;
	
}


void dayType::printDay() const{				//printDay definition
	

	if(day == 1){
	cout << "The day is Sunday" << endl;
	}
	else if( day == 2){
	cout << "The day is Monday" << endl;
	}
	else if(day == 3){
	cout << "The day is Tuesday" << endl;
	}
	else if(day == 4){
	cout << "The day is Wednesday" << endl;
	}
	else if(day == 5){
	cout << "The day is Thursday" << endl;
	}
	else if(day == 6){
	cout << "The day is Friday" << endl;
	}
	else if(day == 7){
	cout << "The day is Saturday" << endl;
	}
	else{
	cout << "Invalid Day" << endl;
	}
}


int dayType::getDay() const{			//getDay definition

	

	return day;
}


int dayType::nextDay(){				//nextDay Definition
	day++;
	
	if(day > 7){
	day = 1;
	}

	return day;
}


int dayType::prevDay(){				//previous day definition
	day--;

	if(day < 1){
	day = 7;
	}

	return day;

}


This is my main Method, every time i try to test the class with the main method i always get garbage some huge negative number which is probably just memory trash i'm assuming.

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
#include "dayType.h"
#include <iomanip>
#include <iostream>

using namespace std;



int main(){
dayType test(1);      //making an object of class






test.setDay(1);
test.printDay();       //just trying to get it to print the correct output
cout << test.getDay(); //always prints garbage





system("pause");
return 0;
}


i've been experimenting with this forever and for some reason i can't get it to work right any help would be appreciated. Thanks.
Let's start here:
1
2
3
4
void dayType::setDay(int setd){		//setDay definition
	setd=day;  //???????????????????????
	
}
Last edited on
the setd=day is referring to the day private variable in the class

will that not work the way it is? that is probably the problem i guess?
Last edited on
With the assignment operator, the object you want to change goes on the left of the = sign;

object_to_be_changed = new_value


See if you can figure out what you have done wrong now.

Ha! very stupid mistake on my part. I should have known that! Thank you very much Guestgulkan for pointing that out for me i been banging my head for a while now cause i knew the rest was right but i was so puzzled why that wasn't working. I really appreciate your help :)
Also, nothing to do with your question or anything.. but isn't switch case a better idea instead of if(day ==1)... if(day == 2)...

switch(day){
case 1:
//Do something;
break;
case 2:
//Do something;
break;
...
}

just to give you an idea..
Topic archived. No new replies allowed.