473,386 Members | 1,610 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,386 software developers and data experts.

A Car case

Tim
Dear All,

For the car case I posted before (car has some parts, and parts must
have the same maker, such as Ford), what I really concerned is that:

There are two implementations:

1.

class Car
{
public:
SetEngine(Engine*);
SetTransmission(Transmission*)
private:
Engine *m_engine;
Transmission *m_transmission;
...
};

For a Ford car

Car fordCar;
FordEngine * pFordEngine;
FordTranismission *pFordTransmission;
fordCar.SetEngine(pFordEngine);
fordCar.SetTransmission(pFordTransmission);
2.

class Car
{
public:
virtual SetEngine(Engine*);
virtual SetTransmission(Transmission*)
};

class FordCar : public Car
{
public:
virtual SetEngine(Engine*) {...}
virtual SetTransmission(Transmission*) {...}
private:
FordEngine *m_engine;
FordTransmission *m_transmission;
};

The difference is that I put the private part members is the derived
classes. I want to know which design is better and why?

I appreciate your kind help

Tim

Aug 21 '07 #1
3 1267
Hi, Tim!
I would rather do this in following way:
1. class Car should have two virtual public (as in your 2. solution)
2. class Car should have two handlers for Engine and Transmission -
but in PROTECTED section
3. class FordCar, derived from Car, should have overloaded functions
from its ancestor
Pros of this solution:
- you cannot create just a 'car' - it has to be FordCar, OpelCar,
FiatCar etc.
- protected section allows you to use both handlers in derived classes
- if you ever try to create a class hierarchy for Engine or/and
Transmission - you can still use your car-hierarchy: every Car has its
own engine, fully defined in derived classes.
Waiting for comments from other members :-)

hbrt

Aug 21 '07 #2
Tim
On Aug 21, 1:24 pm, hbrt <Hubert.Swietli...@gmail.comwrote:
Hi, Tim!
I would rather do this in following way:
1. class Car should have two virtual public (as in your 2. solution)
2. class Car should have two handlers for Engine and Transmission -
but in PROTECTED section
3. class FordCar, derived from Car, should have overloaded functions
from its ancestor
Pros of this solution:
- you cannot create just a 'car' - it has to be FordCar, OpelCar,
FiatCar etc.
- protected section allows you to use both handlers in derived classes
- if you ever try to create a class hierarchy for Engine or/and
Transmission - you can still use your car-hierarchy: every Car has its
own engine, fully defined in derived classes.
Waiting for comments from other members :-)

hbrt
I have some questions on your implementation:
1. FordCar has to implement the virtual set.. functioins? Is it
repeating code?
2. In set engine to Ford, you need to check the engine type?
3. I am very interested you talking about the engine/transmission
hierarchy, considering these, it there any better design for the whole
system?
Thanks.

Aug 21 '07 #3
Tim wrote:
Dear All,

For the car case I posted before (car has some parts, and parts must
have the same maker, such as Ford), what I really concerned is that:
Second poster with the same question. School must be back in
session. Kiddies do your own homework.
Aug 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: cs168 | last post by:
Hi I am new in ASP programming so I do use the very basic and simple way to do all my stuff. Now I do really got stuck at how can I loop thru the calculation for all my selection.. My full code is as...
10
by: MLH | last post by:
Suppose the following... Dim A as Date A=#7/24/2005# I wish to compare value of A against 2 other values: 1) 8/1/2005 2) 9/1/2005 Which is better and why... First:
7
by: Lauren Quantrell | last post by:
Is there any speed/resource advantage/disadvantage in using Select Case x Case 1 Case 2 etc. many more cases... End Select VS.
22
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point...
24
by: clockworx05 | last post by:
Hey guys i have this program that i need to write for class. here are the instructions: Write a function called foo that asks the user for their age. Pass the age value to a function called...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.