A code challenge between some friends, as I came from a Procedural way of coding, instead of using Objects. The paramaters are below:
- store the properties of a car
- colour
- make
- model
- speed
- direction
- demonstrate how these properties are accessed
- manipulate these properties through standard calls (functions/methods)
- from rest, accelerate to 60km/h
- make a 90-degree left-hand turn
- make a 90-degree right-hand turn
- accelerate to 90km/h
- brake to 0km/h
I am not used to C++, but decided to try it with some tutorials. I do not think I'm doing it correctly though, looking at other examples. Am I demonstrating how the properties are accessed properly? Am I using the getters and setters correctly?
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
GetColour
prints the colour, so call itPrintColour
. Same for the other methods called GetSomething. (This is not OOP-specific) \$\endgroup\$