0

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.setInstance(cObj);
    mObj.doSomething();
    cObj.meow();

    mObj = dObj + cObj;

    return 0;
   }
3
  • I'm confused ... what is the question exactly? Commented Apr 8, 2010 at 21:30
  • 2
    do you have some objection to std::string? Commented Apr 8, 2010 at 21:32
  • Read up on the protected access qualifier keyword. Otherwise, this example is nonsensically contrived. Why should operator+ be in a class derived from its operands? Commented Apr 8, 2010 at 22:27

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

In other words: you can touch the privates of your friends, but you can't touch the privates of your friends parents.
+1: Just because you trust your friends into your house, does not mean that you father will trust them with the safe combination
Or put another way - friendship is not transitive or inherited. God that makes me hot!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.