By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 982 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

Inheritance problem ...whats next?

P: n/a
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 */
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"David" <d_***@shaw.ca> wrote in message
news:gDdtc.599169$oR5.507854@pd7tw3no...
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
Jul 22 '05 #2

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2h************@uni-berlin.de...

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.)
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.