473,385 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Abstract base class + virtual Method

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
Jul 22 '05 #1
5 1339

"Luis G Hernandez" <si****@hotmail.com> wrote in message
news:70*************************@posting.google.co 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
Jul 22 '05 #2

"Luis G Hernandez" <si****@hotmail.com> wrote in message
news:70*************************@posting.google.co 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
Jul 22 '05 #3
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;

Jul 22 '05 #4

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c2*************@news.t-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
Jul 22 '05 #5
John Harrison wrote:
"Luis G Hernandez" <si****@hotmail.com> wrote in message
news:70*************************@posting.google.co 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> )
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
8
by: Dev | last post by:
Hello, Why an Abstract Base Class cannot be instantiated ? Does anybody know of the object construction internals ? What is the missing information that prevents the construction ? TIA....
12
by: placid | last post by:
Hi if a have the following classes class A { public: A(); virtual ~A(); virtual string someFunction() const = 0;
6
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
15
by: Pohihihi | last post by:
How can I overload abstract type method in child class? e.g. public abstract void BaseMethod() { // do something } // in child class
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
9
by: silversurfer2025 | last post by:
Hello everyone, I am currently having problems with a C++ abstract class. I have a class FrameWork.h which defines some methods (of which some are abstract, i.e. virtual void method() = 0). In...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.