File ---> "Car.h"
#pragma once
class Car {
public: // Programmer Interface (programmer can access/use these).
Car (void); // Default constructor.
Car (int, char*, int); // Constructor
Car (int, char*);
// Accesors
int getYearmodel(void) const;
char* getMake (void) const;
int getSpeed (void) const;
void accelerate (void); // accelerate() method
void brake (void); // brake() method
~Car (void); // Destructor.
private: // Data members not accessible by programmer.
int yearModel;
char* Make;
int Speed;
};
File ---> "Car.cpp"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "Car.h"
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
//---------------------------------------------------------------------------
const int MAKE_SIZE = 20; // Defining Max size for make of the car.
const int MAX_SPEED = 140;
//---------------------------------------------------------------------------
// Define member functions for class Car.
Car::Car (void) { // Default constructor. Assigning zero to all.
yearModel = 0;
Make = new char [MAKE_SIZE];
*Make = '\0';
Speed = 0;
}
Car::Car(int year, char* car_name, int speed) { // Constructor taking 3-parameters.
yearModel = year;
Make = new char [strlen(car_name) + 1];
strcpy (Make, car_name);
Speed = speed;
}
Car::Car(int year, char* car_name) {
yearModel = year;
Make = new char [strlen(car_name) + 1];
strcpy(Make, car_name);
Speed = 0;
}
int Car::getYearmodel(void) const { // Returns year of car.
return yearModel;
}
char* Car::getMake(void) const { // Return make of car.
return Make;
}
int Car::getSpeed(void) const { // Return speed of car
return Speed;
}
void Car::accelerate(void) { // Adds 5 to speed variable.
Speed += 5;
if (Speed > MAX_SPEED) {
Speed = MAX_SPEED;
}
}
void Car::brake(void) { // Subtracts 5 to speed variable.
Speed -= 5;
if (Speed < 0) {
Speed = 0;
}
}
//---------------------------------------------------------------------------
Car::~Car (void) { // Destructor.
delete [] Make;
}
//---------------------------------------------------------------------------
File --> Main
//------------------------------------------------------------------------------
// Date/Time: 5/3/2010 11:53:09 AM
//------------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "extras.h"
#include "Car.h"
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
//---------------------------------------------------------------------------
const int CAR_SIZE = 20; // Defining Max size for make of the car.
//---------------------------------------------------------------------------
// Structure Declarations
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Function Prototypes
//---------------------------------------------------------------------------
bool Do_Again (void);
//---------------------------------------------------------------------------
int main (void) {
cout.setf (ios::fixed, ios::floatfield);
cout.setf (ios::showpoint);
cout.precision (2);
do {
char typeCar[CAR_SIZE]; // Character Array.
int yearCar; // Integer variable.
int speedCar;
int userInput = 0;
char userResponse;
cout << endl << endl << "Simple Mechanics";
cout << endl << "----------------";
cout << endl << "Enter Car Type: ";
cin.getline(typeCar, CAR_SIZE); // Getting car type in an array.
cout << endl << "Enter Car Year: ";
cin >> yearCar; // Getting year of the car.
cout << endl << "Enter Car Speed: ";
cin >> speedCar; // Getting Speed of the car.
while (speedCar < 0) {
// Unless the user types valid speed it prompts the user to type in
// the speed of car in infinite loop.
cout << endl << "Invalid Car Speed :: Enter Positive Car Speed: ";
cin >> speedCar;
}
Car CarObject(yearCar, typeCar, speedCar);
// Creating the object of a Car Class.
cout << endl << "Do you want to increase the speed of your car? " << endl;
cout << "Press Y or y to increse you speed or PRESS ANY KEY not to...."
<< endl;
cin >> userResponse;
while (userResponse == 'y' || userResponse == 'Y') {
if (userInput >= 0 || userInput <= 100) {
cout << endl << "How many times you want to accelarate you car?" << endl;
cin >> userInput;
for (int i = 0; i < userInput; ++i) {
CarObject.accelerate();
}
// Displaying the Current speed of the car after acceleration.
cout << endl << "Current Speed :: After Acceleration: "
<< CarObject.getSpeed() << " Miles/Hr";
cout << endl << endl << "Do you want to increase the speed of your car?";
cout << endl << "Press Y or y to increse you speed or PRESS ANY KEY...." << endl;
cin >> userResponse;
}
}
cout << endl << "Do you want to use the BREAK? " << endl;
cout << "Press Y or y to use the BREAK or PRESS ANY KEY not to...."
<< endl;
cin >> userResponse;
while (userResponse == 'y' || userResponse == 'Y') {
if (userInput >= 0 || userInput <= 100) {
cout << endl << "How many times you want to use BREAK?" << endl;
cin >> userInput;
for (int i = 0; i < userInput; ++i) {
CarObject.brake();
}
// Displaying the Current speed of the car after acceleration.
cout << endl << "Current Speed :: After BREAK Applied: "
<< CarObject.getSpeed() << " Miles/Hr";
cout << endl << endl << "Do you want to use the BREAK?";
cout << endl << "Press Y or y to use the BREAK or PRESS ANY KEY not to...." << endl;
cin >> userResponse;
}
}
cout << endl << endl << "Vehicle Information!";
cout << endl << "--------------------";
// Displaying Car Information typed by user.
cout << endl << "CAR TYPE: " << CarObject.getMake();
cout << endl << "CAR YEAR: " << CarObject.getYearmodel();
cout << endl << "CURRENT CAR SPEED: " << CarObject.getSpeed() << " Miles/Hr";
} while (Do_Again ());
return 0;
}
//------------------------------------------------------------------------
// Function Definitions
//------------------------------------------------------------------------
bool Do_Again (void) {
(void) fflush (stdin);
cout << endl << endl << "\aDo Again? (y/n): ";
return static_cast
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.