Like Tree5Likes
  • 3 Post By Neo1
  • 1 Post By laserlight
  • 1 Post By manasij7479

Program of inheritance

This is a discussion on Program of inheritance within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, I am making a program on inheritance, I have made a small program of automobile and want to ...

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

    Program of inheritance

    Hi Guys,

    I am making a program on inheritance, I have made a small program of automobile and want to make sure that 2 cars(base class) inherit the characteristics of the parent class(Vehicle).

    This is the extract from the program:

    Code:
    class Vehicle
    
    {
    
    private:
    
    int modelno;
    char name;
    
    
    public :
    
    int getmodel(); 
    int setmodel();
    
    
    };
    
    class Car:public Vehicle 
    
    {
    
    
    };
    ----------------------------------------------------------------------

    I wanted to know that do I have to define the functions "int getmodel()" and "int setmodel()" in the base class "Car" or it will inherit these functions from the parent class "Vehicle"?

    Will this class Car inherit the data members from the parent class "Vehicle" itself?

    Thanks
    Last edited by student111; 06-06-2012 at 04:25 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by student111 View Post
    I wanted to know that do I have to define the functions "intgetmodel()" and "intsetmodel()" in the base class "Car" or it will inherit these functions from the parent class "Vehicle"?
    Car is not a base class. Veichle is a base class.
    As for your question, Car will inherit them.

    Will this class Car inherit the data members from the parent class "Vehicle" itself?
    Yes, it will inherit them, but it can't access them since they're private.

    Refer to the tutorial for more info:
    Inheritance in C++ Syntax Tutorial - Cprogramming.com
    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.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Yes sorry I made a mistake, I meant that can the derived class (Car) inherit from the base class(Vehicle).


    So do I have to define a function in the child class to make sure that it can access the private data members of the parent class "Vehicle"?

    Thanks

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by student111 View Post
    So do I have to define a function in the child class to make sure that it can access the private data members of the parent class "Vehicle"?
    No, you must do it in the base class (because the derived class does not have access to those members).
    Or, you can use the protected identifier instead of private.
    Nevertheless, it is always a good idea to use getters and setters to all members.
    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.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    If you have more derived class which may inherit the class Vehicle and the behavior of getmodel() and setmodel() you want to design specific to the derived class then your problem has a solution of virtual functions.
    S_ccess is waiting for u. Go Ahead, put u there.

  6. #6
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Elysia View Post
    Nevertheless, it is always a good idea to use getters and setters to all members.
    Wait....what?!

    Surely that should be: Making getters and setters is the last resort, if you can't think of a design where the outside doesn't need direct access to member variables?
    antred, iMalc and manasij7479 like this.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Wait, what? I can't make sense of your statement >_<
    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.

  8. #8
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    249
    He's saying that ideally, users of a class shouldn't require access to a class's internal state at all, neither directly nor through getters and setters, and I'd have to agree with him. If you have a class with tons of getters and setters, that's a pretty good indicator that you need to rethink your design.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    I did not say they have to be public. Furthermore, what these getters and setters actually do is obviously up to the implementation.
    The more getters and setters, the better you are protected against code changes. The tradeoff is time. Don't expose getters and setters to members that should not be public to the outside world.
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,740
    Quote Originally Posted by Elysia
    The more getters and setters, the better you are protected against code changes.
    Not really: there's still the "problem" that member functions and friends don't actually have to use them. I would prefer a minimal but complete interface where member functions and friends are concerned instead to "protect" against changes to the internals of the class. In such a case, you don't really gain much from private getters and setters.
    iMalc likes this.
    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

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    If you have access to internals, you have to use them wisely. Using public functions is preferred over directly manipulating members. It protects more against changes.
    All I am trying to convene is that if you use an extra indirection--that is, getters and setters--you protect yourself from code changes better than if without (this obviously means the code have to use them; otherwise they're render useless). This is strictly different from minimal interfaces and such. It doesn't replace them; it complements them, if you will.
    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.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    O_o

    The others here aren't arguing what you apparently think.

    Using "getters" and "setters" that isolate the underlying type from an interface is great.

    Using "getters" and "setters" that isolate a mechanism by which the underlying type is manipulated is great.

    What everyone is saying is that even other members in the class should be written, if at all possible, in such a way that the mechanic and interface types are properly isolated by intent as a real interface doing a real thing.

    In other words, instead of the function `void OSomeClass::doSomethingFancy()' using `int OSomeClass::Get()' and `void OSomeClass::Set(int)' directly it will use `void OSomeClass::doSomethingAwesome()'. With the interface and implementation so designed changes to members are already isolated by intent; the extra isolation granted by "getters" and "setters" doesn't give you anything.

    Soma

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    Am I the only one wondering how this method will work?

    Code:
    int setmodel();
    Tim S.
    PS: C programmer learning C++.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    Am I the only one wondering how this method will work?
    ^_^;

    Now that you mention it.

    Soma

  15. #15
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    That is easy to explain... the vehicles here are just plain old transformers.
    laserlight likes this.
    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 !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inheritance
    By Aisthesis in forum C++ Programming
    Replies: 8
    Last Post: 09-27-2009, 03:35 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Inheritance and program structure planning please help a newbie
    By ninjacookies in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2005, 12:18 PM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  5. Inheritance?
    By Krak in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2003, 08:32 PM

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