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! -
void convert(int, int, int); // my method
-
-
void Date::convert(int, int, int)
-
{
-
cout << "The date is "
-
<< setw(8) << year*10000+month*100+day;
-
}
-
-
-
firstDate.convert(4, 1, 2002);
-
secondDate.convert(5, 1, 2007); // I don't think these are right.
-
I am a beginner so any help would be useful.
6 2103
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().
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;
-
}
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: -
void Date::convert(int year, int month, int day)
-
{
-
cout << "The date is "
-
<< setw(8) << year*10000+month*100+day;
-
}
-
And also, like WFC said, your function should return a result: -
long int Date::convert(int year, int month, int day)
-
{
-
long int date = year * 10000 + month * 100 + day;
-
return date;
-
}
-
-
main
-
...
-
...
-
long int date = firstDate.convert(2000, 3, 23);
-
cout << date << endl;
-
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.
Thanks for the help! I think I got it and understand it.
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: -
long int Date::convert()
-
{
-
long int date = this->year * 10000 + this->month * 100 + this->day;
-
return date;
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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#
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |