C++ program need car.h car.cpp and main.cpp ====================================
ID: 657287 • Letter: C
Question
C++ program need car.h car.cpp and main.cpp
================================================================================
lab8 is?
car.h
--------------------------------------
#pragma once
#include <string>
using std::string;
class Car
{
private: // private variables to store what each car object knows
string driverName;
double odometerMiles;
double speedMPH;
public: // public member functions (methods) to define what each Car object can do
// constructors
Car();
Car(string initDriverName);
// GETTERS and SETTERS
// getter and setter for drivers name
string getDriverName();
void setDriverName(string newName);
// getter and setter for odometer
double getOdometerMiles();
void setOdometerMiles(double newOdometerMiles);
// getter and setter for current speed
double getSpeedMPH();
void setSpeedMPH(double newSpeedMPH);
};
------------------------------------------------------------------------------------------------------
car.cpp
#include "Car.h"
// IMPLEMENTATION for Car class member functions (methods)
// Constructors
Car::Car()
{
driverName = "";
odometerMiles = 0.0;
speedMPH = 0.0;
}
Car::Car(string initDriverName)
{
driverName = initDriverName;
odometerMiles = 0.0;
speedMPH = 0.0;
}
// GETTRS and SETTERS
// getter and setter for drivers name
string Car::getDriverName()
{
return this->driverName;
}
void Car::setDriverName(string newName)
{
this->driverName = newName;
}
// getter and setter for odometer
double Car::getOdometerMiles()
{
return this->odometerMiles;
}
void Car::setOdometerMiles(double newOdometerMiles)
{
this->odometerMiles = newOdometerMiles;
}
// getter and setter for current speed
double Car::getSpeedMPH()
{
return this->speedMPH;
}
void Car::setSpeedMPH(double newSpeedMPH)
{
this->speedMPH = newSpeedMPH;
}
--------------------------------------------------------------------------------------------------------
lab08.cpp
//**************************************************************************************************
// FILE: lab08.cpp
//
// DESCRIPTION: This program simulates a race between two cars.
//
// This lab project illustrates the following C++ concepts:
// 1. Declaring and using user defined objects.
//
// AUTHORS: <Huiyi Tang> (<527009697@qq.com)
// <Gengyu Hong> (<Gengyu.Hong@asu.edu>)
//
// COURSE: CSE100 Principles of Programming with C++, Spring 2015
//
// LAB INFO: Lab Number 8; Date/Time: <March 25 10:30am>; TA: <Junghyo Lee>
//
// -------------------------------------------------------------------------------------------------
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
// include Car header file
// so we can declare and use Car objects
// NOTE: the use of quotation marks instead of angle brackets
// for user created header files
#include "Car.h"
int main()
{
// declare and initialize your variables
Car car1, car2;
string tempName1, tempName2;
int minutes = 0;
srand(time(0));
// 1. The user will be asked to provide the names for the drivers of the two cars.
cout << "Enter the name of driver 1: ";
cin >> tempName1;
car1.setDriverName(tempName1);
cout << "Enter the name of driver 2: ";
cin >> tempName2;
car2.setDriverName(tempName2);
// 2. Set each car?s odometer to 0.0 miles. Before the race begins.
car1.setOdometerMiles(0.0);
car2.setOdometerMiles(0.0);
// 3. Set each car?s speed to a random value between 50 and 100 miles per hour.
car1.setSpeedMPH(rand() % 51 + 50);
car1.setSpeedMPH(rand() % 51 + 50);
// 4. start the race
cout << " Race Starts: ";
// 5. the race will consist of simulated one minute intervals
while (true){
// a. each car travels for one minute at its current speed
minutes++;
// b. update each car's odometerMiles
car1.setOdometerMiles(car1.getOdometerMiles() + car1.getSpeedMPH() / 60.0);
car2.setOdometerMiles(car2.getOdometerMiles() + car2.getSpeedMPH() / 60.0);
// c. report each car's progress
cout << endl << "After " << minutes << " minutes:" << endl;
cout << car1.getDriverName() << " travelled " << car1.getOdometerMiles() << " miles" << endl;
cout << car1.getDriverName() << " travelled " << car2.getOdometerMiles() << " miles" << endl;
// d. check to see if either car has travelled more than 10 miles
// the first car to travel more than 10 miles is the winner and the race is over - announce the winner
if (car1.getOdometerMiles() > car2.getOdometerMiles() && car1.getOdometerMiles() >= 10){
cout << endl << car1.getDriverName() << " is the winner" << endl;
break;
}
if (car1.getOdometerMiles() < car2.getOdometerMiles() && car2.getOdometerMiles() >= 10){
cout << endl << car2.getDriverName() << " is the winner" << endl;
break;
}
if (car1.getOdometerMiles() == car2.getOdometerMiles() && car1.getOdometerMiles() >= 10){
cout << endl << "Its a draw!" << endl;
break;
}
// if no car has exceeded 10 miles then give each car a new random speed between 50 and 100 MPH
else{
car1.setSpeedMPH(rand() % 51 + 50);
car2.setSpeedMPH(rand() % 51 + 50);
}
// and continue the simulation
}
cin.sync(); cin.get();
return 0;
}
Explanation / Answer
The following codings might be helpful for starting the program.
#include <stdio.h>
#include <conio.h>
#include <process.h>
class car
.{
private:
int x1,y1,x2,y2,col,col2,col3;
public:
car():x1(200),y1(300),x2(270),y2(200),col(4)
{}
car(int a,int b,int c,int d,int e,int f,int
g):x1(a),y1(b),x2(c),y2(d),col(e),col2(f),col3(g)
{}
.void get(int a,int b,int c,int d,int e,int f,int g)
.{
x1=a;
y1=b;
x2=c;
y2=d;
col=e;
col2=f;
col3=g;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.