开发者

Adding two different Objects by overloading operator+ C++

I've been trying to figure out how to add a private member from Object A, to a private member from Object B.

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:

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

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

mObj = dObj + cObj;

Here is the entire source for a complete reference into the class objects:

   #include <iostream>
   #include <vld.h>

   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 &newInstance);
    void doSomething();

    friend MyClass operator+(const Dog &dObj, const Cat &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->name, length, name);
    }
    else name = NULL;
    if (gender)
    {
     size_t length = strlen(gender) +1;
     strcpy_s(this->gender, length, gender);
    }
    else gender = NULL;
    if (age)
    {
     this->age = age;
    }
   }
   Cat::~Cat()
   {
    delete name;
    delete gender;
    age = 0;
   }
   void Cat::walk()
   {
    cout << name << " is walking now.. " << endl;
   }
   void Cat::eat()
   {
    cout << name << " is eating now.. " << endl;
   }
   void Cat::meow()
   {
    cout << name << " says meow.. " << 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->name, length, name);
    }
    else name = NULL;
    if (gender)
    {
     size_t length = strlen(gender) +1;
     strcpy_s(this->gender, length, gender);
    }
    else gender = NULL;
    if (age)
    {
     this->age = age;
    }
   }
   Dog::~Dog()
   {
    delete name;
    delete gender;
    age = 0;
   }
   void Dog::eat()
   {
    cout << name << " is eating now.. " << endl;
   }
   void Dog::bark()
   {
    cout << name << " says woof.. " << endl;
   }
   void Dog::walk()
   {
    cout << name << " is walking now.." << endl;
   }


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


   MyClass operator+(const Dog &dObj, const Cat &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.setInsta开发者_如何学运维nce(cObj);
    mObj.doSomething();
    cObj.meow();

    mObj = dObj + cObj;

    return 0;
   }


If you want to access a private member of Dog, then your operator has to be a friend of Dog, not just a friend of some derived class of Dog.


If you add friend MyClass operator+(const Dog &dObj, const Cat &cObj); to the definition of class Cat and class Dog, then the function will have access to dObj.age and cObj.age.

Alternatively, you could add friend MyClass; to the definitions of class Cat and class Dog, but then all of MyClass's functions have access to all of Cat and Dog's internals.

Alternatively alternatively, you could make age protected rather than private.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜