Creating Classes in C++
Not what you're looking for?
I am very confused with classes in C++. I need help with my problem below:
Create a class called car.
Have several variables with the necessary variables that
that store, gas level, amount of gas, gear car is in, car in motion.. etc
Have functions that do:
Check the battery level
Check the gas level
Turn right, Left,Go Straight
Put forward gear
Put backward gear
Start Car (function should check if battery is good and you have gas)
Example: If good then print message "car started", if not "print car not started because ...."
Park Car
M
ove car # number of feet. ( Car uses 1 unit of gas per 100 feet)
( sub tract 1 unit of gas per 100 feet traveled)
As the car moves (determine if it needs gas)
Purchase this Solution
Solution Summary
This solution helps with a problem regarding classes in C++.
Solution Preview
Here is a sample for you. The Car class contains the basics as you requested and the main() function contains a menu of actions that user may choose. You can extend the car class with more functionality using the example. Good luck.
#include <iostream>
using namespace std;
class Car
{
//private variables that store the car state
private:
int batteryLevel;
float gasAmount;
int gearNumber;
int carMotion;
int carDirection;
int maxGas;
bool carStarted;
//public methods to access car state
public:
Car(int maxGas)
{
// the max amount of gallons this car can have
this->maxGas = maxGas;
this->batteryLevel = 0;
this->carStarted = false;
}
int GetBatteryLevel()
{
return this->batteryLevel;
}
void ChargeBatteryLevel(int level)
{
if (level<0 || level>100)
{
cout << "Invalid option" << endl;
return;
}
this->batteryLevel = level;
}
float GetGasLevel()
{
return ((float)this->gasAmount/(float)maxGas) * 100;
}
void Gear(int gearNumber)
{
//motion: 0 - stop, 1 - forward, -1 ...
Purchase this Solution
Free BrainMass Quizzes
Word 2010: Table of Contents
Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.
Excel Introductory Quiz
This quiz tests your knowledge of basics of MS-Excel.
Basic Computer Terms
We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.
Javscript Basics
Quiz on basics of javascript programming language.
Word 2010: Tables
Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.