Adding two different Objects by overloading operator+ C++
<p>I've been trying to figure out how to add a private member from Object A, to a private member from Object B.</p>

<p>Both Cat and Dog Class's inheriate from the base class Animal. I have a thrid class 'MyClass', that I want to inheriate the private members of the Cat and Dog class. So in MyClass, I have a friend function to overload the + operator. THe friend function is defined as follows:</p>

<pre><code>MyClass operator+(const Dog &amp;dObj, const Cat &amp;cObj);
</code></pre>

<p>I want to access dObj.age and cObj.age within the above function, invoke by this statement in main:</p>

<pre><code>mObj = dObj + cObj;
</code></pre>

<p>Here is the entire source for a complete reference into the class objects:</p>

<pre><code>   #include &lt;iostream&gt;
   #include &lt;vld.h&gt;

   using namespace std;

   class Animal
   {
   public :
    Animal() {};
    virtual void eat()  = 0 {};
    virtual void walk() = 0 {};
   };

   class Dog : public Animal
   {
   public :
    Dog(const char * name, const char * gender, int age);
    Dog() : name(NULL), gender(NULL), age(0) {};
    virtual ~Dog();
    void eat();
    void bark();
    void walk();

   private :
    char * name;
    char * gender;
    int age;
   };

   class Cat : public Animal
   {
   public :
    Cat(const char * name, const char * gender, int age);
    Cat() : name(NULL), gender(NULL), age(0) {};
    virtual ~Cat();
    void eat();
    void meow();
    void walk();
   private :
    char * name;
    char * gender;
    int age;
   };

   class MyClass : private Cat, private Dog
   {
   public :
    MyClass() : action(NULL) {};
    void setInstance(Animal &amp;newInstance);
    void doSomething();

    friend MyClass operator+(const Dog &amp;dObj, const Cat &amp;cObj);

   private :
    Animal * action;
   };

   Cat::Cat(const char * name, const char * gender, int age) : 
     name(new char[strlen(name)+1]), gender(new char[strlen(gender)+1]), age(age)
   {
    if (name)
    {
     size_t length = strlen(name) +1;
     strcpy_s(this-&gt;name, length, name);
    }
    else name = NULL;
    if (gender)
    {
     size_t length = strlen(gender) +1;
     strcpy_s(this-&gt;gender, length, gender);
    }
    else gender = NULL;
    if (age)
    {
     this-&gt;age = age;
    }
   }
   Cat::~Cat()
   {
    delete name;
    delete gender;
    age = 0;
   }
   void Cat::walk()
   {
    cout &lt;&lt; name &lt;&lt; " is walking now.. " &lt;&lt; endl;
   }
   void Cat::eat()
   {
    cout &lt;&lt; name &lt;&lt; " is eating now.. " &lt;&lt; endl;
   }
   void Cat::meow()
   {
    cout &lt;&lt; name &lt;&lt; " says meow.. " &lt;&lt; endl;
   }
   Dog::Dog(const char * name, const char * gender, int age) : 
     name(new char[strlen(name)+1]), gender(new char[strlen(gender)+1]), age(age)
   {
    if (name)
    {
     size_t length = strlen(name) +1;
     strcpy_s(this-&gt;name, length, name);
    }
    else name = NULL;
    if (gender)
    {
     size_t length = strlen(gender) +1;
     strcpy_s(this-&gt;gender, length, gender);
    }
    else gender = NULL;
    if (age)
    {
     this-&gt;age = age;
    }
   }
   Dog::~Dog()
   {
    delete name;
    delete gender;
    age = 0;
   }
   void Dog::eat()
   {
    cout &lt;&lt; name &lt;&lt; " is eating now.. " &lt;&lt; endl;
   }
   void Dog::bark()
   {
    cout &lt;&lt; name &lt;&lt; " says woof.. " &lt;&lt; endl;
   }
   void Dog::walk()
   {
    cout &lt;&lt; name &lt;&lt; " is walking now.." &lt;&lt; endl;
   }


   void MyClass::setInstance(Animal &amp;newInstance)
   {
    action = &amp;newInstance;
   }
   void MyClass::doSomething()
   {
    action-&gt;walk();
    action-&gt;eat();
   }


   MyClass operator+(const Dog &amp;dObj, const Cat &amp;cObj)
   {
    MyClass A;
    //dObj.age;
    //cObj.age;
    return A; 
   }

   int main()
   {
    MyClass mObj;
    Dog dObj("B", "Male", 4);
    Cat cObj("C", "Female", 5);

    mObj.setInstance(dObj); // set the instance specific to the object.
    mObj.doSomething();  // something happens based on which object is passed in
    dObj.bark();

    mObj.setInstance(cObj);
    mObj.doSomething();
    cObj.meow();

    mObj = dObj + cObj;

    return 0;
   }
</code></pre>
