Crazy Games with Inheritance

This is a discussion on Crazy Games with Inheritance within the C++ Programming forums, part of the General Programming Boards category; So I wrote the following program to test the limits of inheritance: Code: #include <stdio.h> #include <conio.h> class baseMomma { ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    28

    Question Crazy Games with Inheritance

    So I wrote the following program to test the limits of inheritance:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    class baseMomma {
        public:
            virtual int x ();
            virtual int set(int _x);
        };
        
    class baby : public baseMomma {
        int mx;
        public:
            virtual int x () {
                return mx;
            };
            virtual int set(int _x) {
                int temp = mx;
                mx = _x;
                return temp;
            };
    };
    
    int main () {
        baseMomma narf[10];
        baby Bob;
        Bob.set(123456);
        narf[3] = Bob;
        printf("%d",narf[3].x());
        getch();
        return 0;
    };
    and came up with the linker errors:
    Code:
      [Linker error] undefined reference to `vtable for baseMomma' 
      [Linker error] undefined reference to `vtable for baseMomma' 
      [Linker error] undefined reference to `vtable for baseMomma'
    Can this be fixed? I'm trying to see what happens if I declare an array of a base object class and fill it with child inherited objects instead, as opposed to making an array of pointers to objects of the base type. I expect it won't work, but I'd like to know a bit more about it. Sorry for the lack of any recognizable question; I'm not sure quite what I'm looking for . So, your thoughts?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Theres no implementation for your base class functions and neither are they abstract. Try this.
    Code:
    #include<iostream>
    class baseMomma {
        public:
            virtual int x () { std::cout<<"Base::x"<<std::endl;}
            virtual int set(int _x) { std::cout<<"Base::set"<<std::endl;}
        };
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    As for what you're trying to find out, here's the answer: What will happen is the baby part of the object (e.g. Bob) will be sliced off leaving only the baseMomma part when you copy it into the vector. If you'll notice, that's what you're doing, assigning a copy of Bob to the element at index 3 in your narf array. When you copy a derived class object to a base class object it is called slicing, since the target of the copy only gets the base class data.

    If you want to hold a derived class object with a variable based on the base class type, use a reference or a pointer. In this case, if narf was an array of pointers and you assigned the address of Bob to it instead, the virtual method will work correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  2. When done right, PC games are amazing
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-13-2008, 06:32 PM
  3. Violent video games?
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 58
    Last Post: 04-26-2006, 02:43 PM
  4. Video Games Industry. 5 years left.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 12-10-2002, 10:52 PM

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