Thread: further about Inheritance

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    further about Inheritance

    Code:
    class Vehicle 
    { 
    
    private: 
    int modelno; 
    char name; 
    string regno;
    
    public:
    
    int getmodel(); 
    int setmodel(); 
    void addregno():
    
    }; 
    
    class Car: public Vehicle 
    { 
    
    };
    hi guys

    I wanted to know that is their a way to restrict the derived class "Car" so that it could only use addregno() function of the base class "Vehicle"?

    by addregno I mean registeration number.


    Does "string" data type allows us to have a characters with both numeric and alphabets?


    Thanks
    Last edited by student111; 06-11-2012 at 10:20 AM. Reason: adding more

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by student111 View Post
    I wanted to know that is their a way to restrict the derived class "Car" so that it could only use addregno() function of the base class "Vehicle"?

    by addregno I mean registeration number.


    Does "string" data type allows us to have a characters with both numeric and alphabets?


    Thanks
    1.Just make the other functions private.
    2.Yes

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Hi

    Thanks for the reply, ok but if I just want to restrict the derived class "Car" to only use that function, so can I just mention that function in the public portion of the derived class "Car"? Will that be the same thing?

    Also if I don't write the public before the base class "Vehicle" as:


    Code:
    class Car : Vehicle
    
    {
    
    
    
    };
    Will this code be wrong?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by student111
    Thanks for the reply, ok but if I just want to restrict the derived class "Car" to only use that function, so can I just mention that function in the public portion of the derived class "Car"? Will that be the same thing?
    Let's talk a little about interfaces: suppose you're writing the main function. What member functions of the Vehicle class would you expect to use? What about the Car class?

    Quote Originally Posted by student111
    Will this code be wrong?
    No, but it means that you would be using private inheritance.
    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

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Btw, if your Car is inheriting from a Vehicle, it is a good idea to make the car able to do everything a vehicle should be able to do.

    It may do it a little differently, but it removing functionality down the ladder of inheritance isn't a good idea or design, in my opinion.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Here you go again...posting on an old thread intentionally trying to prove me wrong.
    I suggest that you think carefully about what I wrote, before trying to counter it.

    The base class is the most general type...often used as an interface.
    If the classes derived from it do not support the same interface, it will be a violation of the Liskov substitution principle; and will be a broken design.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I think you should read a good book on programming and understand the concept of "specialization" in C programming.
    I think you should be careful. This comment could swing either way but because you've already been warned about your behavior today we are much all going to interpret this as a comment with intent to directly insult.

    You will learn how to restrict a perticular behaviour for a perticular derived class .
    O_o

    This isn't C where "classes" are hidden solely behind abstract interfaces.

    This is C++ where classes are "first class" where "public inheritance" means I can get those pesky restrictions of the derived class to evaporate by using a base class pointer.

    Besides, a derived class must not add restrictions to an interface less it break substitution; a derived class must only lessen restrictions.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance
    By freakyboard in forum C++ Programming
    Replies: 20
    Last Post: 01-01-2011, 12:20 PM
  2. Using inheritance.
    By kpridd in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2009, 03:20 PM
  3. c++ inheritance
    By seizmic in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2005, 09:20 PM
  4. about inheritance, please help
    By Antigloss in forum C++ Programming
    Replies: 10
    Last Post: 08-10-2005, 06:11 PM
  5. Inheritance Fun
    By XSquared in forum C++ Programming
    Replies: 8
    Last Post: 08-10-2005, 12:21 AM