Issues with basic OOP Program

Hello

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.

Vehicles.h (File): http://pastebin.com/kV2fSRGz

Vehicles.cpp (File): http://pastebin.com/cgGrAF0f

Car.h (File): http://pastebin.com/mCUAm3Jq

Car.cpp (File): http://pastebin.com/z38G6jeL

main.cpp (File): http://pastebin.com/eiJYTPyr

I also want the number of doors data to be entered by the user, but whenever I enter a num it goes to this:

http://puu.sh/mTvwR/35357f48aa.png


It would be much appreciated if there is somebody who can help me out.
Thanks guys!

- Tyrel
Last edited on
The members of Vehicles must be virtual so they can be overridden by derived classes, including the destructor.
Right.. okay,

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.
You forgot to initialize cNumOfDoors in the Car constructor.
Okay I understand but how do I initialize it within the other constructor.

Car::Car(string _vMake, string _vModel, double _vTopSpeed, double _vEngineSize, int _cNumOfDoors)
:Vehicles(_vMake, _vModel, _vTopSpeed, _vEngineSize)
: Car(_cNumOfDoors)
{
}
Last edited on
Hi,

As Peter87 is saying, you have a parameter, but you didn't set it in the member initialiser list

Car::Car(string _vMake, string _vModel, double _vTopSpeed, double _vEngineSize, int _cNumOfDoors) <---
:Vehicles(_vMake, _vModel, _vTopSpeed, _vEngineSize, here)

Btw, your string parameters should be const and passed by reference. _cNumOfDoors should be unsigned

Hope this helps a little bit.
Last edited on
Use , cNumOfDoors(_cNumOfDoors) instead of : Car(_cNumOfDoors).
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:

1
2
3
4
5
6
Car::Car(string _vMake, string _vModel, double _vTopSpeed, double _vEngineSize, int _cNumOfDoors)
	:Vehicles(_vMake, _vModel, _vTopSpeed, _vEngineSize, _cNumOfDoors)
	:cNumOfDoors(_cNumOfDoors)
{

}
The second colon should be a comma.

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

Your code should look something like:
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
28
29
30
31
32
33
34
35
36
37
38
39
#include "vehicles.h"
#include "Car.h"
#include "Bike.h"
#include <string>

Vehicle* CreateCar();
Vehicle* CreateBike();

using namespace std;

int main()
{
	Vehicle* v = nullptr;

	do
	{
		cout << "Select one of the options below to create the vehicle below: " << endl;
		cout << "[1] - Car" << endl;
		cout << "[2] - Bike" << endl;
		cout << endl;

		int vOption;
		cout << "Choice: ";
		cin >> vOption;

		// Create a Vehicle
		if (vOption == 1)
			v = CreateCar();
		else if (vOption == 1)
			v = CreateBike();

		// Use Vehicle
		v->display();

		// Manual cleanup for now
		delete v; v = nullptr;
	}
	while (true); // for now
}
Last edited on
@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.
Topic archived. No new replies allowed.