Write a C++ programme, (Such as VW, Mazda etc) owner, model (such as Golf,Polo e
ID: 3727575 • Letter: W
Question
Write a C++ programme, (Such as VW, Mazda etc) owner, model (such as Golf,Polo etc) year of registration, weight,color country of origin. Create a Car class,with getter and setter methods for private attributes. Have an array of Car objects for keeping car informations. The array should be able to contain 100 cars.Initialize the array with 10 cars.
you program should have the following menu structure:
> Add a new car.
> Find cars owned by person and show all the informations about those cars.
> Show all cars (all the informations about them)
> Modify a car record: input the licence plate number, ask the field to modify, and get the new value from the user. Modify the record accordingly.
> Delete a car with a specific licence plate number.
> Read cars from a file.
> Write cars to a file.
> Quit.
Explanation / Answer
main.cpp
#include <iostream>
#include "car.h"
using namespace std;
int main()
{
car MyCar;
int randomCrapIDontCareAbout;
cout << "OOP Code Challenge - Cars" << endl;
MyCar.SetColour(4); // 1-4 only
MyCar.SetMake(3); // 1-4 only
MyCar.SetDirection(2); // 1-4 only
MyCar.SetModel(1); // 1-4 only
MyCar.SetSpeed(0); // 1-200 only
cout << "Initial Values:" << endl;
MyCar.PrintCurrentCar();
cout << "Step 1 - From rest, accelerate to 60km/h"<<endl;
MyCar.SetSpeed(60);
MyCar.PrintCurrentCar();
cout << "Step 2 - Make a 90-degree left-hand turn" << endl;
MyCar.ChangeDirection(-1);
MyCar.PrintCurrentCar();
cout << "Step 3 - Make a 90-degree right-hand turn" << endl;
MyCar.ChangeDirection(+1);
MyCar.PrintCurrentCar();
cout << "Step 4 - Accelerate to 90km/h" << endl;
MyCar.SetSpeed(90);
MyCar.PrintCurrentCar();
cout << "Step 5 - Brake to 0km/h" << endl;
MyCar.SetSpeed(0);
MyCar.PrintCurrentCar();
cin >> randomCrapIDontCareAbout;
return 0;
}
car.cpp
#include "car.h"
car::car()
{
//ctor
SetColour(1);
SetDirection(1);
SetMake(1);
SetModel(1);
}
car::~car()
{
//dtor
}
car.h
#ifndef CAR_H
#define CAR_H
#include <iostream>
using namespace std;
class car
{
public:
car();
virtual ~car();
void PrintCurrentCar() {
cout << "COLOUR: ";
GetColour();
cout << endl << "MAKE: ";
GetMake();
cout << endl << "DIRECTION: ";
GetDirection();
cout << endl << "MODEL: ";
GetModel();
cout << endl << "SPEED: ";
GetSpeed();
cout << endl << "-----------------------------------------" << endl;
}
unsigned int GetColour() {
switch (m_Colour) {
case 1:
cout << "Red";
break;
case 2:
cout << "Blue";
break;
case 3:
cout << "Green";
break;
case 4:
cout << "Yellow";
break;
default:
cout << "Invalid";
return false;
}
return true;
}
void SetColour(unsigned int val = 1) {
if ((val > 4) || (val < 1)) {
cout << "Error assigning colour. Use numbers 1-4 only";
} else {
m_Colour = val;
}
}
unsigned int GetMake() {
switch (m_Make) {
case 1:
cout << "Ford";
break;
case 2:
cout << "Honda";
break;
case 3:
cout << "Toyota";
break;
case 4:
cout << "Pontiac";
break;
default:
cout << "Invalid";
return false;
}
return true;
}
void SetMake(unsigned int val = 1) {
if ((val > 4) || (val < 1)) {
cout << "Error assigning Make. Use numbers 1-4 only";
} else {
m_Make = val;
}
}
unsigned int GetModel() {
switch (m_Model) {
case 1:
cout << "Truck";
break;
case 2:
cout << "Car";
break;
case 3:
cout << "Van";
break;
case 4:
cout << "Super Bike";
break;
default:
cout << "Invalid";
return false;
}
return true;
}
void SetModel(unsigned int val = 1) {
if ((val > 4) || (val < 1)) {
cout << "Error assigning Model. Use numbers 1-4 only";
} else {
m_Model = val;
}
}
unsigned int GetSpeed() {
cout << m_Speed << "km/h";
return true;
}
void SetSpeed(unsigned int val) {
if (val > 200) {
cout << "Too fast (max speed: 200km/h)";
} else {
m_Speed = val;
}
}
unsigned int GetDirection() {
switch (m_Direction) {
case 1:
cout << "North";
break;
case 2:
cout << "East";
break;
case 3:
cout << "South";
break;
case 4:
cout << "West";
break;
default:
cout << "Invalid";
return false;
}
return true;
}
void SetDirection(unsigned int val = 1) {
m_Direction = val;
}
void ChangeDirection(int val = 1) {
int tempDirection = m_Direction + val;
if (tempDirection > 4) {
tempDirection -= 4;
}
if (tempDirection < 1) {
tempDirection += 4;
}
SetDirection(tempDirection);
}
protected:
private:
unsigned int m_Colour;
unsigned int m_Make;
unsigned int m_Model;
unsigned int m_Speed;
unsigned int m_Direction;
};
#endif // CAR_H
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.