Base Class And Derived Class

So I'm starting to learn simple inheritance with classes can someone clarify if my view on base class and derved class are correct


So this is what I think these two concepts are

If we have a class called vehicles this would be our base class

Our derived clad would be something like seats windows tires speed etc....


How I think of how dervied and base classes are I'm I right or wrong
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...).
Last edited on
Thank you so much both of you so basically

It would be like this

Base class--Vehicle

And this would have car track etc

And then each of the car truck trainer etc could have another class called Components like windows

And btw I'm not asking about inheritance I just want to get my knowledge straight
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;
};

So on the second class is that saying car is part of vechile and what is inheritance

And where can I find a clear tutorial on this
So on the second class is that saying car is part of vechile

No, it's saying a car _IS_ a vehicle.
Mizfizz wrote:
So on the second class is that saying car is part of vechile and what is inheritance


Actually no. It means that car ' has derived ' or ' inherited ' or ' got ' the properties of class vehicle, in addition to its own properties.

This site also contains a very nice basic tutorial on inheritance
http://www.cplusplus.com/doc/tutorial/inheritance/

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.
Last edited on
A car is a vehicle.
A car has windows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Our base class
class Vehicle {
public :
	Vehicle() {}

	virtual void 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;
	}
};
Thx so is a member function a function in a class
Yes.

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
}
Topic archived. No new replies allowed.