About inheritance.

This is a discussion on About inheritance. within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, I want to know about inheritance in a program of vehicle which I had before. The program is ...

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

    About inheritance.

    Hi Guys,

    I want to know about inheritance in a program of vehicle which I had before.

    The program is as:
    --------------------------------------------------------------------
    Code:
    class Vehicle
    
    {
    private:
    
    char name;
    int regno;
    
    public:
    void getname();
    void regno();
    
    
    };
    
    class Car :Vehicle
    
    {
    
    public:
    ------
    
    }
    --------------------------------------------------------------------------

    I wanted to know that if we just write the child class for car as "class Car: Vehicle" and don't use "public" in this code,then will the child class be able to access the public members of the class "Vehicle"?


    Also do child classes only access the public members of the parent class?



    Thanks
    Last edited by student111; 06-21-2012 at 12:30 PM. Reason: correcting and adding more

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Default inheritance is private, thus:
    Code:
    class Car : Vehicle
    ...can be looked at as the same as:
    Code:
    class Car : private Vehicle
    Car will only have access to Vehicle's public or protected members. Car does not get access to any of Vehicle's private members. ... I think I've got that right.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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

    thanks for the reply so can the child class "Car" use the member functions of the class "Vehicle" to access its private data members?
    Last edited by student111; 06-21-2012 at 02:21 PM. Reason: rephrasing

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    The different levels of inheritance only affects how the outside can access the members inherited.
    In public, the access levels are unchanged.
    In protected, all public members become protected and the rest is the same.
    In private, all protected and public members become private.
    Regardless of inheritance type, the child class can access and use all public and protected members of the parent class.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. oop: inheritance
    By hilarius in forum C++ Programming
    Replies: 4
    Last Post: 12-30-2009, 05:43 PM
  2. Using inheritance.
    By kpridd in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2009, 03:20 PM
  3. Inheritance
    By donkeyass in forum C++ Programming
    Replies: 7
    Last Post: 10-18-2007, 01:28 PM
  4. Inheritance
    By Bones in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2003, 05:24 PM
  5. Inheritance
    By DrunkenTiger in forum C++ Programming
    Replies: 7
    Last Post: 08-28-2003, 01:31 AM

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