-
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
-
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.
-
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?
-
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.