473,406 Members | 2,745 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,406 software developers and data experts.

virtual function and string - help

ali
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

Feb 27 '07 #1
4 1769
ali
On Feb 26, 10:28 pm, "ali" <aliasger.jaf...@gmail.comwrote:
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;

Feb 27 '07 #2
ali wrote:
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
};
>
//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().
//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;
cout<<*(base.toString()<<endl;
The pointer construct is not necessary once you return by value

cout << d.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;
The above implementation is illegal because base has pure virtual
(abstract).
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;
Feb 27 '07 #3
ali
On Feb 26, 10:42 pm, red floyd <no.s...@here.dudewrote:
ali wrote:
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};
//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().
//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;
cout<<*(base.toString()<<endl;

The pointer construct is not necessary once you return by value

cout << d.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;

The above implementation is illegal because base has pure virtual
(abstract).
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...

Feb 27 '07 #4
ali wrote:
>
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. :)
Feb 27 '07 #5

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

Similar topics

20
by: qazmlp | last post by:
My class in a header file, contains inline virtual destructor. Is this Ok? Can it cause any problems? class base { public: base() { } virtual ~base { std::cout<<"Inside virtual destructor\n";...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
5
by: Duck Dodgers | last post by:
Here is my situation class base { }; class child1 { int data; }; class child2 {
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
6
by: Alden Pierre | last post by:
Hello, http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 As per the link above it's wise to have a virtual deconstructor when creating an abstract class. Here is when I'm...
3
by: trialproduct2004 | last post by:
Hi all, Can someone tell me how virtual functions works. Like how memory is getting allocated to virtual function. And how to call base class function through derived class pointer. And why...
4
by: tommaso.gastaldi | last post by:
Hi friends I was in the need to find a sort of "definitive" :) solution to transform a virtual path such as "~/MyDir/MyFile into a full web address. In particular I needed it * within web...
12
by: mijobee | last post by:
I'm very new to c++ and just writing some code to learn. I've run into a problem, with a javaish design, and want to know if there is any possible solution without modifying the design. I've read...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.