How to do that

Discussion in 'C++' started by Vince, Oct 12, 2004.

  1. Vince

    Vince Guest

    Hello,

    I have two classes FooA and FooB with exactly the same methods.
    In my application I read a INI File and I am supposed to instantiate either
    FooA or FooB.(Actually FooA and FooB
    represents classes to control two differents smart card reader but with same
    methods).

    class FooA
    {
    int Init();
    int Close();
    }

    class FooB
    {
    int Init();
    int Close();
    }




    Myapp
    -------
    void *pPointer = NULL;
    CIni ini; // Class to read a ini file

    ini.ReadIni() // read ini file
    IF ini.GetType == FooA
    pPointer = new FooA
    else
    pPointer = new FooB

    pPointer->Init()
    pPointer->Close()

    I try to do it by declaring pPointer as a void pointer but it didn't
    compile.
    How can I do that ??
    Vince, Oct 12, 2004
    #1
    1. Advertising

  2. Vince

    Phlip Guest

    Vince wrote:

    > Hello,
    >
    > I have two classes FooA and FooB with exactly the same methods.


    Find a reason to give them a common base class.

    > In my application I read a INI File and I am supposed to instantiate

    either
    > FooA or FooB.(Actually FooA and FooB
    > represents classes to control two differents smart card reader but with

    same
    > methods).


    Factory Pattern. Get the book /Design Patterns/ and read the entire thing
    before you code or post again.

    > void *pPointer = NULL;
    > CIni ini; // Class to read a ini file
    >
    > ini.ReadIni() // read ini file
    > IF ini.GetType == FooA
    > pPointer = new FooA
    > else
    > pPointer = new FooB
    >
    > pPointer->Init()
    > pPointer->Close()
    >
    > I try to do it by declaring pPointer as a void pointer but it didn't
    > compile.
    > How can I do that ??


    You have rediscovered the need for abstract base classes. Look them up in
    your tutorials.

    FooA and FooB must inherit a common class; then pPointer can type to that
    class.

    You have also reinvented Factory Pattern, so you are ready to go read.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Oct 12, 2004
    #2
    1. Advertising

  3. Vince

    JKop Guest


    > class FooA
    > {
    > int Init();
    > int Close();
    > }
    >
    > class FooB
    > {
    > int Init();
    > int Close();
    > }


    That won't compile. You must put in a ; after a class definition. You must
    specify "public" (The default is "private"):

    class FooA
    {
    public:

    int Init();
    int Close();
    };

    class FooB
    {
    public:

    int Init();
    int Close();
    };


    > Myapp
    > -------
    > void *pPointer = NULL;
    > CIni ini; // Class to read a ini file


    I presume that should be something like:

    CIni ini("blah.ini");


    > ini.ReadIni() // read ini file
    > IF ini.GetType == FooA


    Good God where do I start?

    Google for "typeid".

    Also that syntax should be:

    if (ini.GetType() ==

    (assuming "GetType" is a function)


    > pPointer = new FooA
    > else
    > pPointer = new FooB
    >
    > pPointer->Init()
    > pPointer->Close()


    pPointer->Init();
    pPointer->Close();

    Stick a ; after everything.



    You've a lot of learning to do!


    -JKop
    JKop, Oct 12, 2004
    #3
  4. Vince

    Vince Guest

    Heu I wanted to do it simpler but I know all that. My problem is not a
    syntax one because
    I am already programing in C/C++ for 3 years.
    Usually I only take from C++ what I miss from C : template, inheritage,
    encapsulation.
    But until this project I didn't need to use abstract class.
    Thanks anyway





    "JKop" <> a écrit dans le message de news:
    rxUad.33266$...
    >
    >> class FooA
    >> {
    >> int Init();
    >> int Close();
    >> }
    >>
    >> class FooB
    >> {
    >> int Init();
    >> int Close();
    >> }

    >
    > That won't compile. You must put in a ; after a class definition. You must
    > specify "public" (The default is "private"):
    >
    > class FooA
    > {
    > public:
    >
    > int Init();
    > int Close();
    > };
    >
    > class FooB
    > {
    > public:
    >
    > int Init();
    > int Close();
    > };
    >
    >
    >> Myapp
    >> -------
    >> void *pPointer = NULL;
    >> CIni ini; // Class to read a ini file

    >
    > I presume that should be something like:
    >
    > CIni ini("blah.ini");
    >
    >
    >> ini.ReadIni() // read ini file
    >> IF ini.GetType == FooA

    >
    > Good God where do I start?
    >
    > Google for "typeid".
    >
    > Also that syntax should be:
    >
    > if (ini.GetType() ==
    >
    > (assuming "GetType" is a function)
    >
    >
    >> pPointer = new FooA
    >> else
    >> pPointer = new FooB
    >>
    >> pPointer->Init()
    >> pPointer->Close()

    >
    > pPointer->Init();
    > pPointer->Close();
    >
    > Stick a ; after everything.
    >
    >
    >
    > You've a lot of learning to do!
    >
    >
    > -JKop
    Vince, Oct 12, 2004
    #4
  5. Vince

    Howard Guest

    "Vince" <> wrote in message
    news:416c1724$0$28940$...
    > Heu I wanted to do it simpler but I know all that. My problem is not a
    > syntax one because
    > I am already programing in C/C++ for 3 years.
    > Usually I only take from C++ what I miss from C : template, inheritage,
    > encapsulation.
    > But until this project I didn't need to use abstract class.
    > Thanks anyway
    >
    >


    Well, if your code looks like what you posted, then you DO have a syntax
    problem. You should try to copy & paste your real code, not type it into
    your email, because everyone here will focus on what you put in your email,
    not what they *guess* your real code looks like.

    (It looks like you need to do some reading before you go on, specifically
    regarding inheritance, and class design.)

    Basically, what you need is something like this:

    class Foo
    {
    public:
    virtual int Init() = 0;
    virtual int Close() = 0;
    virtual ~Foo(); // <--YOU NEED TO HAVE THIS!
    };

    class FooA : public Foo
    {
    public:
    virtual int Init();
    virtual int Close();
    virtual ~Foo();
    };

    class FooB : public Foo
    {
    public:
    virtual int Init();
    virtual int Close();
    virtual ~Foo();
    };


    ....and later, in implementation code:

    Foo* pFoo; // (you may want to add " = NULL;" here)
    if (*whatever tells you to use FooA*)
    pFoo = new FooA;
    else
    pFoo = new FooB;


    Note the inclusion of a virtual destructor in the base class. You need that
    if you're going to dynamically allocate instances of FooA or FooB using a
    Foo* pointer, in order for the correct destructor to be called when you
    later say "delete pFoo;". (Also, search groups.google.com for something
    called the "Rule of Three". Basically, it tells you that you *may* also
    need an assignment operator and a copy constructor for those classes.)

    By the way, you might also look into something called "Resource Allocation
    Is Initialization" (RAII). Whenever I see an Init() function, I immediately
    wonder why. Initialization should, in general, be done in the constructor,
    unless there are good reasons not to. You might want to have a member
    object contained inside the Foo object that does in its constructor and
    destructor what yyou're now doing in Init and Close. But that's something
    you'll need to decide, after you read up on the subject.


    -Howard
    Howard, Oct 12, 2004
    #5
  6. "Howard" <> wrote in message
    news:_fWad.691695$...
    >
    > "Vince" <> wrote in message
    > news:416c1724$0$28940$...
    >> Heu I wanted to do it simpler but I know all that. My problem is not a
    >> syntax one because
    >> I am already programing in C/C++ for 3 years.
    >> Usually I only take from C++ what I miss from C : template, inheritage,
    >> encapsulation.
    >> But until this project I didn't need to use abstract class.
    >> Thanks anyway
    >>
    >>

    >
    > Well, if your code looks like what you posted, then you DO have a syntax
    > problem. You should try to copy & paste your real code, not type it into
    > your email, because everyone here will focus on what you put in your
    > email,
    > not what they *guess* your real code looks like.
    >
    > (It looks like you need to do some reading before you go on, specifically
    > regarding inheritance, and class design.)
    >
    > Basically, what you need is something like this:
    >
    > class Foo
    > {
    > public:
    > virtual int Init() = 0;
    > virtual int Close() = 0;
    > virtual ~Foo(); // <--YOU NEED TO HAVE THIS!
    > };
    >
    > class FooA : public Foo
    > {
    > public:
    > virtual int Init();
    > virtual int Close();
    > virtual ~Foo();


    Should be virtual ~FooA(); // But you know that =)

    > };
    >
    > class FooB : public Foo
    > {
    > public:
    > virtual int Init();
    > virtual int Close();
    > virtual ~Foo();


    Should be virtual ~FooB(); // But you know that =)


    > };
    >
    >
    > ...and later, in implementation code:
    >
    > Foo* pFoo; // (you may want to add " = NULL;" here)
    > if (*whatever tells you to use FooA*)
    > pFoo = new FooA;
    > else
    > pFoo = new FooB;
    >
    >
    > Note the inclusion of a virtual destructor in the base class. You need
    > that
    > if you're going to dynamically allocate instances of FooA or FooB using a
    > Foo* pointer, in order for the correct destructor to be called when you
    > later say "delete pFoo;". (Also, search groups.google.com for something
    > called the "Rule of Three". Basically, it tells you that you *may* also
    > need an assignment operator and a copy constructor for those classes.)
    >
    > By the way, you might also look into something called "Resource Allocation
    > Is Initialization" (RAII). Whenever I see an Init() function, I
    > immediately
    > wonder why. Initialization should, in general, be done in the
    > constructor,
    > unless there are good reasons not to. You might want to have a member
    > object contained inside the Foo object that does in its constructor and
    > destructor what yyou're now doing in Init and Close. But that's something
    > you'll need to decide, after you read up on the subject.
    >
    >
    > -Howard
    >
    >
    >


    / WP
    William Payne, Oct 12, 2004
    #6
  7. Vince

    Howard Guest

    "William Payne" <> wrote in message
    news:ckhip7$k5n$...
    >
    > > class FooA : public Foo
    > > {
    > > public:
    > > virtual int Init();
    > > virtual int Close();
    > > virtual ~Foo();

    >
    > Should be virtual ~FooA(); // But you know that =)
    >


    D'oh! :)
    Howard, Oct 12, 2004
    #7
  8. Vince

    Vince Guest

    Ok thanks for all this information. I am learning a lot with all of you.
    You say that init function should be done in constructor but a constructor
    doesn't have a return value so how do you know that your initialization went
    well .
    Usually in my init function I instanciate another Class and if soemthing
    goes wrong I return an error code.
    How can I do this with a constructor that doesn't return a value.

    I know that I should read a good C++ book instead of asking stupid questions
    but I prefer to ask and to see
    answers because I must admit I am a bit lazy (Sorry).
    But I want to change and I will try to read Bjarn Stroustrup book.




    "William Payne" <> a écrit dans le message de
    news: ckhip7$k5n$...
    >
    > "Howard" <> wrote in message
    > news:_fWad.691695$...
    >>
    >> "Vince" <> wrote in message
    >> news:416c1724$0$28940$...
    >>> Heu I wanted to do it simpler but I know all that. My problem is not a
    >>> syntax one because
    >>> I am already programing in C/C++ for 3 years.
    >>> Usually I only take from C++ what I miss from C : template, inheritage,
    >>> encapsulation.
    >>> But until this project I didn't need to use abstract class.
    >>> Thanks anyway
    >>>
    >>>

    >>
    >> Well, if your code looks like what you posted, then you DO have a syntax
    >> problem. You should try to copy & paste your real code, not type it into
    >> your email, because everyone here will focus on what you put in your
    >> email,
    >> not what they *guess* your real code looks like.
    >>
    >> (It looks like you need to do some reading before you go on, specifically
    >> regarding inheritance, and class design.)
    >>
    >> Basically, what you need is something like this:
    >>
    >> class Foo
    >> {
    >> public:
    >> virtual int Init() = 0;
    >> virtual int Close() = 0;
    >> virtual ~Foo(); // <--YOU NEED TO HAVE THIS!
    >> };
    >>
    >> class FooA : public Foo
    >> {
    >> public:
    >> virtual int Init();
    >> virtual int Close();
    >> virtual ~Foo();

    >
    > Should be virtual ~FooA(); // But you know that =)
    >
    >> };
    >>
    >> class FooB : public Foo
    >> {
    >> public:
    >> virtual int Init();
    >> virtual int Close();
    >> virtual ~Foo();

    >
    > Should be virtual ~FooB(); // But you know that =)
    >
    >
    >> };
    >>
    >>
    >> ...and later, in implementation code:
    >>
    >> Foo* pFoo; // (you may want to add " = NULL;" here)
    >> if (*whatever tells you to use FooA*)
    >> pFoo = new FooA;
    >> else
    >> pFoo = new FooB;
    >>
    >>
    >> Note the inclusion of a virtual destructor in the base class. You need
    >> that
    >> if you're going to dynamically allocate instances of FooA or FooB using a
    >> Foo* pointer, in order for the correct destructor to be called when you
    >> later say "delete pFoo;". (Also, search groups.google.com for something
    >> called the "Rule of Three". Basically, it tells you that you *may* also
    >> need an assignment operator and a copy constructor for those classes.)
    >>
    >> By the way, you might also look into something called "Resource
    >> Allocation
    >> Is Initialization" (RAII). Whenever I see an Init() function, I
    >> immediately
    >> wonder why. Initialization should, in general, be done in the
    >> constructor,
    >> unless there are good reasons not to. You might want to have a member
    >> object contained inside the Foo object that does in its constructor and
    >> destructor what yyou're now doing in Init and Close. But that's
    >> something
    >> you'll need to decide, after you read up on the subject.
    >>
    >>
    >> -Howard
    >>
    >>
    >>

    >
    > / WP
    >
    Vince, Oct 13, 2004
    #8
  9. Vince

    JKop Guest


    > I know that I should read a good C++ book instead of asking stupid
    > questions but I prefer to ask and to see
    > answers because I must admit I am a bit lazy (Sorry).
    > But I want to change and I will try to read Bjarn Stroustrup book.



    I wouldn't suggest Bjarne's book for the level you're at! I myself found it
    difficult to read, it was very monotanous (where's spell-check when you need
    it!). He introduced classes called "Stack" and I didn't even know what a
    stack was (nor do I still) so I read about 1 in every 10 lines on those
    pages.


    -JKop
    JKop, Oct 13, 2004
    #9
  10. Vince wrote:
    >
    > Ok thanks for all this information. I am learning a lot with all of you.
    > You say that init function should be done in constructor but a constructor
    > doesn't have a return value so how do you know that your initialization went
    > well .
    > Usually in my init function I instanciate another Class and if soemthing
    > goes wrong I return an error code.
    > How can I do this with a constructor that doesn't return a value.


    Eg. By introducing a member function which tells the caller if the
    object is in a good state.

    Or you could eg. throw an exception


    >
    > I know that I should read a good C++ book instead of asking stupid questions
    > but I prefer to ask and to see
    > answers because I must admit I am a bit lazy (Sorry).


    You won't get very far with that.
    C++ as language and the way it is used is too complex for that.
    C++ not only lives with the language but also with what we call
    'idioms'. That is: typical constructs used for special needs.
    RAII is such an idiom as well as the 'factory pattern' or the
    'virtual constructor' or ....

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 13, 2004
    #10
  11. Vince

    Ron Natalie Guest

    JKop wrote:
    >>I know that I should read a good C++ book instead of asking stupid
    >>questions but I prefer to ask and to see
    >>answers because I must admit I am a bit lazy (Sorry).
    >>But I want to change and I will try to read Bjarn Stroustrup book.

    >
    >
    >
    > I wouldn't suggest Bjarne's book for the level you're at! I myself found it
    > difficult to read, it was very monotanous (where's spell-check when you need
    > it!). He introduced classes called "Stack" and I didn't even know what a
    > stack was (nor do I still) so I read about 1 in every 10 lines on those
    > pages.


    1. Spell check is available in a variety of news posters. May
    I suggest you switch from XNews to Thunderbird (which with some
    of the extensions like mhenhy, is very nice..).

    2. Bjarne's book assumes you can program, just not in C++. If
    you don't know what a stack is, you might try a basic CS book
    like Horowitz and Sahni or such. You can't even begin to understand
    concepts like containers without it really.
    Ron Natalie, Oct 13, 2004
    #11
  12. Vince

    JKop Guest


    > 1. Spell check is available in a variety of news posters. May
    > I suggest you switch from XNews to Thunderbird (which with some
    > of the extensions like mhenhy, is very nice..).


    Thanks, will look into it.

    > 2. Bjarne's book assumes you can program, just not in C++. If
    > you don't know what a stack is, you might try a basic CS book
    > like Horowitz and Sahni or such. You can't even begin to understand
    > concepts like containers without it really.


    hmm... effort... uhh


    -JKop
    JKop, Oct 13, 2004
    #12
    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