How to

Discussion in 'C++' started by Tian.Xiao.2007@gmail.com, Aug 21, 2007.

  1. Guest

    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
     
    , Aug 21, 2007
    #1
    1. Advertising

  2. On Aug 21, 8:26 pm, "" <Tian.Xiao.
    > 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
     
    Neelesh Bodas, Aug 21, 2007
    #2
    1. Advertising

  3. On 2007-08-21 17:26, 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
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 21, 2007
    #3
  4. Guest

    On Aug 21, 11:44 am, Neelesh Bodas <> wrote:
    > On Aug 21, 8:26 pm, "" <Tian.Xiao.
    >
    >
    >
    >
    >
    > > 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!
     
    , Aug 21, 2007
    #4
  5. Guest

    On Aug 21, 11:48 am, Erik Wikström <> wrote:
    > On 2007-08-21 17:26, 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?
     
    , Aug 21, 2007
    #5
  6. On Aug 21, 8:50 pm, "" <Tian.Xiao.
    > wrote:
    > On Aug 21, 11:44 am, Neelesh Bodas <> wrote:
    >
    >
    >
    > > On Aug 21, 8:26 pm, "" <Tian.Xiao.

    >
    > > > 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
     
    Neelesh Bodas, Aug 21, 2007
    #6
  7. red floyd Guest

    wrote:
    > On Aug 21, 11:48 am, Erik Wikström <> wrote:
    >> On 2007-08-21 17:26, 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);
    };
     
    red floyd, Aug 21, 2007
    #7
  8. Tim Guest

    On Aug 21, 1:08 pm, red floyd <> wrote:
    > wrote:
    > > On Aug 21, 11:48 am, Erik Wikström <> wrote:
    > >> On 2007-08-21 17:26, 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.
     
    Tim, Aug 21, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page