Inheritance

This is a discussion on Inheritance within the C++ Programming forums, part of the General Programming Boards category; I have one class in one file and another class in another file. One class is derived from the other. ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    92

    Inheritance

    I have one class in one file and another class in another file. One class is derived from the other.

    From my understanding a derived class doesn't have to initialize any of the base class methods to be able to use them. When I try to compile my program it says that my derived class has no member that matches the base class.

    Code:
    class Base{
        void print();
    }
    
    class Derived : Base{
    }
    
    int main{
       
        Derived d;
        d.print();  //This should just default to the base member but I'm getting an error saying that my derived class has no member 'print'

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    This works for me:
    Code:
    class Base
    {
    public:
      void print() { }
    };
    
    class Derived : public Base
    {
    
    };
    
    int main()
    {  
        Derived d;
        d.print();
    }
    "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

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Please post actual code for actual help with actual code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    MmmmmKay. I need to figure out what I need to post up because my program is close to 1500 lines with all the files together. Do you just want me to post all of it?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by warfang View Post
    MmmmmKay. I need to figure out what I need to post up because my program is close to 1500 lines with all the files together. Do you just want me to post all of it?
    No, that's a bit long.

    One good step in debugging is to start trimming away at the code until you're left with the problem. One way to do this is to #define some macro constant such as OOPS, or whatever, and do something like this:
    Code:
    #ifndef OOPS
    /* code to 'remove' */
    #endif
    This is an after-the-fact method when you haven't done or had the opportunity to do incremental builds.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Well the derived and base classes aren't terribly long. I'll post the Derived first since I'm sure the problem is just in the Derived class. There's two derived classes to be exact and I did them both the same way.

    EDIT: Took out Code, uploading files...
    Last edited by warfang; 04-24-2007 at 12:20 AM.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I see a few problems with your code, but I don't think I see the cause of your error.

    Code:
    CaseString::CaseString() : String(){
    What is this "String"? CaseString is derived from "string," isn't it? Or is that a typo.

    Code:
    CaseString::~CaseString() : ~String(){
    This shouldn't compile (as far as I know). And assuming the base class is "String" after all, then its destructor will be called implicitly when CaseString's destructor is called. (Note that CaseString's destructor is not guaranteed to be called if you forget to make String's destructor virtual)
    "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

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Ah, looking at your other post I might just see the reason for your error:
    Code:
    //string.h
    using namespace std;
    Try removing that line and see if your error changes

    Edit: And you'll have to explicitly qualify std::ostream (and possibly some others)
    "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

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Quote Originally Posted by JaWiB View Post
    What is this "String"? CaseString is derived from "string," isn't it? Or is that a typo.
    Wow, I feel dumb . Thank you so much. I would have looked everywhere but there for hours. You rock.

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Glad to hear you've got it working. Don't miss my last point, though: this is a good reason to avoid "using namespace std;" At the very least, don't put it in a header file.
    "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

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Well regardless if I have
    Code:
    using namespace std;
    or if I don't have it... I get Linker errors for undefined references to 'CaseString::print()' and other CaseString member functios.

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    I'm getting Linker Errors on both derived classes now. I'm not sure if this is a step backwards or not because I just changed a method and now it's a Linker Error.

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    I fixed it all. It was because I was missing an #include

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    The print() method isn't public in Base. And Derived doesn't inherit publicly from Base, so even if you made it public in Base, it still wouldn't work.

  15. #15
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Well it works. I use the print method in the derived class just fine.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 01:46 PM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM

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