G
gorda
Hello,
I am playing around with operator overloading and inheritence,
specifically overloading the + operator in the base class and its
derived class.
The structure is simple: the base class has two int memebers "dataA",
"dataB". The derived class has an additional int member "dataC". I am
simply trying to overload the + operator so that 'adding' two objects
will sum up the corresponding int members.
Can someone tell me if I'm overloading the + operator correctly in the
derived class, or if there is a better way to do it?
#include <iostream>
using namespace std;
class base
{
public:
//base int members
int dataA,dataB;
//default constructor
base():dataA(0),dataB(0)
{}
//overloaded constructor
base(int x, int y): dataA(x), dataB(y)
{}
//operator + in the base class
base operator+(const base& rhs) const
{
base tmp;
tmp.dataA = this->dataA + rhs.dataA;
tmp.dataB = this->dataB + rhs.dataB;
return tmp;
}
};
class derived: public base
{
public:
//additonal derived class int member
int dataC;
//default constructor
derived():base()
{
dataC=0;
}
//overloaded constructor
derived(int x, int y, int z):base(x,y)
{
dataC=z;
}
//copy constructor
derived(const derived& t): base(t)
{
this->dataC = t.dataC;
}
//operator + in the derived class
derived operator+(const derived &rhs) const
{
derived tmp;
base *pbase;
//!!!****is this the best way for my purpose?****!!!//
pbase=&tmp;
*pbase = base:perator+(rhs);
tmp.dataC = this->dataC + rhs.dataC;
return tmp;
}
//print values
void getValues()
{
cout << dataA <<" "<< dataB <<" "<< dataC << endl;
}
};
int main()
{
derived a(10,20,30);
derived b(1,2,3);
derived c;
c = a + b;
c.getValues();
}
The output is as expected: 11 22 33
If someone can suggest a better way to overload the + operator in the
derived class if I haven't done it correctly, i'd appreciate any
responses.
-Thanks
Gorda Smith
I am playing around with operator overloading and inheritence,
specifically overloading the + operator in the base class and its
derived class.
The structure is simple: the base class has two int memebers "dataA",
"dataB". The derived class has an additional int member "dataC". I am
simply trying to overload the + operator so that 'adding' two objects
will sum up the corresponding int members.
Can someone tell me if I'm overloading the + operator correctly in the
derived class, or if there is a better way to do it?
#include <iostream>
using namespace std;
class base
{
public:
//base int members
int dataA,dataB;
//default constructor
base():dataA(0),dataB(0)
{}
//overloaded constructor
base(int x, int y): dataA(x), dataB(y)
{}
//operator + in the base class
base operator+(const base& rhs) const
{
base tmp;
tmp.dataA = this->dataA + rhs.dataA;
tmp.dataB = this->dataB + rhs.dataB;
return tmp;
}
};
class derived: public base
{
public:
//additonal derived class int member
int dataC;
//default constructor
derived():base()
{
dataC=0;
}
//overloaded constructor
derived(int x, int y, int z):base(x,y)
{
dataC=z;
}
//copy constructor
derived(const derived& t): base(t)
{
this->dataC = t.dataC;
}
//operator + in the derived class
derived operator+(const derived &rhs) const
{
derived tmp;
base *pbase;
//!!!****is this the best way for my purpose?****!!!//
pbase=&tmp;
*pbase = base:perator+(rhs);
tmp.dataC = this->dataC + rhs.dataC;
return tmp;
}
//print values
void getValues()
{
cout << dataA <<" "<< dataB <<" "<< dataC << endl;
}
};
int main()
{
derived a(10,20,30);
derived b(1,2,3);
derived c;
c = a + b;
c.getValues();
}
The output is as expected: 11 22 33
If someone can suggest a better way to overload the + operator in the
derived class if I haven't done it correctly, i'd appreciate any
responses.
-Thanks
Gorda Smith