Inheritance Question

This is a discussion on Inheritance Question within the C++ Programming forums, part of the General Programming Boards category; Hey there, image i have a class called BMPImage. The class looks simplified like this: Code: #include "Matrix.h" class BMPImage ...

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    Inheritance Question

    Hey there,

    image i have a class called BMPImage. The class looks simplified like this:
    Code:
    #include "Matrix.h"
    
    class BMPImage
    {
    public:
        BMPImage(const char *filename);
        ~BMPImage(void);
    
    private:
        Matrix<float> *data;
    };
    data is allocated in the constructor of BMPImage.

    The Matrix class looks like this:
    Code:
    template<typename T>
    class Matrix
    {
    public:
        Matrix(const int nRows, const int nCols, const int nChannels);
    
        T get(const int row, const int col);
        void set(const int row, const int col, T value);
    
    private:
        T *data;
    };
    data is aswell allocated in the constructor.

    BMPImage should have much of the same functionality as a Matrix. This would actually be solved by letting BMPImage inherit from Matrix. This is what i actually want to do.

    The problem is, that the two constructors look quite different for Matrix and BMPImage.
    What would be an elegant way to implement this?

    I think of something like this
    Code:
    class BMPImage : public Matrix<float>
    {
    public:
        BMPImage(const char *filename)
        {
             // read the BMP File
    
             // set nRows, nCols of Matrix accordingly to the BMP file
        }
    };
    This would require Matrix to have a default constructor, which would complicate the Matrix class somewhat more than i like to.

    Another way would be to implement getters and setters in BMPImage which would simply wrap the getters and setters of the Matrix where the BMP Data is stored.

    Any ideas on what solution would be more adequate?

    Thanks and best regards, threahdead

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    From a design perspective, inheritence is used to represent "is a" relationships between types. For example, "Dog" is a "Mammal". From what you say, the only reason you want to use inheritance is because of similar interfaces which is necessary but usually considered insufficient from a design perspective. Generally it is considered best if class hierarchies comply with the Liskov Substitution Principle which, roughly speaking, means the derived classes are specialised (rather than extended) versions of the base class. You are breaking that guideline - the fact you have a constructor with different signature (accepting a filename) hints strongly at that.

    Generally, you are better off using containment (i.e. BMPImage contains and manages an instance of Matrix<float>). In practice, that means writing forwarding functions, and those forwarding functions are able to perform additional error checking if desired (for example range checking).

    That said, the compiler will let you do it either way. However, it is better to stick with accepted design principles, in order to avoid getting into design habits that confuse other programmers.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    I'll just give you some advice:
    SourceForge.net: Raw pointer issues - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Thank you very much for your answers. It helped me alot to see clearly again, how OO code should be designed.
    I tend to forget some principles when doing a long coding session.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance question
    By alexy in forum C++ Programming
    Replies: 16
    Last Post: 11-06-2008, 02:46 PM
  2. Nooblet question regarding inheritance
    By HyperShadow in forum C++ Programming
    Replies: 4
    Last Post: 04-26-2008, 12:05 PM
  3. Inheritance question.
    By antex in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2006, 09:59 AM
  4. Different question about inheritance
    By Marcos in forum C++ Programming
    Replies: 5
    Last Post: 03-02-2006, 02:52 PM
  5. Inheritance question
    By CompiledMonkey in forum C++ Programming
    Replies: 5
    Last Post: 12-19-2003, 12:20 PM

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