Abstract base class + virtual Method

Discussion in 'C++' started by Luis G Hernandez, Mar 5, 2004.

  1. I am required to create a pure virtual method for a project that
    returns a string. The method name is: getCoverType()
    I know that for a data type int I could do this:

    protected:
    // method to decide how to sort the list.
    virtual bool toCompare_ (const Key& key1, const Key& key2) const =
    0;

    But, how would I declare this for a method that returns a string?
    //method to return a string for the book cover type
    virtual string getCoverType() /*what else ??*/ ;

    Thanks
     
    Luis G Hernandez, Mar 5, 2004
    #1
    1. Advertising

  2. "Luis G Hernandez" <> wrote in message
    news:...
    > I am required to create a pure virtual method for a project that
    > returns a string. The method name is: getCoverType()
    > I know that for a data type int I could do this:
    >
    > protected:
    > // method to decide how to sort the list.
    > virtual bool toCompare_ (const Key& key1, const Key& key2) const =
    > 0;
    >
    > But, how would I declare this for a method that returns a string?
    > //method to return a string for the book cover type
    > virtual string getCoverType() /*what else ??*/ ;
    >
    > Thanks


    Anything wrong with this?

    virtual string getCoverType() const = 0;

    Provided you have remembered to put this at the top of your code it should
    be fine.

    #include <string>
    using namespace std;

    Basically whatever you are doing with int, just replace int with string. How
    could it be any other way?

    john
     
    John Harrison, Mar 5, 2004
    #2
    1. Advertising

  3. "Luis G Hernandez" <> wrote in message
    news:...
    | I am required to create a pure virtual method for a project that
    | returns a string. The method name is: getCoverType()
    | I know that for a data type int I could do this:
    |
    | protected:
    | // method to decide how to sort the list.
    | virtual bool toCompare_ (const Key& key1, const Key& key2) const =
    | 0;
    |
    | But, how would I declare this for a method that returns a string?
    | //method to return a string for the book cover type
    | virtual string getCoverType() /*what else ??*/ ;

    Exactly the same way.

    Cheers.
    Chris Val
     
    Chris \( Val \), Mar 5, 2004
    #3
  4. Luis G Hernandez

    Rolf Magnus Guest

    Luis G Hernandez wrote:

    > I am required to create a pure virtual method for a project that
    > returns a string. The method name is: getCoverType()
    > I know that for a data type int I could do this:
    >
    > protected:
    > // method to decide how to sort the list.
    > virtual bool toCompare_ (const Key& key1, const Key& key2) const =
    > 0;


    Where is the int?

    > But, how would I declare this for a method that returns a string?


    Just the same.

    > //method to return a string for the book cover type
    > virtual string getCoverType() /*what else ??*/ ;


    virtual string getCoverType() const = 0;
     
    Rolf Magnus, Mar 5, 2004
    #4
  5. "Rolf Magnus" <> wrote in message
    news:c29v7t$p9u$04$-online.com...
    | Luis G Hernandez wrote:
    |
    | > I am required to create a pure virtual method for a project that
    | > returns a string. The method name is: getCoverType()
    | > I know that for a data type int I could do this:
    | >
    | > protected:
    | > // method to decide how to sort the list.
    | > virtual bool toCompare_ (const Key& key1, const Key& key2) const =
    | > 0;
    |
    | Where is the int?

    Right above :).

    Cheers.
    Chris Val
     
    Chris \( Val \), Mar 5, 2004
    #5
  6. Luis G Hernandez

    Jorge Rivera Guest

    John Harrison wrote:
    > "Luis G Hernandez" <> wrote in message
    > news:...
    >
    >>I am required to create a pure virtual method for a project that
    >>returns a string. The method name is: getCoverType()
    >>I know that for a data type int I could do this:
    >>
    >> protected:
    >> // method to decide how to sort the list.
    >> virtual bool toCompare_ (const Key& key1, const Key& key2) const =
    >>0;
    >>
    >>But, how would I declare this for a method that returns a string?
    >> //method to return a string for the book cover type
    >> virtual string getCoverType() /*what else ??*/ ;
    >>
    >>Thanks

    >
    >
    > Anything wrong with this?
    >
    > virtual string getCoverType() const = 0;
    >
    > Provided you have remembered to put this at the top of your code it should
    > be fine.
    >
    > #include <string>
    > using namespace std;


    Havin a using directive in a header file might not be a good idea.

    I personally prefer not to use the using command,
    and declare the function as:

    std::string getCoverType() const = 0;

    (You still need #include <string> )
     
    Jorge Rivera, Mar 6, 2004
    #6
    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.
Similar Threads
  1. Sameer
    Replies:
    4
    Views:
    613
    Roedy Green
    Aug 31, 2005
  2. Roy Smith
    Replies:
    5
    Views:
    415
    Duane Hebert
    Nov 18, 2005
  3. Chris Zopers

    Abstract method in non-abstract class

    Chris Zopers, Aug 8, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    383
    Göran Andersson
    Aug 8, 2008
  4. Stefan Ram
    Replies:
    27
    Views:
    761
    Stefan Ram
    Aug 17, 2009
  5. Marcel Müller
    Replies:
    2
    Views:
    128
    Marcel Müller
    Mar 15, 2014
Loading...

Share This Page