Intro to classes

I am working on a homework question and I am a little stuck. I am supposed to add the function convrt() to the date class, and then in response it is going to access my month, year and date and return a long integer. It is calculated as year*10000+month*100+day. Anyway, I am running into a problem when it comes to that formula. Thanks in advance.

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
  #include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <cctype>
using namespace std;
// Class declaration
class Date
{
private:
int month;
int day;
int year;

public:
Date(int = 7, int = 4, int = 2005); // constructor
void convrt(); // another constructor
void showdate(); // member function to display a date 
void setdate(int, int, int); // member function to copy a date
};
// implementation section
Date::Date(int mm, int dd, int yyyy)
{
month = mm;
day = dd;
year = yyyy;
}
void Date::setdate(int mm, int dd, int yyyy)
{
     month = mm;
     day = dd;
     year = yyyy;
     return;
}
void Date:: Convrt(yyyy*10000+mm*100+dd)
{
year = int (yyyymmdd/1000.0); // extract the year
month = int ( (yyyymmdd - year * 10000.0)/100.00); // extract the month
day = int (yyyymmdd - year * 10000.0 - month * 100.0); // extract the day
}
void Date::showdate()
{
cout << "The date is ";
cout << setfill ('0')
<< setw(2) << month << '/'
<< setw(2) << day << '/'
<< setw(2) << year % 100; // extract the last 2 year digits
cout << endl;

return;
}
int main()
{
Date a, b(4, 1, 1998), c(20060515), d (12, 20, 1993); // declare FOUR objects

a.showdate(); // display object a's values
b.showdate(); // display object b's values
c.showdate(); // display object c's values
d.showdate(); // display object d's values

cout<<"\n\n\nThank You.\n\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
closed account (18hRX9L8)
Errors:
1. Case error! convrt is not the same as Convrt.
2. Prototype error! Convrt(void) is not the same as Convrt(int).
3. Statement error! You cannot run a statement when you are defining a function.

Changes made to compile:
1 and 2. Simply change line 20 to void Convrt(int); // ...
3. Change line 38 to void Date:: Convrt(int yyyymmdd)

Final code:
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
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <cctype>
using namespace std;

// Class declaration
class Date {
	private:
		int month;
		int day;
		int year;
	public:
		Date(int = 7, int = 4, int = 2005); // constructor
		void Convrt(int); // another constructor
		void showdate(); // member function to display a date 
		void setdate(int, int, int); // member function to copy a date
};

// implementation section
Date::Date(int mm, int dd, int yyyy) {
	month = mm;
	day = dd;
	year = yyyy;
}

void Date::setdate(int mm, int dd, int yyyy) {
	month = mm;
	day = dd;
	year = yyyy;
}

void Date:: Convrt(int yyyymmdd) {
	year = int (yyyymmdd/1000.0); // extract the year
	month = int ( (yyyymmdd - year * 10000.0)/100.00); // extract the month
	day = int (yyyymmdd - year * 10000.0 - month * 100.0); // extract the day
}

void Date::showdate() {
	cout << "The date is ";
	cout << setfill ('0')
		<< setw(2) << month << '/'
		<< setw(2) << day << '/'
		<< setw(2) << year % 100; // extract the last 2 year digits
	cout << endl;
}

int main() {
	Date a, b(4, 1, 1998), c(20060515), d (12, 20, 1993); // declare FOUR objects

	a.showdate(); // display object a's values
	b.showdate(); // display object b's values
	c.showdate(); // display object c's values
	d.showdate(); // display object d's values

	cout<<"\n\n\nThank You.\n\n\n";
	cin.ignore();
	
	return EXIT_SUCCESS;
}
Last edited on
Boy, you are a genius! Thank you so much.
Topic archived. No new replies allowed.