Building integer into a string

D

dev_15

Hi, this is one of the questions from Bruce Ekels thinking in C++
book, chapter 12, question 14, i have only posted the first part of
the question that i am stuck with.

How do you combine an integer into the string class in C++?

Write a class called Bird that contains a string member and a static
int. In the default constructor, use the int to automatically generate
an identifier that you build in the string, along with the name of the
class (Bird #1, Bird #2, etc.).

Thanks
 
N

Neelesh Bodas

Hi, this is one of the questions from Bruce Ekels thinking in C++
book, chapter 12, question 14, i have only posted the first part of
the question that i am stuck with.

How do you combine an integer into the string class in C++?

Write a class called Bird that contains a string member and a static
int. In the default constructor, use the int to automatically generate
an identifier that you build in the string, along with the name of the
class (Bird #1, Bird #2, etc.).

Thanks

use stringstream ($include <sstream>)

//sample code
class Bird
{
private:
string name;
static int cnt;
public:
Bird()
{
stringstream ss;
ss << cnt;
name = "Bird#" + ss.str();
cnt++;
}
};

-N
int Bird::cnt = 1;
 

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
474,201
Messages
2,571,052
Members
47,656
Latest member
rickwatson

Latest Threads

Top