Class Programing/ Date Program

Hello:

Any assistance is really appreciated. I am working on a program using classes (first timer). The program I am trying to make has a class called Date. It is suppose to display the date in format 01/01/17. But in the program, I need to get the date set a new date and update format to January 1st 2017. I need to do the same thing again but with format 1 January 2017. If any one has experience this I would appreciate it. I placed what I have so far below.

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

class Date{
public:
	Date();
	
	string newdate();
	string getdate();
	string setdate();
	string displaydate();
	string d1;

private:

	


};

[\code]

[code]

#include<iostream>
#include<string>
using namespace std;
#include "Date.h"

Date::Date()
{tring d1 = 01 / 01 / 2017;
}

string Date::getdate()
{
	return d1;
}

string Date::setdate()
{
	return newdate;

}
string Date::displaydate()
{
	return newdate;
[\code]

[code]
#include<iostream>
#include<string>
using namespace std;

#include "Date.h"
int main()
{


	Date d1;

	cout << "The current date is \n";
	d1.getdate();
	cout<<"\n\n";




	system("pause");
		return 0;
}



  Put the code you need help with here.
Please update your post closing the code tags with /code instead of \code (the other slash).

I think you are missing an s in your current line 29.
In your current line 60, you probably want to cout the returned string. Actually line 59, 60 and 61 could be a single line: cout << "The current date is \n" << d1.getdate() <<"\n\n";

Finally, are you sure you are supposed to write your own "Date" class with a hard coded string?
If I were a teacher (which I am not) I would like a student to get the current date and display that in the prescribed formats.
http://www.cplusplus.com/reference/ctime/strftime/
http://www.cplusplus.com/reference/chrono/system_clock/now/
Anyway, if you have to change the format, having it as a string is not the most convenient representation, consider a variable that contains the day of the month, a variable that contains the month and a variable that contains the year.
Last edited on
You mean this?
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
#include<iostream>
#include<string>
using namespace std;
//#include "Date.h"

class Date {
public:
	Date();
	string getDate()const;
	void setDate(const string&);
	void displayDate();
private:
	string d1;
};

Date::Date():d1("01 / 01 / 2017"){}

string Date::getDate()const
{
	return d1;
}

void Date::setDate(const string &str)
{
	d1 = str;
}

void Date::displayDate()
{
	cout << d1;
}

int main()
{
	Date d1;
	cout << "The current date is "<<d1.getDate()<<endl;
	d1.setDate("1 January 2017");
	cout << "The new date is ";
	d1.displayDate();
	cout << endl;
	system("pause");
	return 0;
}
I was more thinking about something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Date{
public:
	Date(int d, int m, int y)
	{
		day = d;
		month = m;
		year = y;
		d1 = std::to_string(day) + "/"+std::to_string(month)+"/"+std::to_string(year);
		// alternatively you could also use http://www.cplusplus.com/reference/cstdio/snprintf/
	}

private:
	int day;
	int month;
	int year;
	string d1;
};

If you need to change the format it doesn't help if you only use strings. In fact, if your Date class only contains a string and a set and get function, you could just delete it and use a regular sting.

Also, if you have a get function and a set function, probably your variable should not be public.
Last edited on
Thank you every.

So I do need to be able to do a getDate, setDate and display date. Would I set that up in the actually class? Or would I use the initialize program? Nico, I used yours for this example. Thank you. Would it look like below?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Date{
public:
	Date(int d, int m, int y)
	{
		day = d;
		month = m;
		year = y;
		d1 = std::to_string(day) + "/"+std::to_string(month)+"/"+std::to_string(year);
     void getDate();
     void setDate();
     void displayDate();
	}

private:
	int day;
	int month;
	int year;
	string d1;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Date{
public:
	Date(int d, int m, int y) // this is a function implementation, you can't define other functions inside of it. 
	// Maybe it would have been more clear if I only put a declaration here and put the implemantation later on (next to the implementation of getDate()) but I was lazy.
	{
		day = d;
		month = m;
		year = y;
		// the above makes sure that the three variables in the Date class have the proper values from now on.
		// the line below is one way (of many possible ways) to create a date string using those variables
		d1 = std::to_string(day) + "/"+std::to_string(month)+"/"+std::to_string(year);
	}

     void getDate();      // you still need to implement this function somewhere
     void setDate();      // you still need to implement this function somewhere
     void displayDate();  // you still need to implement this function somewhere

private:
	int day;
	int month;
	int year;
	string d1;
};
Topic archived. No new replies allowed.