You are here:

C++/string concetenate

Advertisement


Question
sir,
i am working in v.c++6.0.sir tell me what procedure i adopt
to concetenate the all private data member of student class in one string in string return type function. i.e regid,name,age,department .

Answer
The C++ library facility for this sort of thing is the IOStreams library.

We can stream the members of your class to some output stream using the stream insertion operators for the types of the data members (assuming all such members have such an operator). As the data members are private (as they should be!) such a function would either have to be a member function of the class or a friend function of the class.

If you specifically wish to concatenate

   #include <string>   // for std::string
   #include <iosfwd>   // for IOStreams forward declarations
         // or use <ostream> if VC6 has no <iosfwd> - I forget if it does or not

   class Student
   {
       unsigned int    regId_;
       std::string     name_;
       unsigned int    age_;
       std::string     department_;

   public:
       
   // ...
       
       void Serialise( std::ostream & out ) const;
   };

   void Student::Serialise( std::ostream & out ) const
   {
       out << regId_ << ' '  // remove or change spaces if not appropriate
         << name_ << ' '
         << age_ << ' '
         << department_
         ;
   }

We can then of course call this function from a non-member operator<< for the Student class:

   std::ostream & operator<<( std::ostream & out, Student const & s )
   {
       s.Serialise( out );
       return out;
   }
   
So Student objects can be output to a stream using the likes of:
   
   outStream << studentObject;

Should that be required.

I use the name "Serialise" as it takes an in memory object and writes it one part at a time (in serial) to some stream - a process often referred to as serialisation.

OK says you this is all well and good but I wanted to output my object to a string.

Ah ha! I say.  Then pass Serialise a std::ostringstream object:

   #include <sstream>

   // ...

   Student stu;
   
   // set up Student object stu's state...
   
   std::ostringstream oStrStrm;

   stu.Serialise(oStrStrm); // or oStrStrm << stu; is the operator<< function is defined.

   std::string serialsied_stu_string( oStrStrm.str() );

So if Student object stu had the following state:

   stu.regId_     =     12234
   stu.name_      =     "Reg Student"
   stu.age_       =     21
   stu.department_=     "Computer Science"

Then we would expect:

   serialsied_stu_string = "12234 Reg Student 21 Computer Science"

This shows the basic idea. You can of course modify this scheme in various ways. Here are a couple of possibilities:

   - change the delimiter between fields - for example remove it all together or place
      each one on a line by itself.
   - allow the delimiter character or string to be definable in some way
   - change the format of the output more radically e.g.
       + include the field name.
         This would have to be done manually as C++ throws away such information
         during compilation:
       
         out << "regid_" << '=' << regId_ << ' '
         ...
   
         This may be one of those cases where a preprocessor macro might be useful!
   - wrap up the std::ostringstream usage in a ToString (member) function.

There is also the inverse operation of de-serialisation - reading the serialised stream and creating (or at least updating) a Student object. I shall leave that as an exercise should you be interested.
   
Note that the code here is for exposition purposes only so is not complete or of production quality and may contain mistakes - typos etc - if so then I apologise.

Hope this helps.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.