further about Inheritance

This is a discussion on further about Inheritance within the C++ Programming forums, part of the General Programming Boards category; Code: class Vehicle { private: int modelno; char name; string regno; public: int getmodel(); int setmodel(); void addregno(): }; class ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    21,326
    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.
    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

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,149
    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, 11:20 AM
  2. Using inheritance.
    By kpridd in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2009, 02: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

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