Thread: Question about designing a abstract class

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    Question about designing a abstract class

    Hi guys, this is a question about design pattern.
    I wanna ask if I have a I have base class A, and derived class B, C, D.
    And class A is a abstract class, but all of the instance of B , C, D have a same data member.
    I want to ask should I declare a data member in all these three derived class or is it normal to declare it in the base class A? Also if I have some methods for B C D all doing the same thing,
    In this case, I would like to implement this common methods in the abstract base class. But
    my friend argue me about we should use abstract class without any implementation or any data member in base class (like interface in JAVA?) So he suggests me to change my codes in base class to be pure virtual functions. And implement all the same common methods in each subclasses and declare the same data member in sub class. I don't know what's the merit of doing this...
    Looking for any suggestion, thx.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    In my opinion using the common members and functions is just fine. You should avoid duplicating code and if there is going to be the common functionality between the classes there is nothing wrong with having the base class implement that functionality.
    Woop?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is normal to have the data member be in the base class, and then to define a non-virtual member function in the base class, if it does make sense to have this member function in the base class (i.e., if you later decide to have a derived class E, that data member and member function would still make sense).

    It is also normal to have abstract base classes that are pure interfaces. You could even use both, with multiple inheritance, if you see fit to do so. But without specifics (and possibly even with specifics), it is hard to say what would be the most appropriate solution.
    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

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Thx for you opinion.
    He tries to convince me that we should design a pure interface instead of a abstract class with some default implementation and member. And Implement all the codes in the derived class. He says he found lots of information support his idea. However, I think that will contain lots of duplicate codes as you said.
    Seemes there's no need in my case to use a pure virtual interface.?

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Honestly this goes back to a preference. Just like I could probably find tons of support that
    Code:
    if(condition){
    }
    is better than
    Code:
    if(condition)
    {
    }
    So really if the situation seems to fit the model, then use it.
    Woop?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ovid
    He tries to convince me that we should design a pure interface instead of a abstract class with some default implementation and member. And Implement all the codes in the derived class. He says he found lots of information support his idea. However, I think that will contain lots of duplicate codes as you said.
    Seemes there's no need in my case to use a pure virtual interface.?
    Yes, if you do it that way, it would mean duplicating work. But maybe, given the right interface, you can then implement a single non-member non-friend function that works with this interface, instead of duplicating the implementation across the derived class. Since the derived classes implement this interface, this function would work with objects of any of them.
    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

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Quote Originally Posted by laserlight View Post
    Yes, if you do it that way, it would mean duplicating work. But maybe, given the right interface, you can then implement a single non-member non-friend
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    function that works with this interface, instead of duplicating the implementation across the
    ^^^^^^^^^^^^^^^^^^^^^^^

    derived class. Since the derived classes implement this interface, this function would work with objects of any of them.
    I don't get what you mean here. does the function mean the client code?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ovid
    I don't get what you mean here. does the function mean the client code?
    I am talking about the function that you want to make a member function in the base class.
    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

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Oops. Sorry I am not native speaker. I have trouble understanding what you just reply..
    I thought You approve me with some implementations in the base class, right? Are you just giving a situation that it's better to implement all the functions in the derived type?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ovid
    I thought You approve me with some implementations in the base class, right?
    Yes and no. I merely said that it was normal.

    Quote Originally Posted by ovid
    Are you just giving a situation that it's better to implement all the functions in the derived type?
    No. I am giving a situation where it is better to program to an interface, i.e., to write a function that works with all objects of classes derived from a base class. Maybe it is not feasible in your situation, but it is, then it is better to do that instead of inheriting concrete implementation.
    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

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Thx a lot ... I think I get it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Quick question about class template
    By merixa in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2005, 11:43 PM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. abstract vs pure abstract class
    By Kohatian 3279 in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2002, 11:12 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM