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;}

Your operators should have the following prototypes: Alsation operator + ( const Alsation& x ); Alsation& operator = ( const Alsation& y ); Hope this helps.

thanx duoas, unfortunately it still doesnt work..

>> unfortunately it still doesnt work..

I'm guessing that lines 83 and 84 are still wrong (assigning to local temporaries h and w ).

Furthermore, you also have to explicitly call the base class' operator =() in order to make the assignment fully work, this doesn't happen automatically.

mitrmkar

Thanks for reply..Why do local temps work for operator+ but not operator=? Can you please explain..Also, how exactly do i call the function?

Thanks again

@OP
You are assigning values to locals. In your operator func. the LHS is your "this" pointer. You should be assigning to this->h & this->w;

>>you also have to explicitly call the base class' operator =()
Nope. The default assignment operator(which shouldn't have been there in first place in C++) for the class will get suppressed automatically.

>>Why do local temps work for operator+ but not operator=
They do work, you just coded 'em wrong.

int h = y.height;
      int w = y.weight;
return *this;

You assign temps some values, then return *this. You DO NOT modify the values of *this.

Alsation Alsation::operator+ ( Alsation x){
      int h = height + x.height;
      int w = weight + x.weight;
      return Alsation(h,w);
      }

Here you've changed the values of *this(implicit), hence operator()+ works fine.

>> you also have to explicitly call the base class' operator =()

Nope. The default assignment operator(which shouldn't have been there in first place in C++) for the class will get suppressed automatically.

I don't quite get what you are saying. To be clear, I was saying that in the derived class, you need to explicitly call the operator=() of the base class. If you don't, the base class members will not be changed by the assignment.

struct derived : base
{
  derived & operator = (const derived & other)
  {
    if(this != &other)
    {
       // Do the base class first ..
       base::operator = (other);

       // .. handle this class' members ..
    }
    return *this;
  }
};

What you've stated is true however I interpreted it the otherwise(i.e. Using overloaded = outside class, not considering the derived scenario). Apologies.

struct base
{
   base& operator =(const  base &rhs) {...}
   .
   .
};
base a,b;
a.operator=(b);   //<-- I meant this can be stated as a=b, since overloaded= hides the default assignment operator
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.