See the following code:
#include<iostream>
using namespace std;
class ex
{
int i;
public:
ex(int x){i=x;}
void operator-()
{
i=-i;
}
int geti(){return i;}
};
class derived:public ex
{
int j;
public:
derived(int x,int y):ex(x){j=y;}
void operator-()
{
j=-j;
}
int getj(){return j;}
};
int main()
{
derived ob(1,2);
-ob;
cout<<ob.geti();
cout<<"\n"<<ob.getj();
}
output:
1
-2
Process returned 0 (0x0) execution time : 0.901 s
Press any key to continue.
I define the -
operator in both the base and derived classes, but -ob;
calls only the operator of the derived class. So how to also change the i
field to -i
(calling the operator in the base class).
Do I need any explicit function to achieve this?