How to

T

Tian.Xiao.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
 
N

Neelesh Bodas

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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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;
}
 
T

Tian.Xiao.2007

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!
 
T

Tian.Xiao.2007

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;

}

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

Neelesh Bodas

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
 
R

red floyd

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);
};
 
T

Tim

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.
 

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
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top