No. Derived classes use the "Is a" relationship. Using your example, classes derived from vehicles might be Truck, Car, Motorcycle.
A truck "Is a" vehicle, etc.
The seats, windows, tires would be component members which use "Has a" relationship. A vehicle "Has a" seat, window, tires, etc.
Inheritance is not about "Base consists of Derived" (this is called composition). Inheritance is about "Dervied is special kind of Base".
Derived classes for vehicle would be a plane, a car and a boat.
In your example - why would window be derived from vehicle? It may just as well be part of a building - and buildings and vehicles have little in common (maybe except having windows...).
And then each of the car truck trainer etc could have another class called Components like windows
Possible, but not clear what value that adds? Why not simple add members to the appropriate class?
Consider the following contrived example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class vehicle
{
public:
// Attributes of a vehicle. Common to all vehicles
// A vehicle "has" each of the following
int axels;
int horsepower;
int weight;
... etc
};
class Car : public vehicle // Car "is a " vehicle
{
public:
// Attributes unique about a car
string color;
string model;
};
EDIT:
I think the problem here is you are unable to visualize in you mind the relationship between a derived and a base class. A base class is a class upon which other classes build. For example take the vehicle class. A vehicle has say three properties - no. of wheels, engine and body. But still vehicle doesn't serve any practical purpose. So, we need another class which 'contains' all the properties there are in vehicle but also has new properties like color, manufacturer, cost etc. So basically you inherit from a base class only if you new derived class needs properties available in previous classes in addition to its own new ones.
EDIT 2:
Seems like I made a mistake in my answer. Yes car has the 'is-a' relationship with vehicle but i don't know if that qualifies to say that car also is a vehicle.
//Our base class
class Vehicle {
public :
Vehicle() {}
virtualvoid whatDoIHave() = 0;
};
//A car is a vehicle, therefore it inherits all non-private Vehicle properties and members
class Car : public Vehicle {
public :
Car() : Vehicle() {}
void whatDoIHave() {
std::cout << "I have a horn and windows!" << std::endl;
}
};
//A bicycle is a vehicle, therefore it inherits all non-private Vehicle properties and members
class Bicycle : public Vehicle {
public :
Bicycle() : Vehicle() {}
void whatDoIHave() {
std::cout << "I have a bell and chains!" << std::endl;
}
};
A class that was created based on a previously existing class (i.e., base class). A derived class inherits all of the member variables and methods of the base class from which it is derived.
You can access the code in the derived class from the base class code, but only from within an object which is actually a derived class object, and then only if the methods involved are virtual methods.
If you have an object which is itself an instance of the base class, then from within that instance you cannot see derived class code from the base class .
example
public class Baseclass
{
public void Foo()
{
Bar();
}
public virtual void Bar()
{
print("I'm a BaseClass");
}
}
public classs Derived: BaseClass
{
public override void Bar()
{
print("I'm a Derived Class");
}
}
Main()
{
var b = new BaseClass();
x.Foo() // prints "I'm a BaseClass"
// This Foo() calls Bar() in base class
var d = new Derived();
d.Foo() // prints "I'm a Derived Class"
// in above, the code for Foo() (in BaseClass)
// is accessing Bar() in derived class
}