Thread: Question about classes and inheritance

  1. #1
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320

    Question about classes and inheritance

    Say I have a class Item which overloads the assignment operator if I make a class called weapon that publicly inherits the Item class can I use the operator to assign a weapon to a item?
    like this?
    Code:
    Class Item
    {
       public:
          void SetAB(int, int);
          Item& operator = (const Item& i);
       private:
          int a;
          int b;
    };
    
    Item& Item::operator = (const Item& i)
    {
       a = i.a;
       b = i.b;
       return *this;
    }
    void Item::SetAB(int A,int B)
    {
       a = A;
       b = B;
    }
    
    Class Weapon : public Item
    {
       public:
       //doesnt matter i dont overload the = again
       private:
       int c;
       int d;
    };
    
    int main()
    {
       Weapon sword;
       sword.SetAB(1 , 2);
       Item temp;
       temp = sword;
       return 0;
    }
    Now my question is will temp.a and temp.b hold sword.a and sword.b and if I did it the other way sword = temp would sword hold temp.a and temp.b in sword.a and sword.b?

    Also is this something thats not violating any standards of programming? Like void main() or something i mean if it works but its just bad practace.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    It's easy.
    Last edited by The Brain; 10-23-2004 at 04:00 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can I use the operator to assign a weapon to a item?
    You can do this legally, but I really wouldn't recommend it as a good practice. What's wrong with writing an overloaded assignment operator for Weapon and doing things the conventional way?

    >Class Item
    >Class Weapon : public Item
    Case matters. The keyword is class, not Class.
    My best code is written with the delete key.

  4. #4
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Quote Originally Posted by Prelude
    >can I use the operator to assign a weapon to a item?
    You can do this legally, but I really wouldn't recommend it as a good practice. What's wrong with writing an overloaded assignment operator for Weapon and doing things the conventional way?

    >Class Item
    >Class Weapon : public Item
    Case matters. The keyword is class, not Class.
    I wrote that code not in the editor just off the top of my head so I didn't have any color coding.
    The main reason i dont overload it again is that I need to go from Item to weapon and weapon to Item or Item to Armor and Armor to Item.

    //longer version and background of program
    The reason i needed to do it is the inventory of my characters is an array of items but when they equip something like a weapon or armor i need to take the basic stuff like the ID and type etc just basic stuff when they equip the stuff it loads all the specifics from a file damage range defence for armor shield block rates etc. So instead of making some other function i was wondering if i could have a thing of class Item in a variable (player's hand) and when they equip it to what ever area could i just use = or would i need to write specalized functions to this. I wasnt as concerned about proper but more about would it generate unwanted results ie memory leaks or whatever. As I understand classes it should work since its going to be inherited but im not 100% sure that I wont generate leaks and have really bad things happen like running out of memory while people are playing on my server. Nor would I want the program to get confused and throw a save, and exit exception.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The main reason i dont overload it again is that I need to go from Item to weapon and
    >weapon to Item or Item to Armor and Armor to Item.
    So use pointers or references and polymorphism. That way you can use a pointer or reference to an Item and still have the full Armor or Weapon objects behind it. There's no good reason to allow slicing like that unless you know something that I don't (which is certainly possible).
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining classes following user input. Inheritance.
    By Swerve in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2009, 09:21 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. virtual classes and inheritance
    By skora in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2003, 05:28 PM
  4. Multiple Inheritance Question
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2002, 06:09 AM
  5. Inheritance related question. Is this correct?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 07-19-2002, 04:30 AM