Inheritance!

This is a discussion on Inheritance! within the C++ Programming forums, part of the General Programming Boards category; Aargh! I am really confused about inheritance. I have experience with Standard C, and another OOP language (Object Pascal), but ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    43

    Inheritance!

    Aargh! I am really confused about inheritance. I have experience with Standard C, and another OOP language (Object Pascal), but am just starting with C++.

    Heres the question: is there any way for private fields/member functions to be inherited by derived classes?

    So I'd do something like this (this is a bit of a silly example):

    Code:
    //Program 1
    #include <stdio.h>
    
    class Base {
    private:
    Happy() { printf("Happy "); }
    };
    
    class Derived: public Base {
    public:
    HappyBirthday() { Happy(); printf("Birthday\n"); }
    HappyChristmas() { Happy(); printf("Christmas\n"); }
    };
    
    int main ()
    {
    Derived d;
    d.HappyBirthday();
    d.HappyChristmas();
    getchar();
    return 0;
    }
    and the compiler spits back an error, something along the lines of "Happy() is private in this context". I am using Dev-C++.

    Now, I think I get why this is happening: derived classes do not have access to private members, only the public interface provided by the base class. Here's my problem: I want to make 6 classes which are virtually identical. The only difference between them, in fact, is they have different constructors. The constructor needs to be able to set up the private class members, but it turns out it doesn't have access to them. Furthermore, I need to be able to have a pointer which can point to any of the 6 classes, and so I can't just make a macro which defines the class and repeat it 6 times. I need to use derivation in some sense.

    Now, I tried defining the members in the derived class, and the program compiled, but it appears that these members are different from the ones in the base class, even if they have the same names. For example:

    Code:
    //Program 2
    #include <stdio.h>
    
    class Base {
    private:
    int a;
    public:
    void ChangeBasea(int k) { a = k; }
    void PrintBasea() { printf("%d", a); }
    };
    
    class Derived: public Base {
    private:
    int a;
    public:
    void ChangeDeriveda(int k) {a = k; }
    void PrintDeriveda() { printf("%d", a); }
    };
    
    int main()
    {
    Derived d;
    d.ChangeBasea(0);
    d.ChangeDeriveda(1);
    d.PrintBasea();
    d.PrintDeriveda();
    getchar();
    return 0;
    }
    compiles, but prints 01 instead of 11 which is what it would print if the base and derived class members were the same.

    Now, I understood scope in the context of functions in Standard C, but in Object Pascal if you did something similar then they would be the same.

    Please help me understand and implement my problem above!!

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    is #include <iostream> and using namespace std; required? I really dont have a clue but if I am right then I will shoot myself in excitement.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    #include <stdio.h> --> <cstdio>
    Heres the question: is there any way for private fields/member functions to be inherited by derived classes?
    Make them "protected" members of the base clase. There are three levels of access: public, protected, private. Protected is equivalent to private except that it allows the member to be inherited.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> is #include <iostream> and using namespace std; required?

    No.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    if I am right then I will shoot myself in excitement.
    ...you can put the pistol away.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    43
    Quote Originally Posted by 7stud
    Code:
    #include <stdio.h> --> <cstdio>

    Make them "protected" members of the base clase. There are three levels of access: public, protected, private. Protected is equivalent to private except that it allows the member to be inherited.
    Great! This fixed my problem. Thank you very much.

    However, I am curious about this "cstdio" thing, what does that mean? I thought include directives worked just the same in C++ as they did in C.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    There are newer versions of many of the good old headers you are familiar with from the days of C that include support for namespaces. For those standard C headers, drop the .h extension and add a 'c' to the beginning.:

    Code:
    <stdio.h> -> <cstdio>
    <stdlib.h> -> <cstdlib>
    <string.h> -> <cstring> (not to be confused with the <string> header which is completely different)
    The older versions of the headers are deprecated when used in C++:
    Quote Originally Posted by dictionary.com
    deprecated

    adj. Said of a program or feature that is considered obsolescent and in the process of being phased out, usually in favor of a specified replacement. Deprecated features can, unfortunately, linger on for many years. This term appears with distressing frequency in standards documents when the committees writing the documents realize that large amounts of extant (and presumably happily working) code depend on the feature(s) that have passed out of favor. See also dusty deck.
    Last edited by hk_mp5kpdw; 11-14-2005 at 10:33 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by bumfluff
    is #include <iostream> and using namespace std; required? I really dont have a clue but if I am right then I will shoot myself in excitement.
    Don't hijack someone's thread.

    Since everyone's question is answered, I guess I have nothing more to say...

    [EDIT] Oh, well.. I guess since this thread is over as far as answering the guy's question I'll ask something to hk_mp5kpdw. Have you been watching Dead Like Me lately?

    I only got a free preview to Showtime and I watched all the episodes onDemand, but that was months ago. Anything happen since then?
    Last edited by SlyMaelstrom; 11-14-2005 at 11:04 AM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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