473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Inheritance problem ...whats next?

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
2 1459

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
3
by: Gandu | last post by:
Could some C++ guru please help me? I have a very odd problem with respect templates and inheritance. I have templatized List class, from which I am inheriting to create a Stack class. All works...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
15
by: Sinex | last post by:
Hi, Why does C# disallow multiple inheritance? Whats the reason behind this? Is there any advantage or is it just a method to avoid some problems (if so, what problems?) that come with multiple...
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
4
by: Anarki | last post by:
##include <iostream> using namespace std; class A { }; class B:virtual public A { }; class C:virtual public A
4
by: Chris Fairles | last post by:
I recently implemented c++0x variants of tuple to take advantage of ECBO and then implemented unique_ptr using a tuple to hold the pointer - deleter pair so that sizeof(unique_ptr<int>) ==...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.