virtual classes and inheritance

This is a discussion on virtual classes and inheritance within the C++ Programming forums, part of the General Programming Boards category; i have two question 1.If i have this two classes Code: class Vehicle { public: virtual int weight() const; //.... ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    4

    virtual classes and inheritance

    i have two question

    1.If i have this two classes
    Code:
    class Vehicle {
    public: 
              virtual int weight() const;
             //....
    };
    
    class Automobile {
    public: 
              int weight() const;
             //....
    };
    
    // this function is global
    int heavy(Vehicle v) {
            return v.weight() > 12000;
    }
    
    // main
    
    Automobile a;
    if (heavy(a)) {
           //..
    }
    so whats wrong with this code?? and how to fix it (simple solution) ??

    2. if class Y inherits from class X then following assignment is illegal
    Code:
      X x;
      Y y;
      y = x;
    Any example why allowing this kind of assignemnt would be an extremely bad idea?

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    1.
    Code:
        bool heavy(Vehicle v) {
            return v.weight() > 12000;
        }
    2. You need to overload assignment operator to do such thing

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I think there is a way to use pointers like:

    Code:
    Vehicle* v= new Automobile;
    But I'm a bit rusty on inheritance and i don't have a compiler handy
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    1. It sounds like you want Automobile to derive from Vehicle, but you forgot to do that in your code. Make it

    class Automobile : public Vehicle {

    Also, you must make your heavy function take a reference (or a pointer, but reference makes more sense here). I'd make it:

    int heavy(const Vehicle& v) {

    If you don't make that change then you are making a Vehicle copy of the Automobile to pass to the function, which slices off the Automobile-ness of the object.

    2. That brings us to your second question.
    Code:
    class X { ... };
    class Y : public X { ... };
        ...
      X x;
      Y y;
      y = x;
    That code only works if you overload the assignment operator like alphaoide said. I don't think that is what you really want, though, since it rarely makes sense. In your example above, it would be like having a vehicle of unknown type (could be a bicycle) and then setting an automobile instance equal to that vehicle. You would then use your bicycle like it was an automobile, and I would guess it wouldn't come out quite right. Logically it doesn't make sense to do that.

    The more common thing is when a function (like heavy above) is called that can take any vehicle. Obviously yours is just an example and other functions can be more complicated. But you can check if any vehicle is heavy, but each vehicle determines its weight differently. So using a Vehicle reference parameter allows you to pass in any Vehicle but the correct derived function will still be called if it is virtual.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    4

    Lightbulb

    thanks for your help....
    this should fix my code...

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: virtual classes and inheritance

    Originally posted by skora
    2. if class Y inherits from class X then following assignment is illegal
    Code:
      X x;
      Y y;
      y = x;
    Any example why allowing this kind of assignemnt would be an extremely bad idea?
    Within the data section of the sub-class Y lives some variables. When you assign a super-class (X) to the sub-class(Y), the X data section is placed into the X section of memory within Y. What happens to the variables in the Y data section is up to you. Whatever you do, your intentions are unclear, as it's not a logical course of action, imho.

    Here's an attempt to show you memory mapping for the class:
    Code:
    Assign Superclass to Subclass
    This is illogical behaviour, you have
    to build your own functions to achieve this
    
    Instance        Instance
       of              of   
     Super             Sub  
     Class            Class  
       |                |
      VVV              VVV   
    [-------]       [-------]
    X Super ] --->  X Super ]  The derived class is made of the 
    X Class ] --->  X Class ]  subclass and what it inherited.
    X       ] --->  X       ]
    [-------]       [-------]
                    Y Sub   ]
    What data --->  Y Class ]
    is copied here? Y       ]
                    [-------]
                    
    
    
    
    Assign Subclass to Superclass 
    This is acceptable behaviour, and is 
    allowed via the default assignment operator
                    
    Instance        Instance
       of              of   
      Sub             Super 
     Class            Class  
       |                |
      VVV              VVV   
    [-------]       [-------]       
    X Super ] --->  X Super ] 
    X Class ] --->  X Class ] 
    X       ] --->  X       ] 
    [-------]       [-------]       
    Y Sub   ] 
    Y Class ] <--> Data not copied               
    Y       ]                
    [-------]
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual inheritance
    By 6tr6tr in forum C++ Programming
    Replies: 13
    Last Post: 05-07-2008, 12:20 PM
  2. classes, inheritance and include problems
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 01:45 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 01:46 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 02:26 PM

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