Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old February 27th, 2007, 04:35 AM
ali
Guest
 
Posts: n/a
Default virtual function and string - help

Hi,

I'm have a base class with a virual function toString. All the derived
classes will have to implement this function.

Here's the code I have used:

//in base.h
virtual string* toString();

//in base.cpp
string* base::toString()
{
return new string("Error: using base class toString method");
}

//in main.cpp
base b;
cout<<*(base.toString()<<endl;

I was wondering if there was a way i could use it so that in main.cpp
i could use something like:

cout<<base.toString()<<endl;

I'm learning C++, and since I've programmed in Java most of the time,
its getting a little confusing.

Thanks,

Ali

  #2  
Old February 27th, 2007, 04:55 AM
ali
Guest
 
Posts: n/a
Default Re: virtual function and string - help

On Feb 26, 10:28 pm, "ali" <aliasger.jaf...@gmail.comwrote:
Quote:
Hi,
>
I'm have a base class with a virual function toString. All the derived
classes will have to implement this function.
>
Here's the code I have used:
>
//in base.h
virtual string* toString();
>
//in base.cpp
string* base::toString()
{
return new string("Error: using base class toString method");
>
}
>
//in main.cpp
base b;
cout<<*(base.toString()<<endl;
>
I was wondering if there was a way i could use it so that in main.cpp
i could use something like:
>
cout<<base.toString()<<endl;
>
I'm learning C++, and since I've programmed in Java most of the time,
its getting a little confusing.
>
Thanks,
>
Ali
Sorry, correction:
//in main.cpp
base b;
cout<<*(b.toString())<<endl;

  #3  
Old February 27th, 2007, 04:55 AM
red floyd
Guest
 
Posts: n/a
Default Re: virtual function and string - help

ali wrote:
Quote:
Hi,
>
I'm have a base class with a virual function toString. All the derived
classes will have to implement this function.
>
Here's the code I have used:
>
//in base.h
virtual string* toString();
class base {
public:
virtual string toString() const = 0; // note: returns object not
// pointer. pure virtual
// requires derived
// classes to implement
};
Quote:
>
//in base.cpp
string* base::toString()
{
return new string("Error: using base class toString method");
}
implementation should be deleted. By making toString() pure virtual,
you can't call it through the base class, you can't even instantiate
an object that doesn't have an overridden toString().
Quote:
//in main.cpp
base b;
above is now illegal, assuming the pure virtual. Implement derived

class derived : public base
{
public:
string toString() const { /* some implementation */ }
};
derived d;
Quote:
cout<<*(base.toString()<<endl;
The pointer construct is not necessary once you return by value

cout << d.toString() << endl;
Quote:
I was wondering if there was a way i could use it so that in main.cpp
i could use something like:
>
cout<<base.toString()<<endl;
The above implementation is illegal because base has pure virtual
(abstract).
Quote:
I'm learning C++, and since I've programmed in Java most of the time,
its getting a little confusing.
Repeat after me. C++ is not Java. When possible, you don't want to
dynamically allocate objects.

Even better is to implement operator<< for base and children:

std::ostream& operator<<(std::ostream& os, const base& b)
{
os << b.toString();
return os;
}


This allows you to avoid the toString(), at least for output constructs.

Thus:

derived d;
std::cout << d << std::endl;
  #4  
Old February 27th, 2007, 05:05 AM
ali
Guest
 
Posts: n/a
Default Re: virtual function and string - help

On Feb 26, 10:42 pm, red floyd <no.s...@here.dudewrote:
Quote:
ali wrote:
Quote:
Hi,
>
Quote:
I'm have a base class with a virual function toString. All the derived
classes will have to implement this function.
>
Quote:
Here's the code I have used:
>
Quote:
//in base.h
virtual string* toString();
>
class base {
public:
virtual string toString() const = 0; // note: returns object not
// pointer. pure virtual
// requires derived
// classes to implement};
>
Quote:
//in base.cpp
string* base::toString()
{
return new string("Error: using base class toString method");
}
>
implementation should be deleted. By making toString() pure virtual,
you can't call it through the base class, you can't even instantiate
an object that doesn't have an overridden toString().
>
Quote:
//in main.cpp
base b;
>
above is now illegal, assuming the pure virtual. Implement derived
>
class derived : public base
{
public:
string toString() const { /* some implementation */ }};
>
derived d;
>
Quote:
cout<<*(base.toString()<<endl;
>
The pointer construct is not necessary once you return by value
>
cout << d.toString() << endl;
>
Quote:
I was wondering if there was a way i could use it so that in main.cpp
i could use something like:
>
Quote:
cout<<base.toString()<<endl;
>
The above implementation is illegal because base has pure virtual
(abstract).
>
Quote:
I'm learning C++, and since I've programmed in Java most of the time,
its getting a little confusing.
>
Repeat after me. C++ is not Java. When possible, you don't want to
dynamically allocate objects.
>
Even better is to implement operator<< for base and children:
>
std::ostream& operator<<(std::ostream& os, const base& b)
{
os << b.toString();
return os;
>
}
>
This allows you to avoid the toString(), at least for output constructs.
>
Thus:
>
derived d;
std::cout << d << std::endl;
Thanks Red!! I got the concept :) I think it would have been better to
learn C/C++ first and then Java, rather than the other way...

  #5  
Old February 27th, 2007, 06:25 AM
red floyd
Guest
 
Posts: n/a
Default Re: virtual function and string - help

ali wrote:
Quote:
>
Thanks Red!! I got the concept :) I think it would have been better to
learn C/C++ first and then Java, rather than the other way...
>
You're welcome. Just a point of netiquette -- so you won't get flamed.

There is no such language as C/C++. There is C, and there is C++, and
they are two very different languages. A lot of people here are
sticklers on that point. :)
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles