Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to

Reply
Thread Tools

How to

 
 
Tian.Xiao.2007@gmail.com
Guest
Posts: n/a
 
      08-21-2007
Dear All,

Assume I have a Car class, which include some parts, i.e,

class Engine;
class Transmission;
class Car
{
private:
Engine *m_engine;
Transmission *m_transmission;
...
}

Assume engine and transmission in a car must have the same maker, for
example: Ford car can have Ford engine and Ford transmission, how to
implement this constraint in the class design?

I appreciate your kind help!

Tim

 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      08-21-2007
On Aug 21, 8:26 pm, "(E-Mail Removed)" <Tian.Xiao.
(E-Mail Removed)> wrote:
> Dear All,
>
> Assume I have a Car class, which include some parts, i.e,
>
> class Engine;
> class Transmission;
> class Car
> {
> private:
> Engine *m_engine;
> Transmission *m_transmission;
> ...
>
> }
>
> Assume engine and transmission in a car must have the same maker, for
> example: Ford car can have Ford engine and Ford transmission, how to
> implement this constraint in the class design?
>


One of the solutions is to use templates

template <class T> class Engine { /* your code here */ };
template <class T> class Transmission { /* Your code here */ };

template <class T> class Car
{
private:
Engine<T> *m_engine;
Transmission<T> *m_transmission;
/* other stuff here */
}

class Ford { };
Car<Ford> cFord;

HTH
-N

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      08-21-2007
On 2007-08-21 17:26, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Dear All,
>
> Assume I have a Car class, which include some parts, i.e,
>
> class Engine;
> class Transmission;
> class Car
> {
> private:
> Engine *m_engine;
> Transmission *m_transmission;
> ...
> }
>
> Assume engine and transmission in a car must have the same maker, for
> example: Ford car can have Ford engine and Ford transmission, how to
> implement this constraint in the class design?


By checking when you set the engine of the Car:

void setEngine(Engine* e)
{
if (e->type != this->type)
{
// Do something, like throw an exception
}
m_engine = e;
}

--
Erik Wikström
 
Reply With Quote
 
Tian.Xiao.2007@gmail.com
Guest
Posts: n/a
 
      08-21-2007
On Aug 21, 11:44 am, Neelesh Bodas <(E-Mail Removed)> wrote:
> On Aug 21, 8:26 pm, "(E-Mail Removed)" <Tian.Xiao.
>
>
>
>
>
> (E-Mail Removed)> wrote:
> > Dear All,

>
> > Assume I have a Car class, which include some parts, i.e,

>
> > class Engine;
> > class Transmission;
> > class Car
> > {
> > private:
> > Engine *m_engine;
> > Transmission *m_transmission;
> > ...

>
> > }

>
> > Assume engine and transmission in a car must have the same maker, for
> > example: Ford car can have Ford engine and Ford transmission, how to
> > implement this constraint in the class design?

>
> One of the solutions is to use templates
>
> template <class T> class Engine { /* your code here */ };
> template <class T> class Transmission { /* Your code here */ };
>
> template <class T> class Car
> {
> private:
> Engine<T> *m_engine;
> Transmission<T> *m_transmission;
> /* other stuff here */
>
> }
>
> class Ford { };
> Car<Ford> cFord;
>
> HTH
> -N- Hide quoted text -
>
> - Show quoted text -


If using template, FordCar is not inherited from Car, and so all Car's
methods need to be reimplemented. Am I right?

Thanks!

 
Reply With Quote
 
Tian.Xiao.2007@gmail.com
Guest
Posts: n/a
 
      08-21-2007
On Aug 21, 11:48 am, Erik Wikström <(E-Mail Removed)> wrote:
> On 2007-08-21 17:26, (E-Mail Removed) wrote:
>
>
>
>
>
> > Dear All,

>
> > Assume I have a Car class, which include some parts, i.e,

>
> > class Engine;
> > class Transmission;
> > class Car
> > {
> > private:
> > Engine *m_engine;
> > Transmission *m_transmission;
> > ...
> > }

>
> > Assume engine and transmission in a car must have the same maker, for
> > example: Ford car can have Ford engine and Ford transmission, how to
> > implement this constraint in the class design?

>
> By checking when you set the engine of the Car:
>
> void setEngine(Engine* e)
> {
> if (e->type != this->type)
> {
> // Do something, like throw an exception
> }
> m_engine = e;
>
> }
>
> --
> Erik Wikström- Hide quoted text -
>
> - Show quoted text -


Thanks, but if some maker's part, such as tire, is good for several
other makers. How to deal with this complex problem?

 
Reply With Quote
 
Neelesh Bodas
Guest
Posts: n/a
 
      08-21-2007
On Aug 21, 8:50 pm, "(E-Mail Removed)" <Tian.Xiao.
(E-Mail Removed)> wrote:
> On Aug 21, 11:44 am, Neelesh Bodas <(E-Mail Removed)> wrote:
>
>
>
> > On Aug 21, 8:26 pm, "(E-Mail Removed)" <Tian.Xiao.

>
> > (E-Mail Removed)> wrote:
> > > Dear All,

>
> > > Assume I have a Car class, which include some parts, i.e,

>
> > > class Engine;
> > > class Transmission;
> > > class Car
> > > {
> > > private:
> > > Engine *m_engine;
> > > Transmission *m_transmission;
> > > ...

>
> > > }

>
> > > Assume engine and transmission in a car must have the same maker, for
> > > example: Ford car can have Ford engine and Ford transmission, how to
> > > implement this constraint in the class design?

>
> > One of the solutions is to use templates

>
> > template <class T> class Engine { /* your code here */ };
> > template <class T> class Transmission { /* Your code here */ };

>
> > template <class T> class Car
> > {
> > private:
> > Engine<T> *m_engine;
> > Transmission<T> *m_transmission;
> > /* other stuff here */

>
> > }

>
> > class Ford { };
> > Car<Ford> cFord;

>
> > HTH
> > -N- Hide quoted text -

>
> > - Show quoted text -

>
> If using template, FordCar is not inherited from Car, and so all Car's
> methods need to be reimplemented. Am I right?
>


All Car's method that are independent of the model can be implemented
only once in Car class. If there is any functionality specific to the
maker Ford, it goes in the Ford class. You can also have a base class
maker from which you can derive Ford (and other) classes.

-N

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      08-21-2007
(E-Mail Removed) wrote:
> On Aug 21, 11:48 am, Erik Wikström <(E-Mail Removed)> wrote:
>> On 2007-08-21 17:26, (E-Mail Removed) wrote:
>>
>>
>>
>>
>>
>>> Dear All,
>>> Assume I have a Car class, which include some parts, i.e,
>>> class Engine;
>>> class Transmission;
>>> class Car
>>> {
>>> private:
>>> Engine *m_engine;
>>> Transmission *m_transmission;
>>> ...
>>> }
>>> Assume engine and transmission in a car must have the same maker, for
>>> example: Ford car can have Ford engine and Ford transmission, how to
>>> implement this constraint in the class design?

>> By checking when you set the engine of the Car:
>>
>> void setEngine(Engine* e)
>> {
>> if (e->type != this->type)
>> {
>> // Do something, like throw an exception
>> }
>> m_engine = e;
>>
>> }
>>
>> --
>> Erik Wikström- Hide quoted text -
>>
>> - Show quoted text -

>
> Thanks, but if some maker's part, such as tire, is good for several
> other makers. How to deal with this complex problem?
>


I assume that your parts are derived from "Part"

e.g:

class Engine : public Part { ... };
etc...

Then part has a method:

class Part {
public:
virtual bool is_OK_to_use_on(const Car* car);
};
 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      08-21-2007
On Aug 21, 1:08 pm, red floyd <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Aug 21, 11:48 am, Erik Wikström <(E-Mail Removed)> wrote:
> >> On 2007-08-21 17:26, (E-Mail Removed) wrote:

>
> >>> Dear All,
> >>> Assume I have a Car class, which include some parts, i.e,
> >>> class Engine;
> >>> class Transmission;
> >>> class Car
> >>> {
> >>> private:
> >>> Engine *m_engine;
> >>> Transmission *m_transmission;
> >>> ...
> >>> }
> >>> Assume engine and transmission in a car must have the same maker, for
> >>> example: Ford car can have Ford engine and Ford transmission, how to
> >>> implement this constraint in the class design?
> >> By checking when you set the engine of the Car:

>
> >> void setEngine(Engine* e)
> >> {
> >> if (e->type != this->type)
> >> {
> >> // Do something, like throw an exception
> >> }
> >> m_engine = e;

>
> >> }

>
> >> --
> >> Erik Wikström- Hide quoted text -

>
> >> - Show quoted text -

>
> > Thanks, but if some maker's part, such as tire, is good for several
> > other makers. How to deal with this complex problem?

>
> I assume that your parts are derived from "Part"
>
> e.g:
>
> class Engine : public Part { ... };
> etc...
>
> Then part has a method:
>
> class Part {
> public:
> virtual bool is_OK_to_use_on(const Car* car);
>
>
>
> };- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -


Very Good! Better than comparing the types directly because it hides
the implementation details (such as comparing types) and more firendly
to clients. Thanks.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM



Advertisments