473,507 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help adding convert method to existing Date class

63 New Member
I have to enter a new method (convert() ) to a previous class. this method has to access the month, year and day and return a long integer such as year*10000 + month * 100 + day. I think I did it right but not sure. I will put what I wrote and welcome any comments. Thanks!
Expand|Select|Wrap|Line Numbers
  1. void convert(int, int, int);  // my method
  2.  
  3. void Date::convert(int, int, int)
  4. {
  5.     cout << "The date is "
  6.          << setw(8) << year*10000+month*100+day;
  7. }
  8.  
  9.  
  10. firstDate.convert(4, 1, 2002);
  11.     secondDate.convert(5, 1, 2007); // I don't think these are right.
  12.  
I am a beginner so any help would be useful.
Sep 11 '07 #1
6 2097
weaknessforcats
9,208 Recognized Expert Moderator Expert
void convert(int, int, int); // my method
This is not part of any class. Where is your Date class??

void Date::convert(int, int, int)
{
cout << "The date is "
<< setw(8) << year*10000+month*100+day;
}
What are the arguments for?? You don't use them.

Also, this function doesn't convert anything. Instead it displays a string which is really part of your main().

The function should return the result of the conversion and the display of that result should be in main().
Sep 11 '07 #2
gator6688
63 New Member
Here is my class. I am new to this so I am going to make mistakes.
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. class Date
  7. {
  8.     // data declaration section
  9. private:
  10.     int month;
  11.     int day;
  12.     int year;
  13.  
  14.     //methods declaration section
  15. public:
  16.     Date();
  17.     Date(int, int, int);
  18.     void setDate(int mm, int dd, int yyyy);
  19.     void showDate();
  20.     void convert(int, int, int);
  21. };
  22.  
  23. // methods implementation section
  24. Date::Date()
  25. {
  26.     month = 7;
  27.     day = 4;
  28.     year = 2005;
  29.     cout << "From the default constructor:"
  30.          << "\n Created a new Date object with data values"
  31.          << "\n   month = " << month << " day = " << day
  32.          << "  year = " << year << "\n\n";
  33. }
  34.  
  35. Date::Date(int mm, int dd, int yyyy)
  36. {
  37.     month = mm;
  38.     day = dd;
  39.     year = yyyy;
  40.     cout << "From the overloaded constructor:"
  41.          << "\n Created a new Date object with data values"
  42.          << "\n   month = " << month << " day = " << day
  43.          << "   year = " << year << "\n\n";
  44. }
  45.  
  46. void Date::setDate(int mm, int dd, int yyyy)
  47. {
  48.     month = mm;
  49.     day = dd;
  50.     year = yyyy;
  51. }
  52.  
  53. void Date::showDate()
  54. {
  55.     cout << "The date is " << setfill('0')
  56.          << setw(2) << month << '/'
  57.          << setw(2) << day << '/'
  58.          << setw(2) << year % 100;
  59. }
  60.  
  61. void Date::convert(int, int, int)
  62. {
  63.     cout << "The date is "
  64.          << setw(8) << year*10000+month*100+day;
  65. }
  66.  
  67. int _tmain(int argc, _TCHAR* argv[])
  68. {
  69.     Date firstDate;
  70.     Date secondDate(5,1,2006);
  71.  
  72.     firstDate.showDate();
  73.     secondDate.showDate();
  74.  
  75.     secondDate.setDate(12,25,2007);
  76.     secondDate.showDate();
  77.  
  78.     firstDate.convert(4, 1, 2002);
  79.     secondDate.convert(5, 1, 2007);
  80.     return 0;
  81. }
Sep 11 '07 #3
ilikepython
844 Recognized Expert Contributor
Here is my class. I am new to this so I am going to make mistakes.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

class Date
{
// data declaration section
private:
int month;
int day;
int year;

//methods declaration section
public:
Date();
Date(int, int, int);
void setDate(int mm, int dd, int yyyy);
void showDate();
void convert(int, int, int);
};

// methods implementation section
Date::Date()
{
month = 7;
day = 4;
year = 2005;
cout << "From the default constructor:"
<< "\n Created a new Date object with data values"
<< "\n month = " << month << " day = " << day
<< " year = " << year << "\n\n";
}

Date::Date(int mm, int dd, int yyyy)
{
month = mm;
day = dd;
year = yyyy;
cout << "From the overloaded constructor:"
<< "\n Created a new Date object with data values"
<< "\n month = " << month << " day = " << day
<< " year = " << year << "\n\n";
}

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

void Date::showDate()
{
cout << "The date is " << setfill('0')
<< setw(2) << month << '/'
<< setw(2) << day << '/'
<< setw(2) << year % 100;
}

void Date::convert(int, int, int)
{
cout << "The date is "
<< setw(8) << year*10000+month*100+day;
}

int _tmain(int argc, _TCHAR* argv[])
{
Date firstDate;
Date secondDate(5,1,2006);

firstDate.showDate();
secondDate.showDate();

secondDate.setDate(12,25,2007);
secondDate.showDate();

firstDate.convert(4, 1, 2002);
secondDate.convert(5, 1, 2007);
return 0;
}[/code]
You should name your arguements:
Expand|Select|Wrap|Line Numbers
  1. void Date::convert(int year, int month, int day)
  2. {
  3.     cout << "The date is "
  4.          << setw(8) << year*10000+month*100+day;
  5. }
  6.  
And also, like WFC said, your function should return a result:
Expand|Select|Wrap|Line Numbers
  1. long int Date::convert(int year, int month, int day)
  2. {
  3.     long int date = year * 10000 + month * 100 + day;
  4.     return date;
  5. }
  6.  
  7. main
  8.     ...
  9.     ...
  10.     long int date = firstDate.convert(2000, 3, 23);
  11.     cout << date << endl;
  12.  
Sep 11 '07 #4
RRick
463 Recognized Expert Contributor
Your convert method doesn't know where it wants to get the data from. You specify parameters for convert, but you never use them. Instead convert uses with the values internal to Date (and set by the constructor or setDate).

What do you want convert to do? Should it use the values inside of Date or use what is passed to it? Your convert code does the first option. If this is the case, use setDate before you call convert. Since you're not using the parameters passed to convert, get rid of them. They are only confusing.

You still have the problem of what to do with the convert value. Currently, you are printing it out to cout. Do you need to return an integer value? If so, change convert to return an int and return the calculated value inside the method.
Sep 11 '07 #5
gator6688
63 New Member
Thanks for the help! I think I got it and understand it.
Sep 11 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
Should it use the values inside of Date or use what is passed to it? Your convert code does the first option.
I think you have this reversed. The code is:
long int Date::convert(int year, int month, int day)
{
long int date = year * 10000 + month * 100 + day;
return date;
}
This code is using the function arguiments. Local variables ar eused in preference to member variables.

The code should be:
Expand|Select|Wrap|Line Numbers
  1. long int Date::convert()
  2. {
  3.     long int date = this->year * 10000 + this->month * 100 + this->day;
  4.     return date;
  5. }
  6.  
Sep 12 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
5343
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
48
3182
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
4
5449
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
17
71348
by: Terry Jolly | last post by:
New to C# ---- How do I convert a Date to int? In VB6: Dim lDate as long lDate = CLng(Date) In C#
1
2448
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
7
3939
by: erekose666 | last post by:
I need a java prog to do the following: Create class Date with the following capabilities: a) Output the date in multiple formats, such as: MM/DD/YYYY June 14, 2005 DDD YYYY b) Use...
1
1237
by: nrasch | last post by:
I am coding an application in VB.Net 2005 where objects of a custom class are saved/retrieved into/out of a DB. As my application moves into its 2nd version I have to add new methods and properties...
2
3135
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
4
3939
tolkienarda
by: tolkienarda | last post by:
hi all I am working on a php driven database program for a literacy program, it will allow them to keep track of classes and students, the part i am strugling with is adding new classes, the...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.