Inheritance, overriding methods and basic UI help!
Apr 14, 2014 at 10:23pm UTC
Hi guys,
So I'm studying computer science and I've been given a (pretty basic) assignment, which I can't get my head around at all!!
The tasks are as follows:
1. Implement the outlined class structure including the attributes and methods as detailed in the class diagrams.
2. Override the getVechicleDetails method so that you can create a vehicle and pass the required information to the method.
And this is the class structure i've been given:
http://puu.sh/88p8O/dde80163ad.png
With these methods:
http://puu.sh/88p9X/a46d0cfd1a.png
I did this correctly, However, the extension task in this assignment I feel makes the task 2 redundant:
http://puu.sh/88pcY/e50d793753.png
Any ideas how I could implement this basic console UI which the extension task is asking for? I'm completely stuck!!
Thanks.
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
using namespace std;
class vehicle {
public :
string manufacturer;
int year;
string regnum;
void getVehicleDetails() {
cout << "Please enter the details for your vehicle" << endl;
cout << "Please enter the manufacturer of your vehicle: " << endl;
cin >> manufacturer;
cout << "Please enter the year of your vehicle's manufacture: " << endl;
cin >> year;
cout << "Please enter your vehicle's registration number: " << endl;
cin >> regnum;
cout << "Thank your for your details" << endl;
}
void printVehicleDetails() {
cout << "Your vehicle's details are as follows: " << endl;
cout << "Your Vehicle's manufacturer is " << manufacturer << endl;
cout << "Your Vehicle's year of manufacture is " << year << endl;
cout << "Your Vehicle's registration number is " << regnum << endl;
}
void saveVehicleDetails() {
ofstream vehiclefile;
vehiclefile.open ("vehicle.txt" );
vehiclefile << "***Your Vehicle's Details***" << endl;
vehiclefile << "Manufacturer:" << manufacturer << endl;
vehiclefile << "Year of Manufacture:" << year << endl;
vehiclefile << "Registration Number: " << regnum << endl;
vehiclefile.close();
}
void openVehicleDetails() {
}
};
class car : public vehicle{
public :
int numpassengers;
string cartype;
void getCarDetails() {
cout << "Please enter the details for your Car" << endl;
cout << "Please enter the number of maximum passengers your car can hold: " << endl;
cin >> numpassengers;
cout << "Please enter the car body type: " << endl;
cin >> cartype;
cout << "Thank your for your details" << endl;
}
void printCarDetails() {
cout << "Your car's details are as follows: " << endl;
cout << "Your car's maximum passengers is: " << numpassengers << endl;
cout << "The body type of your car is: " << cartype << endl;
}
void saveCarDetails() {
ofstream vehiclefile;
vehiclefile.open ("vehicle.txt" );
vehiclefile << "Car or Lorry: Car" << endl;
vehiclefile << "Number of passengers: " << numpassengers << endl;
vehiclefile << "Type of car: " << cartype << endl;
vehiclefile.close();
}
};
class lorry : public vehicle{
public :
double tonnage;
string bodtype;
void getLorryDetails() {
cout << "Please enter the details for your Lorry" << endl;
cout << "Please enter the gross weight of your Lorry: " << endl;
cin >> tonnage;
cout << "Please enter the body type of your Lorry: " << endl;
cin >> bodtype;
cout << "Thank your for your details" << endl;
}
void printLorryDetails() {
cout << "Your lorry's details are as follows: " << endl;
cout << "Your lorry's maximum weight is: " << tonnage << endl;
cout << "The body type of your lorry is: " << bodtype << endl;
}
void saveLorryDetails() {
ofstream vehiclefile;
vehiclefile.open ("vehicle.txt" );
vehiclefile << "Car or Lorry: Lorry" << endl;
vehiclefile << "Maximum weight: " << tonnage << endl;
vehiclefile << "Body type: " << bodtype << endl;
vehiclefile.close();
}
};
void vehiclemenu();
void carmenu();
void lorrymenu ();
int main () {
int flag = 0;
while (flag == 0) {
char choice;
cout << "***Main Menu***" << endl; //Menu to allow ease of access within the program.
cout << "Select by letter:" << endl;
cout << "1 - Vehicle" << endl;
cout << "2 - Lorry" << endl;
cout << "3 - Car" << endl;
cout << "4 - Quit" << endl;
cin >> choice;
switch (choice) {
case '1' :
vehiclemenu();
break ;
case '2' :
lorrymenu();
break ;
case '3' :
carmenu();
break ;
case '4' :
flag = 1;
break ;
default :
cout << "\nInvalid selection. Press a key to return to main menu." ;
}
}
if (flag == 1) {
return 0;
}
}
void vehiclemenu() {
char choice;
int ifchoice = 0;
vehicle veh;
cout << "***Main Menu***" << endl; //Menu to allow ease of access within the program.
cout << "Select by letter:" << endl;
cout << "1 - Add new entry" << endl;
cout << "2 - Show entry" << endl;
cout << "3 - Save entry" << endl;
cout << "4 - Open saved document" << endl;
cout << "5 - Delete entry" << endl;
cin >> choice;
switch (choice) {
case '1' :
cout << "Is your vehicle a Car or a Lorry? " << endl;
cout << "Press '1' for Car " << endl;
cout << "Press '2' for Lorry " << endl;
cin >> ifchoice;
if (ifchoice == 1)
{
carmenu();
}
if (ifchoice == 2)
{
lorrymenu();
}
break ;
case '2' :
veh.printVehicleDetails();
break ;
case '3' :
veh.saveVehicleDetails();
break ;
case '4' :
veh.openVehicleDetails();
break ;
case '5' :
veh.~vehicle();
break ;
default :
cout << "\nInvalid selection. Press a key to return to main menu." ;
}
}
void carmenu() {
char choice;
car car;
vehicle veh;
cout << "***Main Menu***" << endl; //Menu to allow ease of access within the program.
cout << "Select by letter:" << endl;
cout << "1 - Add new entry" << endl;
cout << "2 - Show entry" << endl;
cout << "3 - Save entry" << endl;
cout << "4 - Open saved document" << endl;
cout << "5 - Delete entry" << endl;
cin >> choice;
switch (choice) {
case '1' :
car.getVehicleDetails();
car.getCarDetails();
break ;
case '2' :
car.printVehicleDetails();
car.printCarDetails();
break ;
case '3' :
car.saveVehicleDetails();
car.saveCarDetails();
break ;
case '4' :
car.openVehicleDetails();
break ;
case '5' :
veh.~vehicle();
break ;
default :
cout << "\nInvalid selection. Press a key to return to main menu." ;
}
}
void lorrymenu() {
char choice;
lorry lor;
vehicle veh;
cout << "***Main Menu***" << endl; //Menu to allow ease of access within the program.
cout << "Select by letter:" << endl;
cout << "1 - Add new entry" << endl;
cout << "2 - Show entry" << endl;
cout << "3 - Save entry" << endl;
cout << "4 - Open saved document" << endl;
cout << "5 - Delete entry" << endl;
cin >> choice;
switch (choice) {
case '1' :
lor.getVehicleDetails();
lor.getLorryDetails();
break ;
case '2' :
lor.printLorryDetails();
break ;
case '3' :
lor.saveLorryDetails();
break ;
case '4' :
lor.openVehicleDetails();
break ;
case '5' :
veh.~vehicle();
break ;
default :
cout << "\nInvalid selection. Press a key to return to main menu." ;
}
}
Apr 14, 2014 at 11:26pm UTC
I'm not asking for direct 'HOW DO I DO THIS'
Just, a pointer in the right direction, as I am completely confused.
Apr 15, 2014 at 2:53am UTC
Are you asking how to make the sentinel controlled loop?
If so just create a infinite loop with a menu item that once chosen will break the loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
for (;;)
{
// Do Stuff
if (/* user controlled condition*/ )
{
break ;
}
}
// Or while
while (1)
{
// Do Stuff
if (/* user controlled condition*/ )
{
break ;
}
}
ya looking at the program it does need a sentinel controlled loop
Last edited on Apr 15, 2014 at 3:03am UTC
Topic archived. No new replies allowed.