Thread: oop: inheritance

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    60

    oop: inheritance

    I have a question about this class:

    Code:
    class Word {
    	public:
    		Word();
    		...
    	private:
    		string lemma;
    		string category;
    };
    A word has a lemma and a (grammatical) category, for example "cat" (lemma) is a "substantive" (category).
    Another example: "The" is an "article".
    If I derive subclasses from this class Word, for example:

    Code:
    class Article: public Word {
    	public:
    		Article(string lemma);
    	        ....
    	private:
    		bool definite;
    };
    Isn't the member "category" of the class "Word" superfluous? Because, if you make an instance of Article, then it will be clear that the category is "article".
    On the other hand, if you're just handling items of type Word, then the category can be very relevant.
    So my question is: do I have to keep the member category of class Word or is this bad OOP practice?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If the category member variable is going to serve as a kind of type code, then it probably really is unnecessary and should be removed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Thanks, laserlight, I followed C_ntua's
    advice to dive into C++ and it's fun.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Even though technically there is nothing wrong on deriving Article from Word, an article would probably contain words, not be a word. It would make more sense if you had something like:
    Code:
    class Text
    {
    public:
        vector<Word> words; //this is an array of objects of class Word
        ...
        Text();
    };
    
    class Article : Text
    {
    };
    Thus an article will be a Text, where a Text will contain a vector (an array) of Word's.

    Don't really know what lasersight means with "type code". But I would say it doesn't matter. It is not superfluous. Why? Because category is private. Article cannot use it anyways. If you wanted Article to use lemma but not category, since category makes no sense for an article, you should have:
    Code:
    class Word {
    	public:
    		Word();
    		...
    	protected:
    		string lemma;
            private:
    		string category;
    };
    It could be superfuous if you really really care about the object's memory and you don't want it to have an extra string w/o a reason. Then you would have to make a basicWord class. Same as word without category. Word would derive from basicWord as well as Article. But most of the times these are details. You shouldn't go into a lot of trouble, wasting time, just to avoid having an unused by the derived class member.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Even though technically there is nothing wrong on deriving Article from Word, an article would probably contain words, not be a word.
    I think, by article OP means "a", "an" and "the".

    As to keeping out the category:

    Code:
    std::pair<Word*, std::string> p(new Article("a"), "article");
    (No idea what the program does, but most probably it will need to know the type of the word in ways that polymorphism can't provide. For internal usage, an enum might be a cheaper choice of identifying types that strings.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 10:32 AM
  3. recommendation for a good OOP book
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2006, 04:28 PM
  4. OOP Theory Question
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2005, 08:37 AM
  5. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM