473,395 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help on OO leap year calculator

Hi!!! I need some help on a project I'm that calculates leap years; I'm
getting errors that I have no idea what they mean; here's the code:

#include <iostream>
#include <cstdlib>
using namespace std;

class Year
{
public:
Year(int y=0):year(new int) *year(y){}
~Year(){delete year;}
Year(const Year& rhs)*year(rhs.*year){}
Year operator=(const Year& rhs)
{
if(this!=&rhs)
{
*year=rhs->year;
delete year;
}
else return this;
}
void SetYear(int y):year(new int),*year(y){}
int GetYear()const{return *year;}
bool IsLeapYear(const Year& y)const
{
if(y->year%400==0&&y->year%4==0)
return true;
else if(y->year%100)
return false;
else return false
}
friend ostream& operator<<(ostream& os,const Year& y);
private:
int* year;
};

ostream& operator<<(ostream& os,const Year& y)
{
os << y.GetYear();
return os;
}
}
int main()
{
int date;
for(;;)
{
cout << "Enter a year: " << endl;
cin >> date;
Year year(date);
if(year.IsLeapYear)
cout << year << " is a leap year. " << endl;
else
cout << year << " is not a leap year. " << endl;
bool cont;
cout << "Continue?[1=yes|0=no]: " << endl;
cin >> cont;
if(cont==true)continue;else break;
system("PAUSE");
return 0;
}
And as a side note, it's not letting me use constructor inizilization
syntax. Can you help me (this isn't homework)? Thanks for your help!!!

Sep 18 '05 #1
14 4733
Daw
And what erros u get?

Sep 18 '05 #2
Daw
And I'm sure this _is_ a school work ;>

Sep 18 '05 #3
No it's not; my friend, Jonathon Mastron at BIOLA, asked me to it for
him. My school work is trig stuff, not calendars and dates. Just help
me please.

Sep 18 '05 #4
OK, I fixed it; it compiles and runs now; here's the code:

#include <iostream>
#include <cstdlib>
using namespace std;

class Year
{
public:
Year(int y=0): year(y){}
~Year(){}
Year(const Year& rhs):year(rhs.year){}
Year operator=(const Year& rhs)
{
if(this!=&rhs)
year=rhs.year;
return *this;
}
void SetYear(int y){year=y;}
int GetYear(void)const{return year;}
bool IsLeapYear(void)const
{
if(year%400==0&&year%4==0)
return true;
else if(year%100)
return false;
else return false;
}
friend ostream& operator<<(ostream& os,const Year& y);
private:
int year;
};

ostream& operator<<(ostream& os,const Year& y)
{
os << y.GetYear();
return os;
}

int main()
{
int date;
for(;;)
{
cout << "Enter a year: " << endl;
cin >> date;
Year year(date);
if(year.IsLeapYear())
cout << year << " is a leap year. " << endl;
else
cout << year << " is not a leap year. " << endl;
bool cont;
cout << "Continue?[1=yes|0=no]: " << endl;
cin >> cont;
if(cont==true)continue;else break;
}
system("PAUSE");
return 0;
}

Anyway I can make it more efficient? And other than calculating leap
years, what other uses can I have for this class? And what other ops
should I overload? Thanks for the help!!!

Sep 18 '05 #5
I'm trying to overload operator>>, but my program keeps aborting;
here's the code for operator>>:

istream& operator>>(istream& is,const Year& y)
{
is >> y.year;
return is;
}

Why does it keep aborting?

Sep 18 '05 #6
On 18 Sep 2005 15:35:16 -0700, "Protoman" <Pr**********@gmail.com> wrote:
I'm trying to overload operator>>, but my program keeps aborting;
here's the code for operator>>:

istream& operator>>(istream& is,const Year& y)
{
is >> y.year;
You are trying to modify a const value. In fact, the const qualifier is
not appropriate for this kind of function.
return is;
}

Why does it keep aborting?


If it aborts at that region, then perhaps there might be a logic error in
your code. Best to find some form of debugger.

Sep 19 '05 #7
I fixed it and it works.

Sep 19 '05 #8
to encapsulate, you should really make the leap year identification a
non-friend, non-member, standalone function.

like:

bool is_leapyear(Year year){/*...*/}

ben
Sep 19 '05 #9

Protoman wrote:
OK, I fixed it; it compiles and runs now; here's the code:
..
bool IsLeapYear(void)const
{
if(year%400==0&&year%4==0)
return true;
else if(year%100)
return false;
else return false;
}


This function is clearly not correct. It will return false for nearly
every year that is a leap year. The first clue that something is amiss,
is the fact that it tests whether a year is divisible by 4 after it
knows it is divisible by 400. Anyway, here is a corrected version:

bool IsLeapYear() const
{
if ( year%4 ) // not divisible by 4
return false;
if ( year%100 ) // not a century year
return true;
if ( year%400 ) // not a fourth century
return false;
return true;
}

Also, I would work on writing more readable code. The layout of the
code in the original program is atrocious. Do spaces cost extra?

Greg

Sep 19 '05 #10

"benben" <moc.liamtoh@hgnohneb read backward> wrote in message
news:43***********************@news.optusnet.com.a u...
to encapsulate, you should really make the leap year identification a
non-friend, non-member, standalone function.

like:

bool is_leapyear(Year year){/*...*/}

ben


How does that help anything? How does it provide encapsulation, when it's
now an external function which has to know (and have access to) the
internals of the Year class?

And the year member of that object is a private variable, so if this is a
non-friend, it can't possibly access the member.

(Also, you're passing the Year object by value, which requires a copy for no
apparent reason.)

-Howard


Sep 19 '05 #11
Your IsLeapYear method is wrong, e.g. 2004 will fail: 2004%400 == 4,
2004%4 == 0, so the first if will fail, 2004%100 == 4, so the else if
will fail, so the method will return false.

see http://www.faqs.org/faqs/calendars/faq/part1/ for the correct
method.

Sep 19 '05 #12
Howard wrote:

"benben" <moc.liamtoh@hgnohneb read backward> wrote in message
news:43***********************@news.optusnet.com.a u...
to encapsulate, you should really make the leap year identification a
non-friend, non-member, standalone function.

like:

bool is_leapyear(Year year){/*...*/}

ben
How does that help anything?


Helps to keep the interface of Year small. That makes for fewer things to
care about when you actually try to reap the advantages of encapsulation
and set out to change the implementation. You will not need to even look at
is_leapyear().

How does it provide encapsulation, when it's
now an external function which has to know (and have access to) the
internals of the Year class?
Maybe I am imagination impaired, but why would is_leapyear() need to know
"the internals" of Year. Don't you think that an ordinary call to a public
member function like

signed long GetYear() const;

would suffice?

And the year member of that object is a private variable, so if this is a
non-friend, it can't possibly access the member.
So you decided that a GetYear() function is inherently bad and that your
Year class will never ever allow any client to actually figure out which
year a given object of type Year represents. That, indeed, is very tight
encapsulation.

Admittedly, I have a hard time imagining a Year class to have interesting
internals (the only invariant I can fathom is that the year cannot be 0).
However, dreaming up an interface of Year that would not allow non-friend
clients to tell whether the year is divisible by 4 and by 400 is beyond me.

(Also, you're passing the Year object by value, which requires a copy for
no apparent reason.)


Good point. However, what would you think that sizeof(Year) should be?
Best

Kai-Uwe Bux

Sep 19 '05 #13

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:dg**********@murdoch.acc.Virginia.EDU...
Howard wrote:

Admittedly, I have a hard time imagining a Year class to have interesting
internals (the only invariant I can fathom is that the year cannot be 0).
However, dreaming up an interface of Year that would not allow non-friend
clients to tell whether the year is divisible by 4 and by 400 is beyond
me.


Looking more at his class, I'd have to agree. I see no reason for the class
in the first place, really. A year is simply an integer. Why does it need
functionality? Making the internal value a pointer to an integer is even
more needless complexity.

-Howard
Sep 19 '05 #14
Protoman wrote:
I fixed it and it works.


C++ language issues aside, your leap year calculation is flawed. For
example, according to your code, 1996 is not a leap year.

Also, I'm not sure if a complete class isn't a little overkill, unless
you do more with the year than figuring out if it's a leap year.

This formula should work better:

bool IsLeapYearFixed( const unsigned int year )
{
if ( 0 != year % 4 )
return false;

if ( 0 == year % 400 )
return true;

if ( 0 == year % 100 )
return false;

return true;
}

Cheers,
Andre

Sep 19 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Se'noj | last post by:
Hi all, In a program I am writing, I need to convert a date stored as an integer number of days into a standard gregorian format, with day zero being 1/1/1800. Can anyone help me with this? ...
13
by: vgame64 | last post by:
Hi, I have been struggling with writing a program for a few hours. The requirements are that: """You will be writing a program which will determine whether a date is valid in terms of days in that...
22
by: deepak.rathore | last post by:
can someone give the regular expr. to validate leap yr like 02/29/2000,02/29/00 Thanks
8
by: rn216_ccc | last post by:
Hi there all, I found a web site,. http://www.onlineconversion.com/leapyear.htm, where it can give a list of leap years by within the given range by the user, or the user may enter a year and the...
3
by: xGx | last post by:
Hi please could someone help. i have created this programming so you can work out if there is a LEAP YEAR. But i need it to handle centuries. heres the code def isLeapYear (year): result =...
12
by: Brigitte Behrmann | last post by:
Can anyone assist with my code. I just cannot get this to work: I have to write a script that allows the user to enter a year & then determine whether it is a leap year or not. The request is for a...
37
by: mazwolfe | last post by:
I'm new here, so excuse me if my style is incorrect. Can anyone come up with a better method for this calculation? Code: int is_leap(int year) { switch (year % 19) { case 0: case 3: case 6:...
5
by: jimix | last post by:
here's what i have. using microsoft studio #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main( void ) { int year, b, c, e; b = 4; c = 100;
7
by: gubbachchi | last post by:
Hi all, In my application I need to display the data fetched from mysql database after the user selects date from javascript calender. I have written the code in which after the user selects the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...

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.