Class date objects/operator overloading

Overloading an operator to find differance in dates

Page 1 of 1

6 Replies - 6558 Views - Last Post: 01 June 2007 - 12:06 PM Rate Topic: -----

#1 varon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-February 07

Class date objects/operator overloading

Post icon  Posted 01 June 2007 - 05:01 AM

hey all
i am juss kinda stuck in doing my assignment and i am almost through doin it except the code responsible for calculating the difference between two date objects
could anyone help me out here that wuill be really appreciate

here my code
#ifndef DATE_H
#define DATE_H

class Date {

private:

	int day;
	int month;
	int year;

public:
	Date();
	Date(int a,int b, int c);
	void Display();
	void operator +=(int a); //Add number of days to date object



	   Date operator-(const Date &x,const Date &y); //****

	//**** could i use anything like this for calculating the differences between two date ?? objects


	bool  valid(const int year,const int month, const int day); 
	~Date();
};
#endif 
#include<iostream>
using namespace std;
#include"Date.h"

Date::Date()
{	month=1;
	 day=1;
	 year=1;
}

Date::Date(int a,int b, int c)
{
	
	if (!valid(a,b,c))
		cout << " Date is invalid " <<endl;

	else 
		year=a;
		month=b;
		day=c;

}
bool Date::valid(const int year,const int month, const int day)  
{

if (year<0) 
return false; 
if (month >12 || month <1) 
return false;
if (day >31 || day <1)
return false;
if ((day ==31 &&( month ==2 || month ==4 || month ==6 || month ==9 || month ==11) ) )
return false;
if (day ==30 && month ==2) 
return false;
if (year <2000){
if ((day ==29 && month ==2) && !(((year-1900)%4==0)) )
return false;
}
if (year >2000){ 
if ((day ==29 && month ==2) && !(((year-2000)%4==0)))
return false;
}
return true;
}


void Date::Display()
{
   static char *name[] = {"null", "January", "February", "March", "April",
			"May", "June", "July", "August", "September", "October",
			"November", "December" };

   cout <<'\n' << name[month] << ' ' << day << "," << year << '\n';
}

Date::~Date()
{
	cout<< "Static date objects are deleted "<<endl;
}

 void Date::operator+=(int a)
{
	day+=a;
	const int month_days[ ] = {0, 31, 28, 31, 30, 31, 30 , 31, 31 , 30, 31, 30, 31}; 
	if (day == month_days[month]) // is it last day of the month?
   {  day = 1;
	  month = month % 12 + 1;
   }
   else day++;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Class date objects/operator overloading

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Class date objects/operator overloading

Posted 01 June 2007 - 05:34 AM

Quote

could i use anything like this for calculating the differences between two date ?? objects


Yes. -- I am too tired to look at it now, but it should not be all that hard.
Was This Post Helpful? 0
  • +
  • -

#3 varon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-February 07

Re: Class date objects/operator overloading

Posted 01 June 2007 - 05:53 AM

View PostNickDMax, on 1 Jun, 2007 - 05:34 AM, said:

Quote

could i use anything like this for calculating the differences between two date ?? objects


Yes. -- I am too tired to look at it now, but it should not be all that hard.





hey thnx but wen i try to build my project it is giving me error sayin operator - having too much parameters ???

wuts up with that ?
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Class date objects/operator overloading

Posted 01 June 2007 - 06:13 AM

I believe that Date operator-(const Date &x, const Date &y); would have to be a global function. To overload using a member function use
const Date operator-(const Date &x);
Was This Post Helpful? 0
  • +
  • -

#5 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: Class date objects/operator overloading

Posted 01 June 2007 - 06:54 AM

So, using the advice of NickDMax, these two should be realy close:
Date operator-(Date &a, Date &b) {
  Date t;
  t.year = a.year - b.year;
  t.month = a.month - b.month;
  t.day = a.day - b.day;  
  return t;
}	 	  
  
Date operator-(Date &b) {
  Date t;
  t.year = year - b.year;
  t.month = month - b.month;
  t.day = day - b.day;  
  return t;
}  


Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Class date objects/operator overloading

Posted 01 June 2007 - 11:45 AM

Thanks Aj. I was much to tired to think when I posted earlier. One note on the above. The reason that + - * / operators are declared with that initial const keyword:

const Date operator-(Date &b);

is to support operator chaining A-B-C and to ensure that nonsensical syntax like (A - B) = C is not allowed. With out the const keyword there (A-B) = C or (A-B)++ will actually compile since the operator returns a L-Value (which it needs to do to support operator chaining). It really is not a big deal, it is just one of those small technical points.

The following compiles just fine:
#include <iostream>

class Foo
{
private:
	int total;
public:
	Foo(int i = 0) : total(i) { }
	
	//The two const keywords here just say that I will not be changing
	// either the C nor this object.
	Foo &operator-(const Foo &C) const
	{
		//create a dynamic object...
		Foo *NewFoo = new Foo(total - C.total);
		return *NewFoo;
	}
	
	//postfix ++ operator
	void operator++(int x)
	{
		total++;
	}
	
	//Just a 
	void show() { std::cout <<total; }
};

int main()
{
	Foo A(10);
	Foo B(5);
	Foo C;

	(A - B) = C; //This is a potential memory leak as all referance to the object (A-B) is lost...
	(A - B)++; //Ditto
	(A - B).show(); //Ditto... but using the const keyword will not fix this one.
	
	return 0;
}


but if you add a const keyword to the beginning of the operator then (A - B ) = C; and (A - B )++; both produce errors.
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Class date objects/operator overloading

Posted 01 June 2007 - 12:06 PM

umm.... well thinking about it a little...
Thinking about the above example with the class Foo... The memory leak is not in the expression (A - B ) = C; it is in returning a dynamic object. As long as one is using new to generate your return object this is going to leak like a sieve. The expression C = C - A - B; would also be a memory leak since the intermediate C - A produces an object, and then (C - A) - B produces and object, and the default assignment operator copies (C - A) - B into the C object. So that leaves two objects that have been lost.

Guess I was not thinking clearly when I made that example. The operator should have been defined as
	Foo operator-(const Foo &C) const
	{
		Foo NewFoo(total - C.total);
		return NewFoo;
	}
which would not produce the memory leak, but would still support nonsensical syntax like (A - B ) = C.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1