473,803 Members | 4,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(cons t 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<<(ostr eam& os,const Year& y);
private:
int* year;
};

ostream& operator<<(ostr eam& 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.IsLeapY ear)
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)c ontinue;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
14 4765

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

like:

bool is_leapyear(Yea r 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@hg nohneb read backward> wrote in message
news:43******** *************** @news.optusnet. com.au...
to encapsulate, you should really make the leap year identification a
non-friend, non-member, standalone function.

like:

bool is_leapyear(Yea r 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
1783
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? Bryan Jones NightDaemon Enterprises nightdaemon.co.nr
13
2476
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 month. We are assuming that the year will be valid 4 digit integer. So you don't have to think much about that(in terms of validation) except for the month of February. If the month is February, then you have to check whether that year is Leap...
22
4453
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
2968
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 program will make the decision if it is a leap year or not. In addition, I kind of understand that there are certain rules to calculating leap years. For instance, if the year is divisable by 4 and if the year is not divisable by 100 but is...
3
3651
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 = year % 4 if result > 0: print "It is NOT a leap year" else: print "It IS a leap year"
12
7704
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 form with a single text box and using an alert dialog box stating if the entered year is a standard or leap year. Code so far: <HTML> <HEAD> <TITLE>Is it a leap year?</TITLE> </HEAD> <SCRIPT LANGUAGE="JAVASCRIPT>
37
14970
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: case 8: case 11: case 14: case 17: return 1; default: return 0;
5
3494
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
2375
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 date from calender and clicks search button it will fetch data from database. But what I need is as soon the user clicks the date, it should fetch data for that date from database. How to do it. Here is the code I am using <html> <script...
0
9699
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
9562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10309
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
10289
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,...
0
10068
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9119
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6840
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
5496
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...
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.