I have a class called Date and another called DateTime that is derived from Date. The class Date has a provision to overload the + operator. I am trying to overload the + operator in the DateTime class as well.
Now, according to me, the overloading of the + operator in the DateTime class should overload the + operator in the Date class, right? But when I try to do it, it is throwing me an error: "DateTime DateTime::operator+(int) cannot be overloaded."
Date Date::operator+(int extradays){
Date temp;
int rem;
temp.dd=dd+extradays;
switch(mm){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:if(temp.dd>31){
temp.dd=temp.dd%31;
temp.mm=mm+1;
}
break;
case 12: if(temp.dd>31){
temp.dd=temp.dd%31;
temp.mm=1;
temp.yy++;
}
break;
case 4:
case 6:
case 9:
case 11:if(temp.dd>30){
temp.dd=temp.dd%30;
temp.mm=mm+1;
}
break;
case 2: if(yy%4==0)
if(temp.dd>29){
temp.dd=temp.dd%29;
temp.mm=mm+1;
}
elseif(temp.dd>28){
temp.dd=temp.dd%28;
temp.mm=mm+1;
}
}
return temp;
}
So for something like:
Date d;
d=d+3;
3 days will be added to the date.
And here's the code to overload the + operator in the derived class:
Is it that you want to override operator+ instead of overloading it?
Overloaded functions have the same name but different arguments.
Overridden functions have the same name and the same arguments, but they have different definitions in the base class and the derived class.
the overloading of the + operator in the DateTime class should overload the + operator in the Date class
The derived class is affected by the base class and the base class is independent of the derived class. You can think of inheritance as copying all non-private functions from the base class and pasting them in the derived class. The derived class will then 'inherit' functions of the base class (which can be overridden) but there will be no change in the base class. Then you can add some member functions and data members of your own to the derived class otherwise it will be almost an exact copy of the base class. These members will not affect the base class.
I don't know what the error message means as I haven't seen the code. So please post code.
@Eklavya: I made a mistake in my original post. I meant that shouldn't the derived class' overloaded operator be called instead of the base class' one when manipulating an object that belongs to the derived class?
#include <iostream>
class A
{
public:
A operator+(int rhs)
{
this->i += rhs;
std::cout<<"A+"<<std::endl;
return *this;
}
private:
int i;
};
class B: public A
{
public:
B operator+(int rhs)
{
this->d += rhs;
std::cout<<"B+"<<std::endl;
return *this;
}
private:
int d;
};
int main()
{
B b;
b = b + 1;
A a;
a = a + 1;
std::getchar();
return 0;
}
EDIT: Yes, there should be some more consts in there, but I'm lazy.
Which is what I expected it to print. Peter had a problem which for some reason would not allow him to define the operator+ for his derived class, and I simplified his code to get to the root of the problem.
> I meant that shouldn't the derived class' overloaded operator be called instead of the base class' one
> when manipulating an object that belongs to the derived class?
For that, the overloaded operator has to be a virtual function that is overridden in a derived class. This is fine for operator+= which returns a reference, but problematic for operator+ which returns a value.
In general, run-time polymorphism, static typing and value semantics don't mix well - you will have to let go of at least one of them. It has been a perennial problem in languages like C++ and Java that have kludged run-time polymorphism on top of C.
It works! It was such a stupid mistake that I had made and it only came to my attention after playing a bit of Slender. I had declared the + operator overloading function twice in the same class.
Moral: When stuck, take a break and try again with a fresh mindset.
Many thanks for all your help though.
@BlackSheep: You've saved me a lot of embarrassment by catching that bug.
@JLBorges: I found your last post to be very helpful.