Hi,
Im trying to write some code which adds the combined height and weight of two dogs, returns the result and then displays it..The problem is, the result returned is wrong (i think the operation is just adding two memory addresses)..The =operator code is returning rubbish as well, i think the problem is with the operator= function because when i remove it the other works fine..
Please see attached code
Thanks
//DOG: main.cpp
#include "Dog.h"
int main()
{
Alsation a(20, 50);
Alsation b(30, 50);
Alsation c;
c = a + b;
c.display();
d = a;
d.display();
}
// Dog.h
#include "Animal.h"
#ifndef Dog_h
#define Dog_h
using namespace std;
class Dog{
protected:
int ID;
public:
int getID(){return ID;} //17. Inline methods..
void setID(int newID){ID=newID;}
};
class Retriever;
class Alsation: public Dog{
private:
int height, weight;
public:
Alsation(int aHeight, int aWeight);
Alsation(){};
friend void mixBreed(Alsation a, Retriever b);
Alsation operator+ (Alsation );
Alsation operator= (const Alsation &);
// bool Alsation::operator == (Alsation x);
void display();
};
#endif
// Dog.cpp
#include "Dog.h"
using namespace std;
Alsation::Alsation(int aHeight, int aWeight):
height(aHeight), weight(aWeight){}
Alsation Alsation::operator+ ( Alsation x){
int h = height + x.height;
int w = weight + x.weight;
return Alsation(h,w);
}
void Alsation::display(){
cout <<"Combined height = " << height << endl;
cout <<"Combined weight = " << weight << endl;
}
Alsation Alsation::operator= (const Alsation &y){
int h = y.height;
int w = y.weight;
return *this;}