Thread: Inheritance question

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Inheritance question

    Code:
    class Foo{
    public:
    };
    
    class Poo : public Foo{
         // local variables
    public:
         // Accessor/Mutator
    };
    
    class Boo : public Foo{
         // local variables
    public:
        // Accessor/Mutator
    };
    If I passed a reference or pointer of Foo into another class, how am I to access Poo and Boo members through Foo?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You mean to access members of foo through Poo and Boo?
    An example would help
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by std10093 View Post
    You mean to access members of foo through Poo and Boo?
    An example would help
    No, to access Poo and Boo members through Foo; Foo has no members!

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Foo has no access to members of Poo and Boo.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You're probably asking about polymorphism. In C++ virtual functions make it work:

    Code:
    #include <iostream>
    using namespace std;
    class Foo {
       private:
       int data;
    
       public:
       Foo(int d = 0) : data(d) { }
       virtual ~Foo() { }
       virtual void displayData() const { cout << "data=" << data << endl; }
    };
    class Bar : public Foo {
       private:
       int moreData;
    
       public:
       Bar(int d = 0, int dd = 0): Foo(d), moreData(dd) { }
       virtual ~Bar() { }
       virtual void displayData() const { 
          Foo::displayData();
          cout << "moreData=" << moreData << endl; 
       }
    };
    void func(Foo& thing) {
       // Be polymorphic:
       thing.displayData();
    }
    int main() {
       Bar b1(42, 24);
       Foo f1(256);
       func(b1);
       cout << endl;
       func(f1);
       return 0;
    }
    C++ code - 35 lines - codepad

    Kind of a stupid example really, but you can see the change in behavior in spite of my questionable use of func().

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by whiteflags View Post
    You're probably asking about polymorphism. In C++ virtual functions make it work:

    Code:
    #include <iostream>
    using namespace std;
    class Foo {
       private:
       int data;
    
       public:
       Foo(int d = 0) : data(d) { }
       virtual ~Foo() { }
       virtual void displayData() const { cout << "data=" << data << endl; }
    };
    class Bar : public Foo {
       private:
       int moreData;
    
       public:
       Bar(int d = 0, int dd = 0): Foo(d), moreData(dd) { }
       virtual ~Bar() { }
       virtual void displayData() const { 
          Foo::displayData();
          cout << "moreData=" << moreData << endl; 
       }
    };
    void func(Foo& thing) {
       // Be polymorphic:
       thing.displayData();
    }
    int main() {
       Bar b1(42, 24);
       Foo f1(256);
       func(b1);
       cout << endl;
       func(f1);
       return 0;
    }
    C++ code - 35 lines - codepad

    Kind of a stupid example really, but you can see the change in behavior in spite of my questionable use of func().
    No, I like it. I was just curious if Poo or Boo could be accessed via Foo but it appears any such access must be polymorphic and your example is quite clear there.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can, in theory, use dynamic_cast to cast the Foo reference down to a Poo or Boo reference, but try to avoid it. It is rather poor style and inhibits extensibility. (It's not always avoidable.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inheritance question
    By Darkroman in forum C++ Programming
    Replies: 10
    Last Post: 09-24-2012, 06:52 PM
  2. Question about inheritance
    By dayalsoap in forum C++ Programming
    Replies: 11
    Last Post: 10-06-2011, 10:22 AM
  3. Inheritance Question
    By threahdead in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2011, 02:42 PM
  4. Inheritance question
    By alexy in forum C++ Programming
    Replies: 16
    Last Post: 11-06-2008, 03:46 PM
  5. Inheritance question
    By CompiledMonkey in forum C++ Programming
    Replies: 5
    Last Post: 12-19-2003, 01:20 PM