Abstract base class + virtual Method

  • Thread starter Luis G Hernandez
  • Start date
L

Luis G Hernandez

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
 
J

John Harrison

Luis G Hernandez said:
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
 
C

Chris \( Val \)

| 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
 
R

Rolf Magnus

Luis said:
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;
 
C

Chris \( Val \)

| 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
 
J

Jorge Rivera

John said:
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> )
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top