Composition

Here is the code:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <string>
#include <iostream>
using namespace std;

class Engine
	{
		//attributes
	private:
		int size;
		int cyclinders;

		//constructors
	public:
		Engine();
		Engine(int size, int cyclinders);

		//behaviors
		//NONE

		//properties(or Get Set methods)
		int getSize();
		void setSize(int value);
		int getCyclinders();
		void setCyclinders(int value);
	};


	Engine::Engine()
	{
		size = 0;
		cyclinders = 0;
	}

	Engine::Engine(int size, int cyclinders)
	{
		// set given values to the object
		this->size = size;
		this->cyclinders = cyclinders;
	}

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

	void Engine::setSize(int value)
	{
		size = value;
	}

	int Engine::getCyclinders()
	{
		return cyclinders;
	}

	void Engine::setCyclinders(int value)
	{
		cyclinders = value;
	}
//.h file code:



	class RaceCar
	{
		//attributes
	public:
		std::string name;
		Engine *carEngine;

		//constructors
	public:
		RaceCar();

		RaceCar(std::string name, Engine *carEngine);
		RaceCar(std::string name, int size, int cyclinders);

		//behaviors
		//NONE

		//properties(or Get Set methods)
		std::string getName();
		void setName(std::string value);
		Engine *getCarEngine();
		void setCarEngine(Engine *value);
	};

//.cpp file code:

RaceCar::RaceCar()
{
	//set default values
	name = "unknown";
	carEngine = new Engine();
}

RaceCar::RaceCar(std::string name, Engine *carEngine)
{
	// set given values to the object
	this->name = name;
	this->carEngine = carEngine;
}

RaceCar::RaceCar(std::string name, int size, int cyclinders)
{
	// set given values to the object
	this->name = name;
	this->carEngine = new Engine(size, cyclinders);
}

std::string RaceCar::getName()
{
	return name;
}

void RaceCar::setName(std::string value)
{
	name = value;
}

Engine *RaceCar::getCarEngine()
{
	return carEngine;
}

void RaceCar::setCarEngine(Engine *value)
{
	carEngine = value;
}



	static void main()
{
	//Create a RaceCar with an Engine
	RaceCar *car1 = new RaceCar(); // use defaults
	Engine *eng = new Engine(350, 8);
	//RaceCar car2 = new RaceCar("Midnight Blue", eng);
	RaceCar *car2 = new RaceCar("Midnight Blue", new Engine(350, 8));

	//display the values

	
	
	
}


How do I get this output:
Car1
Car Name: unknown
Engine Size 0
Engine Cyclinders 0
Car2
Car Name: Midnight Blue
Engine Size 350
Engine Cyclinders 8
Press any key to continue . . .

What have you tried? Where are you stuck? Can you be a bit more specific, please?

For your current question, the easiest solution would be: replace everything with

1
2
3
4
5
6
7
8
9
std::cout << "Car1" << std::endl;
std::cout << "Car Name: unknown" << std::endl;
std::cout << "Engine Size 0" << std::endl;
std::cout << "Engine Cyclinders 0" << std::endl;
std::cout << "Car2" << std::endl;
std::cout << "Car Name: Midnight Blue" << std::endl;
std::cout << "Engine Size 350" << std::endl;
std::cout << "Engine Cyclinders 8" << std::endl;
std::cout << "Press any key to continue . . ." << std::endl;


:-P

Ciao, Imi.
For one, the main function should return an int and should not be static.
this is what i got so far
1
2
std::cout << "Car1" << "\n\tCar Name: " << car1->getName()<< std::endl;
	std::cout << "Car2" << "\n\tCar Name: " << car2->getName()<< std::endl;			

Output:
Car1
Car Name: unknown
Car2
Car Name: Midnight Blue
Press any key to continue . . .

I need to get and display the values of engine size and cylinders for car 1 and car 2
Last edited on
got it thank you guys

1
2
3
4
5
6
7
std::cout << "Car1" << "\n\tCar Name: " << car1->getName()<< std::endl;
	
	std::cout << "\n\tEngine Size: " << car1->getCarEngine()->getSize()<< std::endl;
	std::cout << "\n\tEngine Cyclinders: " << car1->getCarEngine()->getCyclinders()<< std::endl;
	std::cout << "Car2" << "\n\tCar Name: " << car2->getName()<< std::endl;
	std::cout << "\n\tEngine Size: " << car2->getCarEngine()->getSize()<< std::endl;
	std::cout << "\n\tEngine Cyclinders: " << car2->getCarEngine()->getCyclinders()<< std::endl;


Output:
Car1
Car Name: unknown

Engine Size: 0

Engine Cyclinders: 0
Car2
Car Name: Midnight Blue

Engine Size: 350

Engine Cyclinders: 8
Press any key to continue . . .
Topic archived. No new replies allowed.