Question
IMPLEMENT OOP IN C++
CAR RENTAL SCHEME
WE WILL SIMULATE A SCHEME WHICH WILL LET US DECIDE THE CONDITION THE CAR IS RECEIVED BACK IN THE COMPANY'S GARAGE. THIS WILL BE A SIMPLE MEMORY GAME, YOUR PERFORMANCE WILL DETERMINE THE CONDITION THE CAR IS RETURNED IN BACK TO THE COMPANY
YOU WILL BE SHOWN A COUPLE OF TRAFFIC STATISTICS FIRST for eg.
*STAT1 - go straight 100m{ * display for 2 seconds, then clear system screen})
*STAT2 - Turn at roundabout , then go left, {display for 2 secs, clear system screen})
and so on, there will be 4 statistics max.
THE VEHICLE WOULD HAVE TO COVER A CERTAIN DISTANCE, AND THROUGHOUT THIS JOURNEY, USER WILL BE QUIZZED REGULARLY ABOUT THE STATISTICS SHOWN PREVIOUSLY (4 questions max.) . A SIMPLE SCORING SYSTEM WILL BE INTRODUCED, WHERE EACH INCORRECT ANSWER WILL RESULT IN THE CAR'S CONDITION DETERIORATING.(eg. if(wrong answer) {car cond-=10;} ) THE USERS CAR SHOULD MOVE ON SMOOTHLY EVEN IF HE ANSWERS INCORRECTLY, AND COMPLETE SPECIFIED DISTANCE FOR EXAMPLE 100 KILOMETERS.
===MY VIEW
I was thinking to maybe approach this by operator overloading , i.e unary overload
eg. ++c1, for first question. --c1 for second question, and so on while a function advances the position ofthe car(A specified length would have to be covered, before game is over.)
IF YOU DRIVE TOO RASHLY( Answer 2 ques. wrong consecutively)---BEWARE!!!- A police car will retrace your path (no graphics of course!) with with greater speed (func. that advances position runs faster). And we can check by binary operator overload , for when distances travelled by police car and user car are same. There culprit will be caught (user car moves no more, stays where it is!)
The user would constantly be updated when faced with a question eg. "u hav covered 45 out of 200 total kilometers, but answer this question NOW! (INSERT QUEST. BASED ON STAT SHOWN PREVIOUSLY)"
TOPICS WE HAVE COVERED: CLASSES AND OBJECTS, OPERATOR OVERLOADING, INHERITANCE, POLYMORPHISM, FUNCTION OVERLOADING, ARRAYS AS CLASS MEMBERS
Explanation / Answer
#if !defined CAR_H #define CAR_H enum TCarType { ctEconomy = 1, ctCompact, ctStandard, ctFullSize, ctMiniVan, ctSUV }; class Car { public: Car(); Car(char *MM, int Y, int Cat); virtual ~Car(); void setMakeModel(const char *mm); char* getMakeModel() const; void setCarYear(const int y); int getCarYear() const; void setCategory(const int c); int getCategory() const; private: char* MakeModel; int CarYear; int Category; }; #endif // CAR_H Source File: car.cpp // Car.cpp: implementation of the Car class. #include #include using namespace std; #include "Car.h" //-------------------------------------------------------------------- Car::Car() { setMakeModel("No Car Selected"); setCarYear(2000); setCategory(1); } //-------------------------------------------------------------------- Car::Car(char *MM, int Y, int Cat) { setMakeModel(MM); setCarYear(Y); setCategory(Cat); } //-------------------------------------------------------------------- Car::~Car() { // delete [] MakeModel; } //-------------------------------------------------------------------- void Car::setMakeModel(const char *m) { MakeModel = new char[strlen(m) + 1]; strcpy(MakeModel, m); } //-------------------------------------------------------------------- char* Car::getMakeModel() const { return MakeModel; } //-------------------------------------------------------------------- void Car::setCarYear(const int y) { CarYear = y; } //-------------------------------------------------------------------- int Car::getCarYear() const { return CarYear; } //-------------------------------------------------------------------- void Car::setCategory(const int c) { Category = c; } //-------------------------------------------------------------------- int Car::getCategory() const { return Category; } //-------------------------------------------------------------------- Header File: customer.h #if !defined CUSTOMER_H #define CUSTOMER_H class Customer { private: char* FirstName; char* LastName; char* Address; char* City; char* State; long ZIPCode; public: void setFirstName(const char *FN); char* getFirstName() const { return FirstName; } void setLastName(const char *LN); char* getLastName() const { return LastName; } char* FullName() const; void setAddress(const char *Adr); char* getAddress() const { return Address; } void setCity(const char *CT); char* getCity() const { return City; } void setState(const char *St); char* getState() const { return State; } void setZIPCode(const long ZIP); long getZIPCode() const { return ZIPCode; } Customer(); Customer(char *FName, char *LName, char *Adr, char *Ct, char *St, long ZIP); Customer(const Customer &Pers); Customer(char * FName, char * LName); ~Customer(); }; #endif // CUSTOMER_H Source File: customer.cpp //--------------------------------------------------------------------------- #include using namespace std; #pragma hdrstop #include "Customer.h" //--------------------------------------------------------------------------- char *Customer::FullName() const { char *FName = new char[40]; strcpy(FName, FirstName); strcat(FName, " "); strcat(FName, LastName); return FName; } //--------------------------------------------------------------------------- Customer::Customer() : ZIPCode(0) { FirstName = new char[20]; strcpy(FirstName, "John"); LastName = new char[20]; strcpy(LastName, "Doe"); Address = new char[40]; strcpy(Address, "123 Main Street Apt A"); City = new char[32]; strcpy(City, "Great City"); State = new char[30]; strcpy(State, "Our State"); } //--------------------------------------------------------------------------- Customer::Customer(char * FName, char * LName) : ZIPCode(0) { FirstName = new char[strlen(FName) + 1]; strcpy(FirstName, FName); LastName = new char[strlen(LName) + 1]; strcpy(LastName, LName); Address = new char[40]; strcpy(Address, "123 Main Street Apt A"); City = new char[32]; strcpy(City, "Great City"); State = new char[30]; strcpy(State, "Our State"); } //--------------------------------------------------------------------------- Customer::Customer(char *FName, char *LName, char *Adr, char *Ct, char *St, long ZIP) : ZIPCode(ZIP) { FirstName = new char[strlen(FName) + 1]; strcpy(FirstName, FName); LastName = new char[strlen(LName) + 1]; strcpy(LastName, LName); Address = new char[40]; strcpy(Address, Adr); City = new char[32]; strcpy(City, Ct); State = new char[30]; strcpy(State, St); } //--------------------------------------------------------------------------- Customer::Customer(const Customer &Pers) : ZIPCode(Pers.ZIPCode) { FirstName = new char[strlen(Pers.FirstName) + 1]; strcpy(FirstName, Pers.FirstName); LastName = new char[strlen(Pers.LastName) + 1]; strcpy(LastName, Pers.LastName); Address = new char[strlen(Pers.Address) + 1]; strcpy(Address, Pers.Address); City = new char[strlen(Pers.City) + 1]; strcpy(City, Pers.City); State = new char[strlen(Pers.State) + 1]; strcpy(State, Pers.State); } //--------------------------------------------------------------------------- void Customer::setFirstName(const char *FN) { strcpy(FirstName, FN); } //--------------------------------------------------------------------------- void Customer::setLastName(const char *LN) { strcpy(LastName, LN); } //--------------------------------------------------------------------------- void Customer::setAddress(const char *Adr) { strcpy(Address, Adr); } //--------------------------------------------------------------------------- void Customer::setCity(const char *CT) { strcpy(City, CT); } //--------------------------------------------------------------------------- void Customer::setState(const char *St) { strcpy(State, St); } //--------------------------------------------------------------------------- void Customer::setZIPCode(const long ZIP) { ZIPCode = ZIP; } //--------------------------------------------------------------------------- Customer::~Customer() { delete [] FirstName; delete [] LastName; delete [] Address; delete [] City; delete [] State; } //--------------------------------------------------------------------------- Header File: Invoice.h #if !defined INVOICE_H #define INVOICE_H #include "Customer.h" #include "Car.h" #include "RentDate.h" class Invoice { public: Invoice(); virtual ~Invoice(); Customer CustomerRegistration(); void CustomerInformation(const Customer& Pers); Car CarSelection(); void CarSelected(const Car& Vehicle); double CalculatePrice(const Car& Vehicle, double& Rate, int &Days); void setMileage(const long g); long getMileage() const; void setTankLevel(const char *v); char* getTankLevel() const; void setCarCondition(const char *c); char* getCarCondition() const; void ProcessOrder(); void CarExamination(); void ShowOrder(); private: long Mileage; char *TankLevel; char *CarCondition; }; #endif // INVOICE_H Source File: Invoice.cpp // Invoice.cpp: implementation of the Invoice class. // ////////////////////////////////////////////////////////////////////// #include #include #include using namespace std; #include "Invoice.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// Invoice::Invoice() { setTankLevel("Empty"); setCarCondition("Good"); } //-------------------------------------------------------------------- Invoice::~Invoice() { } //-------------------------------------------------------------------- Customer Invoice::CustomerRegistration() { char FName[20], LName[20], Addr[40], CT[32], St[30]; long ZC = 0; cout FName; cout > LName; cout > ws; cin.getline(Addr, 40); cout