Classes that contain instances of another class

We had to write a program for an assignment using classes to set different information for a boat and engine class. Well I wrote the program and I thought that it was all well until my teacher told me I didn't meet some of his specifications. He said he wanted my boat class to contain two instances of my engine class and to not use inheritance. Another guideline he gave me was to write a function in boat that prints out all of boats information and all of engines information.
I thought everything was pretty easy until he told me all of this. I am really lost on writing a class inside another class and being able to set and print out all of the information, especially without being able to use inheritance. I am new to all of these also so I am pretty shaky on all of this. Here is my current code that I made before he told me how he wants it. I need to modify this to fit his guidelines:

engine.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef engine_h
#define engine_h
#include <string>
using namespace std;
class engine
{
private:
	int horsepower;
	int fuelcapacity;
	string diesel;
	int year;
public:
	void printengine();
	void sethorsepower(int);
	void setfuelcapacity(int);
	void setdiesel(string);
	void setyear(int);
	int gethorsepower();
	int getfuelcapacity();
	string getdiesel();
	int getyear();
};
#endif 


engine.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "engine.h"
#include <iostream>
#include <string>
using namespace std;
//setters
void engine::sethorsepower(int ahorsepower)
{
	horsepower = ahorsepower;
}
void engine::setfuelcapacity(int afuelcapacity)
{
	fuelcapacity = afuelcapacity;
}
void engine::setdiesel(string adiesel)
{
	diesel = adiesel;
}
void engine::setyear(int ayear)
{
	year = ayear;
}
//getters
int engine::gethorsepower()
{
	return horsepower;
}
int engine::getfuelcapacity()
{
	return fuelcapacity;
}
string engine::getdiesel()
{
	return diesel;
}
int engine::getyear()
{
	return year;
}

void engine::printengine()
{
	cout << "Horsepower: " << horsepower << endl;
	cout << "Fuel Capacity: " << fuelcapacity << " Gallons" << endl;
	cout << "Diesel or Nondiesel: " << diesel << endl;
	cout << "Year: " << year << endl;
}


boat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef boat_h
#define boat_h
#include "engine.h"
#include <string>
using namespace std;
class boat:public engine
{
private:
	string make;
	string model;
	string color;
public:
	void setmake(string);
	void setmodel(string);
	void setcolor(string);
	string getmake();
	string getmodel();
	string getcolor();
	void printboat();
};
#endif 


boat.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
32
33
34
35
36
37
38
#include "boat.h"
#include <iostream>
#include <string>
using namespace std;
//setters
void boat::setmake(string amake)
{
	make = amake;
}
void boat::setmodel(string amodel)
{
	model = amodel;
}
void boat::setcolor(string acolor)
{
	color = acolor;
}
//getters
string boat::getmake()
{
	return make;
}
string boat::getmodel()
{
	return model;
}
string boat::getcolor()
{
	return color;
}

void boat::printboat()
{
	cout << "Make: " << make << endl;
	cout << "Model: " << model << endl;
	cout << "Color: " << color << endl;
	printengine();
}


driver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "boat.h"
#include <iostream>
#include <string>
using namespace std;
void main()
{
	boat myboat;
	myboat.setmake("Bass Hunter");
	myboat.setmodel("BH-007");
	myboat.setcolor("Green");
	myboat.sethorsepower(500);
	myboat.setfuelcapacity(50);
	myboat.setdiesel("Diesel");
	myboat.setyear(2013);
	myboat.printboat();
}


Sorry that it's long! I really appreciate any help that is offered. I tried asking my teacher but he really wouldn't help me at all..
closed account (D80DSL3A)
ngo93 wrote:
He said he wanted my boat class to contain two instances of my engine class and to not use inheritance.


A boat isn't a special type of engine, so it wouldn't be derived from engine.
A boat has engines. Include 2 instances of an engine as data members of the boat class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class boat
{
private:
	string make;
	string model;
	string color;
        engine engine1, engine2;// now the boat has 2 engines.
public:
	void setmake(string);
	void setmodel(string);
	void setcolor(string);
	string getmake();
	string getmodel();
	string getcolor();
	void printboat();// this could call the printengine() function for each engine.
};
After I input that engine1 and engine2 within my boat class, I am not sure how to set values to each of the things inside engine1 and engine2 (such as horsepower, fuelcapacity, etc.). If I try anything it just says it cannot be accessed since I cannot use inheritance.
That's because fun2code made the engine instances private. You should either set those attributes in the constructor or make them public.
This may help:

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
// http://www.cplusplus.com/doc/tutorial/classes/
#include <iostream>
using namespace std;

class CFirst {
public:
    void print1() { cout<<"I'm the First Class."<<endl; }
};

class CSecond {
public:
    void print2() { cout<<"I'm the Second Class."<<endl; }
    CFirst obj1;
};

int main ()
{

    CSecond obj2;
    obj2.obj1.print1();

    CSecond * p_obj;
    p_obj->obj1.print1();


    CSecond obi;
    obi.print2();


    return 0;
}
I'm the First Class.
I'm the First Class.
I'm the Second Class.
Topic archived. No new replies allowed.