I have recently created a basic OOP program for my college assignment but am experiencing a very tedious issue that I have been trying to solve.
I have made a two classes, one of which will inherit from its parent (Vehicles, Cars). However, as I will be adding Motorbikes I want to include more specific attributes that identify a car i.e. number of doors. After coding the the classes correctly and the main .cpp file, when I run the program (As you will see in the image below) I keep on receiving negative numbers.
I added virtual to the members of the vehicle class but it says "it's not allowed".
Or if I try to add virtual to the methods in the V class it doesn't change anything.
Hmm right okay but @TheIdeasMan:
If I add _cNumOfDoors into the ":Vehicles(_vMake, _vModel, _vTopSpeed, _vEngineSize, here)", it requires me to add the cNumOfDoors variable inside of my Vehicles Class which would mean that all the vehicles class now contains number of doors, even though later on I plan on adding a motorbike..
~Peter87, I replaced : Car(_cNumOfDoors) with cNumOfDoors(_cNumOfDoors) but I'm left with:
... it requires me to add the cNumOfDoors variable inside of my Vehicles Class which would mean that all the vehicles class now contains number of doors, even though later on I plan on adding a motorbike..
A motorbike has 0 doors so that's not really a problem.
Wow okay sorted it
So I guess when I create the bike class it doesn't mean that I have to use cNumOfDoors? even though its been declared in the base class?
The whole point of all this is your code works on Vehicles. So they take Vehicle* as a parameter and don't know (or care) what kind of Vehicle is being passed in.
The part of code that creates the actual Vehicle* using new Car(...) or new Bike(...) is called a factory.
This means that:
1. You can't use members of the class directly, you have to use methods only.
2. These methods must be declared as virtual functions in Vehicle, which makes them virtual in Car and Bike.
@kbw Oh right okay I see, relatively makes sense, I guess that would be the next step for my program, but I managed to fix the issue I had so can continue to make the bike class now. I appreciate yours, Peters and TheIdeasMan help today. Thank you.