Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Abstract base class + virtual Method

Reply
Thread Tools

Abstract base class + virtual Method

 
 
Luis G Hernandez
Guest
Posts: n/a
 
      03-05-2004
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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-05-2004

"Luis G Hernandez" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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


 
Reply With Quote
 
 
 
 
Chris \( Val \)
Guest
Posts: n/a
 
      03-05-2004

"Luis G Hernandez" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
| 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


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      03-05-2004
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;

 
Reply With Quote
 
Chris \( Val \)
Guest
Posts: n/a
 
      03-05-2004

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:c29v7t$p9u$04$(E-Mail Removed)-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


 
Reply With Quote
 
Jorge Rivera
Guest
Posts: n/a
 
      03-06-2004
John Harrison wrote:
> "Luis G Hernandez" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) m...
>
>>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> )
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using "abstract" on a class with no abstract method Stefan Ram Java 27 08-17-2009 12:36 AM
Abstract method in non-abstract class Chris Zopers ASP .Net 2 08-08-2008 11:40 AM
Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table? sojin C++ 12 04-07-2006 09:02 AM
About abstract class and abstract method Sameer Java 4 08-31-2005 12:59 AM
Deriving abstract class from non-abstract class Matthias Kaeppler Java 1 05-22-2005 01:28 PM



Advertisments