problems using inheritance

This is a discussion on problems using inheritance within the C++ Programming forums, part of the General Programming Boards category; Hi forum, I am new to C++ and have the following problem. I create a class A from which I ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    problems using inheritance

    Hi forum,

    I am new to C++ and have the following problem. I create a class A from which I derive several other classes B1, B2, ... via inheritance. A member function f of A computes a specific value (property) V of the things I am dealing with. B1, B2, ... all have this property. I thought, "Thank god there is inheritance, now I don't have to write the same member function again and again for every B class." However, the property I am computing in this member function depends on another property specific to the derived classes. Lets call this property P... so P(B1) is different from P(B2) etc. In the main I want to initialize objects from the derived classes b1, b2 etc and call b1.f, b2.f etc. I need to achieve that this function now uses the specific properties P(B1), P(B2) of the derived classes and not the one of the basis class. As f is defined in the scope of A it always uses P(A).

    My constraints are:
    1. the function cannot take further input arguments (no objects by reference etc)
    2. I don't want to redefine f in all of the derived classes (I want f not to appear in the definition of the derived classes).

    I hope this makes sense. Thanks for your help guys.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If all properties are of the same type, you can add a virtual function that returns a reference to the correct property. Then override that virtual function in every derived class and keep the rest of the computation in A.
    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

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Thanks for your answer. I tried using virtual functions for this problem. But I don't know how to return a reference to the correct property. Could you give me a hint how this would look like in code?

  4. #4
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Returning by reference is pretty simple. It's also super cool and you'll come across uses for it time and again when you get really involved in this stuff.

    e.g.:
    Code:
    int& GetSmallest(vector<int>& v)
    {
        assert(!v.empty());
        vector<int>::iterator smallest = v.begin();
        
        vector<int>::iterator it = smallest;
        ++it;
        for (; it != v.end(); ++it)
        {
            if (*it < *smallest)
                smallest = it;
        }
        
        return *smallest;
    }
    This returns not exactly the smallest value in the vector, but a reference to the smallest value in the vector. So you could do something like this:

    Code:
    // v contains {5, 3, 7, 4}
    ++GetSmallest(v);
    // v contains {5, 4, 7, 4}

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Quote Originally Posted by Mozza314
    This returns not exactly the smallest value in the vector, but a reference to the smallest value in the vector.
    Not a terribly important point with respect to the context of your example, but you could have used std::min_element instead of writing the loop yourself.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    It's probably due to the original posters vagueness, but I haven't heard of references to class members (properties). I thought there were only pointers to members. So, if that's what this is about, then I'm not sure how discussing references will help.

  7. #7
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by whiteflags View Post
    I thought there were only pointers to members.
    Seems like a strange thing to say. There's nothing stopping you from doing this (although it is bad practice):

    Code:
    #include <iostream>
    
    class Weird
    {
    private:
        int x;
    
    public:
        int& Get() { return x; }
        void Print() { std::cout << x << std::endl; }
    }
    
    int main()
    {
        Weird w;
        w.Get() = 5;
        w.Print(); // Outputs 5
        
        return 0;
    }

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Well that's not precisely what I was thinking but OK.

    Pointers to members are actually like this:

    Weird::*memptr = &Weird::x; // x is public

    It's a fair bit more restricted, and I haven't heard of and am apparently right in thinking that there isn't a reference analog. Not that I have a better idea, but it seems altogether strange. For some reason I think we aren't getting a full picture.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Quote Originally Posted by whiteflags
    It's a fair bit more restricted, and I haven't heard of and am apparently right in thinking that there isn't a reference analog. Not that I have a better idea, but it seems altogether strange. For some reason I think we aren't getting a full picture.
    I think that you're thinking of "property" as in "member variable", but in this case KarinaErlang stated that it is a domain specific term for some computed value(s). As such, CornedBee's suggestion should be correct: the properties P(B1), P(B2), etc, specific to the derived classes B1, B2, etc, are computed in virtual functions that are called by the non-virtual member function of the base class A that computes the property V. As such, V will be computed using P(B1), P(B2), etc, accordingly, not with P(A) (except when it really is called on an object of type A, not of a derived class type).

    If P does not need to be exposed as part of the interface, this would effectively be an instance of the non-virtual interface idiom since it could be made private.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I simply mean this:
    Code:
    class A {
      virtual int& getProperty() = 0;
    
    public:
      void foo() {
        int& prop = getProperty();
        // do some calculation
        prop = calculation;
      }
    };
    
    class B1 : public A {
      int myProp;
    
      int& getProperty() { return myProp; }
    };
    
    class B2 : public A {
      int hereProp;
    
      int& getProperty() { return hereProp; }
    };
    
    class B3 : public A {
      int someProp;
    
      int& getProperty() { return someProp; }
    };
    And yes, the function is intentionally private.
    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

  11. #11
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by CornedBee View Post
    I simply mean this:
    And yes, the function is intentionally private.
    If I can just offer a suggestion, I think it's good practice to always write "virtual" in front of any member which is implicitly virtual, i.e.:

    Code:
    class A {
      virtual int& getProperty() = 0;
    
    public:
      void foo() {
        int& prop = getProperty();
        // do some calculation
        prop = calculation;
      }
    };
    
    class B1 : public A {
      int myProp;
    
      virtual int& getProperty() { return myProp; }
    };
    
    class B2 : public A {
      int hereProp;
    
      virtual int& getProperty() { return hereProp; }
    };
    
    class B3 : public A {
      int someProp;
    
      virtual int& getProperty() { return someProp; }
    };
    Last edited by Mozza314; 03-31-2011 at 06:54 PM.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Matter of taste. I personally don't care about that.
    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 and program structure planning please help a newbie
    By ninjacookies in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2005, 12:18 PM
  2. inheritance problems
    By generic83 in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2005, 05:04 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  4. Inheritance problems
    By alkis_y3k in forum C++ Programming
    Replies: 5
    Last Post: 01-03-2003, 05:00 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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