Objects as attributes of other objects.

This is a discussion on Objects as attributes of other objects. within the C++ Programming forums, part of the General Programming Boards category; Hi guys, Just having a small problem with something i'm playing around with. Basically, I have two classes, an Orc ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    33

    Objects as attributes of other objects.

    Hi guys,

    Just having a small problem with something i'm playing around with.

    Basically, I have two classes, an Orc class that creates Orc objects, and a Weapon class that creates Weapon objects. Both of these are fairly simple, i'm just trying to get comfortable with OOP in C++.

    I have Orc.cpp, Weapon.cpp and the corresponding header files for both.
    I also have a driver that creates Orc objects.

    What i'm having trouble with is creating Weapon objects that are attributes of Orc objects.
    That is, an Orc object has:

    Code:
    private:
    
    string name;
    int hp;
    Weapon weapon1;
    And in the Orc constructor, I try this:

    Code:
    {
    
    this->name = "";
    this->hp = 100;
    weapon1();
    
    }
    I get "no match for call to (weapon)()" compiler error.

    What am I doing wrong?

    Also, a second question, how can I refer to the public functions within the weapon object from the orc object?

    So basically, let's assume I have an Orc object called orc1, which inside it has the Weapon object weapon1 as a private attribute, how would I call a function of the weapon1? In java, it would look something like:

    Code:
    orc1.weapon1.getDescription();
    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Quote Originally Posted by kbro3
    Basically, I have two classes, an Orc class that creates Orc objects, and a Weapon class that creates Weapon objects.
    I think that it would be simpler to say that you have an Orc class and a Weapon class. Saying "Orc class that creates Orc objects" gives me the impression that you may have say, a member function of the Orc class that returns an Orc object, whereas you probably just have constructors for the class.

    Quote Originally Posted by kbro3
    I get "no match for call to (weapon)()" compiler error.

    What am I doing wrong?
    By the time control reaches the body of the constructor, the member objects have been constructed. As such, weapon1() actually invokes operator() for the weapon1 object, but since Weapon presumably has not overloaded operator(), you get the error that you did. Since you apparently want to default construct the Weapon object, the simple solution is to do nothing yourself. Likewise, you need to do nothing yourself concerning the name. You could write:
    Code:
    Orc::Orc() : hp(100) {}
    thus using the constructor initialisation list to initialise hp to 100.

    Quote Originally Posted by kbro3
    So basically, let's assume I have an Orc object called orc1, which inside it has the Weapon object weapon1 as a private attribute, how would I call a function of the weapon1? In java, it would look something like:
    It would be the same in C++, provided of course that in the given context you can access the weapon1 private member variable.
    Last edited by laserlight; 08-15-2009 at 12:13 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Quote Originally Posted by kbro3 View Post
    Also, a second question, how can I refer to the public functions within the weapon object from the orc object?

    So basically, let's assume I have an Orc object called orc1, which inside it has the Weapon object weapon1 as a private attribute, how would I call a function of the weapon1? In java, it would look something like:

    Code:
    orc1.weapon1.getDescription();
    Thanks.
    Wait, this makes no sense.
    How can you access anything private in either C++ or Java? You cannot.
    Either the object must be public or you make a getter function. Basically that would be a function that would return a reference to the weapon.
    However, that begs another question: how do you manipulate the Weapon object in the first place? Are you going to add functions for every piece of stuff implemented into Weapon into Orc? That wouldn't make sense. I think a better idea is basically to allow the programmer to create the weapon and then assign it to the orc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by laserlight View Post
    Since you apparently want to default construct the Weapon object, the simple solution is to do nothing yourself.

    It would be the same in C++, provided of course that in the given context you can access the weapon1 private member variable.

    Yes, I do want to default construct the Weapon object. Don't I need to call the constructor? (that's what I tried to do in the code). Are you saying that a default weapon object would be created when an Orc object is called without having to call the Weapon constructor?


    As for the second question, i'm calling a public method, so it's not a private variable, hence it's fine to use it in the form I wrote?

    Cheers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by Elysia View Post
    Wait, this makes no sense.
    How can you access anything private in either C++ or Java? You cannot.
    Either the object must be public or you make a getter function. Basically that would be a function that would return a reference to the weapon.
    However, that begs another question: how do you manipulate the Weapon object in the first place? Are you going to add functions for every piece of stuff implemented into Weapon into Orc? That wouldn't make sense. I think a better idea is basically to allow the programmer to create the weapon and then assign it to the orc.
    Ah crap, yes I see what you mean. getDescription is a public function of Weapon object. But I listed Weapon object as a private attribute of Orc, so i would not be able to call its' public methods since the object itself is private, right?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Quote Originally Posted by kbro3
    Yes, I do want to default construct the Weapon object. Don't I need to call the constructor? (that's what I tried to do in the code). Are you saying that a default weapon object would be created when an Orc object is called without having to call the Weapon constructor?
    Yes, for a member object of class type the default constructor is invoked automatically. It is only when you want (or have to) use a non-default constructor that you invoke it in the initialisation list, not the constructor body.

    Quote Originally Posted by kbro3
    As for the second question, i'm calling a public method, so it's not a private variable, hence it's fine to use it in the form I wrote?
    You are calling a public method (or more generally, a public member function since method means virtual member function) of a private member variable.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Quote Originally Posted by kbro3 View Post
    Yes, I do want to default construct the Weapon object. Don't I need to call the constructor? (that's what I tried to do in the code). Are you saying that a default weapon object would be created when an Orc object is called without having to call the Weapon constructor?
    The default constructor is automatically called for you.
    Otherwise, use the initializer list as laserlight showed.

    Quote Originally Posted by kbro3 View Post
    As for the second question, i'm calling a public method, so it's not a private variable, hence it's fine to use it in the form I wrote?

    Cheers.
    Quote Originally Posted by kbro3 View Post
    Ah crap, yes I see what you mean. getDescription is a public function of Weapon object. But I listed Weapon object as a private attribute of Orc, so i would not be able to call its' public methods since the object itself is private, right?
    The problem is that the weapon object in itself is private, so you cannot access it whatsoever. Whether it's just to peek at it or call functions. It simply cannot be done because it's private to the class.

    I would go with my suggestion: create a setter and getter in the Orc class to set/get the weapon and then manually create a weapon and attach it to the object.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by Elysia View Post
    I would go with my suggestion: create a setter and getter in the Orc class to set/get the weapon and then manually create a weapon and attach it to the object.
    Thanks, I created a getter in Orc, called getWeaponDescription, this is what I wrote:

    Code:
    string Orc::getWeaponDescription()
    {
    
    return weapon1.getDescription();
    
    }
    ^ This calls Weapon's getDescription() function to return the details. It compiles, and behaves correctly at run time.

    I'm not sure what you mean by "manually create a weapon and attach it to the object" - I assume something along the lines of having some kind of Weapon array inside Orc, along with a public function that allows passing of Weapon objects to add into the array, am I on the right track?

    Cheers.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by laserlight View Post
    Yes, for a member object of class type the default constructor is invoked automatically. It is only when you want (or have to) use a non-default constructor that you invoke it in the initialisation list, not the constructor body.
    Ah thanks for that, did not realise the member objects are created automatically, but I tried it and as you said, it works.

    I'm very new to C++ and have not yet played with an initialisation list, only created objects through constructors, but will have to read up on that. Cheers.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Quote Originally Posted by kbro3 View Post
    Thanks, I created a getter in Orc, called getWeaponDescription, this is what I wrote:

    ...snip...

    ^ This calls Weapon's getDescription() function to return the details. It compiles, and behaves correctly at run time.
    This is not a good approach. You are essentially wrapping the functions contained in Weapon. For what purpose? You will end up wrapping all the functions, taking time and cluttering the class. Bad idea.
    See solution below instead.

    I'm not sure what you mean by "manually create a weapon and attach it to the object" - I assume something along the lines of having some kind of Weapon array inside Orc, along with a public function that allows passing of Weapon objects to add into the array, am I on the right track?
    Code:
    class COrc
    {
        // ...
        void SetWeapon(const CWeapon& Weapon);
        const Weapon& GetWeapon() const;
        // ...
    };
    
    COrc Orc;
    CWeapon MyWeapon;
    // Manipulate weapon to your choice.
    Orc.AddWeapon(MyWeapon);
    Something like this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Sometime it might preferable to wrap the weapon class, but it might be wise if you declare the main Weapon's getter and setter to be virtual and stick to them.
    Just GET it OFF out my mind!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Attributes as objects
    By drrcknlsn in forum C++ Programming
    Replies: 15
    Last Post: 01-15-2008, 07:25 PM
  2. MURK - a small preview
    By Mario F. in forum Game Programming
    Replies: 27
    Last Post: 12-18-2006, 07:22 AM
  3. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21