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