Inheritance problem ...whats next?

D

David

I am stumped ..how do I use the flag (secton c)


INHERITANCE

(a) Create a class Car that conatins two integer fields. One holds a flag
indicating whether the
ignition is on; the other holds the Car's current speed in miles per
hour. There are four member
functions: two functions that turns the igition on and off, a setSpeedO
function that sets the
Car's speed, and a showCarO function that displays a Car's states.

(b) The function that displays the values of a Car's fields uses the value
of the isIgnitionOn
flag to determine whether the Car is running. It also displays the
current speed of the Car.

(c) The two functions named turnIgnitionOnO and turnIgnitionOffO are
similar; each sets the value
of the isIgnitionOn flag using 1 to represent true and 0 to represent
false.

(d) The setSpeedO function takes an integer argument and uses it to set the
value of the speed
field. However, no Car is allowed to speed greater than 65 miles per
hour, and no Car is
allowed any speed other than 0 if the ignition is off.

(e) Add a mainO function that demonstrate the functions at work. The maninO
function declares
a Car object, turns the Car on and sets a speed. Show the car values as
well.

(f) Define the Convertible class as an extension of the Car class. The
Convertible class contains
one new data member named isTopUp that holds the status of the
Convertible's top.
Three functions work with this data field: one puts the top up ;
another puts it down and the
third prints a message regarding the top's status.

(g) The functions putTopUPO and putTopDownO set the isTopUp field to 1 and
0, respectively.

(h) The ShowTopStatusO function prints an appropriate message based on the
status of the
isTopUp field.

(i) Lastly, re-write the mainO program that declares a Convertible object
and uses the
functions to start the Convertible, set the spped, put the top down and
up and so on.*/


#include <iostream>
using namespace std;

class car
{
public:
void setSpeet(int mph)
void turnOn();
void turnOff();
void showCar

private:
int isIgnitionOn;
int speed;

/* Ignition On --> set isIgnitionOn =1
Ignition Off --> set isIgnitionOn =0
Ignition Off --> set speed =0 */
 
J

John Harrison

David said:
I am stumped ..how do I use the flag (secton c)


INHERITANCE

(a) Create a class Car that conatins two integer fields. One holds a flag
indicating whether the
ignition is on; the other holds the Car's current speed in miles per
hour. There are four member
functions: two functions that turns the igition on and off, a setSpeedO
function that sets the
Car's speed, and a showCarO function that displays a Car's states.

(b) The function that displays the values of a Car's fields uses the value
of the isIgnitionOn
flag to determine whether the Car is running. It also displays the
current speed of the Car.

(c) The two functions named turnIgnitionOnO and turnIgnitionOffO are
similar; each sets the value
of the isIgnitionOn flag using 1 to represent true and 0 to represent
false.


(d) The setSpeedO function takes an integer argument and uses it to set the
value of the speed
field. However, no Car is allowed to speed greater than 65 miles per
hour, and no Car is
allowed any speed other than 0 if the ignition is off.

Simple enough

void Car::turnIgnitionOn()
{
isIgnitionOn = 1;
}

void Car::turnIgnitionOff()
{
isIgnitionOn = 0;
speed = 0; // possibly implied by part d
}

void car::setSpeed(int mph)
{
if (mph> 65 || isIgnitionOn == 0 && mph != 0)
{
cerr << "invalid speed\n";
return;
}
speed = mph;
}

Not sure if turning the ignition off should set the speed to zero, it seems
to be implied by part d. I'm not a car driver so have no idea if that is
realistic or not. Just omit it if it isn't.

The exercise is rubbish I think.

john
 
J

jeffc

John Harrison said:
Not sure if turning the ignition off should set the speed to zero, it seems
to be implied by part d. I'm not a car driver so have no idea if that is
realistic or not. Just omit it if it isn't.

The exercise is rubbish I think.

Actually, the exercise is a perfectly fine way to introduce many of the
concepts involved. Regarding your previous comment, there are lots of real
world scenarios that you could include to make the design more complicated.
If you are driving down the road, put your car in neutral and then turn off
the ignition, the car will continue at the same speed (but assuming flat
travel, will slowly decrease down to 0 over time.) If there is an
acceleration function (possibly with a negative number for breaking), then
positive acceleration would have to be disabled if the ignition were off (a
better model might be if the engine is running or not.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,463
Latest member
FinleyMoye

Latest Threads

Top