473,662 Members | 2,454 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 2103
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(i nt, int, int)
{
cout << "The date is "
<< setw(8) << year*10000+mont h*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(i nt 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(i nt, int, int)
{
cout << "The date is "
<< setw(8) << year*10000+mont h*100+day;
}

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

firstDate.showD ate();
secondDate.show Date();

secondDate.setD ate(12,25,2007) ;
secondDate.show Date();

firstDate.conve rt(4, 1, 2002);
secondDate.conv ert(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(i nt 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
5364
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 calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
48
3223
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 "huge" Indy is both as a project, in support, development, and use. But Indy is new to the .net world. Indy is a HUGE library implementing over 120 internet protocols and standards and comes with complete source. Its an open source project, but not...
4
5476
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 dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last selection and displays actual articles for display. Adding the controls and getting everything...
17
71409
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
2463
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 guy's Javascript "combo box" - http://sandy.mcarthur.org/javascript/select/select.html. It allows my users (when I get some!) to select from a list of preexisting options and also to add a new one by clicking on "add new". Essentially it's a select...
7
3946
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 overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third...
1
1249
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 into my custom class. How does one go about adding new methods and/or properties to a custom class and then updating existing objects of that class w/out breaking everything? Ex: Pull existing object from DB -> Convert to new version of...
2
3147
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 then the calender gone actually i want if i click outside off the calender then it should me removed ..How kan i do this ... Pls inform me as early as possible .. I am waiting for ur quick replay ...Here i attached the source code .... <!DOCTYPE...
4
3955
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 add_class page looks like: <body> ADD CLASS<br> Class Title: <input type="text" value="class_title"><br> Class Name: <input type="text" value="class_name">(Must be Unique)<br> <input type="checkbox" name="children" value="children">Children's...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8856
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8762
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.