Class inheritance problem

I am creating a small program to practice inheritance etc and I am having a issue that I can not figure out. I have an engine class which I can create with its variables fine, and print it all fine. But when I create the engine, and use it in a truck class (which inherits from vehicle) when i try and print the engine information then, it get no errors from the compiler, but what acutally prints to the console is 'round _alloca memory reserved by this function is corrupted' for one of the variables, and a symbol for the other. But one of the variables prints just fine. All the code is below.

I have also added what is actually printed to console at the very bottom, you can also see there, on one of the lines the full cout statement doesnt print either for some reason.


engine h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #pragma once
#include <iostream>
using std::string;

class engine
{
private:
	string make;
	int RPM;
	int size;

public:
	engine(string P_make, int P_RPM, int P_size) : make(P_make), RPM(P_RPM), size(P_size) {}
	string getMake();
	int getRPM();
	int getSize();
};


engine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "engine.h"

string engine::getMake()
{
	return make;
}

int engine::getRPM()
{
	return RPM;
}

int engine::getSize()
{
	return size;
}


vehicle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include <iostream>
#include "engine.h"

using std::string;

class vehicle
{
private:
	string name;
	string colour;
	int doors;
	engine Engine;

public:
	vehicle(string P_name, string P_colour, int P_doors, engine P_engine) : name(P_name), colour(P_colour), doors(P_doors), Engine(P_engine)  {}

	string getName();
	string getColour();
	int getDoors();
	engine getEngine();
	void getDetails();
};

vehicle.cpp
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
#include "vehicle.h"

string vehicle::getName()
{
	return name;
}

string vehicle::getColour()
{
	return colour;
}

int vehicle::getDoors()
{
	return doors;
}

engine vehicle::getEngine()
{
	return Engine;
}

void vehicle::getDetails()
{
	std::cout << "Name: " + getName() << std::endl;
	std::cout << "Engine size: " + getEngine().getSize() << std::endl;
	std::cout << "Engine make: " + getEngine().getMake() << std::endl;
	std::cout << "Engine RPM: " + getEngine().getRPM() << std::endl;
	std::cout << "Colour: " + getColour() << std::endl;
	std::cout << "Number of doors: " + getDoors() << std::endl;
}


truck.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include "vehicle.h"

class truck : public vehicle
{
private:
	bool hasBed;
	string bedType;

public:
	truck(bool P_hasBed, string P_bedType, string P_name, string P_colour, int P_doors, engine P_engine) : hasBed(P_hasBed), bedType(P_bedType), vehicle(P_name, P_colour, P_doors, P_engine)
	{}

	truck(bool P_hasBed, string P_name, string P_colour, int P_doors, engine P_engine) : hasBed(P_hasBed), vehicle(P_name, P_colour, P_doors, P_engine)
	{}

	void getDetails();
};


truck.cpp
1
2
3
4
5
6
7
8
9
10
#include "truck.h"

void truck::getDetails()
{
	vehicle::getDetails();
	if (hasBed)
		std::cout << "Bed type: " << bedType << "\n";
}



main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "truck.h"

int main()
{
	engine Engine("Ford", 8000, 1800);
	truck T1(true, "Flat", "Jeep", "Black", 5, Engine);

	std::cout << Engine.getMake() << '\n';
	std::cout << Engine.getRPM() << '\n';
	std::cout << Engine.getSize() << '\n';
	
	T1.getDetails();
}


And what prints is :
Ford
8000
1800
Name: Jeep
round _alloca memory reserved by this function is corrupted
Engine make: Ford

Colour: Black
r of doors:
Bed type: Flat



Your problem is here:

1
2
3
4
5
6
7
8
9
void vehicle::getDetails()
{
	std::cout << "Name: " + getName() << std::endl;
	std::cout << "Engine size: " + getEngine().getSize() << std::endl;
	std::cout << "Engine make: " + getEngine().getMake() << std::endl;
	std::cout << "Engine RPM: " + getEngine().getRPM() << std::endl;
	std::cout << "Colour: " + getColour() << std::endl;
	std::cout << "Number of doors: " + getDoors() << std::endl;
}

In this code, you are adding an integer offset (1800) to the address of a string literal:

 
	std::cout << "Engine size: " + getEngine().getSize() << std::endl;

You add integers to string literals in a couple of other places, too. For instance, adding 5 (getDoors()) to "Number of doors: " ends up printing "r of doors: ".

You want to do is this:

1
2
3
4
5
6
7
8
9
void vehicle::getDetails()
{
	std::cout << "Name: "            << getName()             << '\n';
	std::cout << "Engine size: "     << getEngine().getSize() << '\n';
	std::cout << "Engine make: "     << getEngine().getMake() << '\n';
	std::cout << "Engine RPM: "      << getEngine().getRPM()  << '\n';
	std::cout << "Colour: "          << getColour()           << '\n';
	std::cout << "Number of doors: " << getDoors()            << '\n';
}

Oh of course, that was stupid of me. thank you
Topic archived. No new replies allowed.