Want to change code from char type to string type !

hi

i want to change this code from char type to string type, is it possible? i put this '===>' where VC is giving error indication.

#include <iostream>
using namespace std;
#include <string.h> //for strcpy(), strcat()
#include <stdlib.h> //for exit()
////////////////////////////////////////////////////////////////
class Strings //user-defined string type
{
private:
//enum { SZ=80 }; //size of String objects
//char str[SZ]; //holds a string

string str;
public:
Strings() //constructor, no args
{ };
Strings( string s) //constructor, one arg
{ str = s; }
void display() const //display the String ========> this line has a problem
{ cout << str; }
Strings operator + (Strings ss) const //add Strings

{
Strings temp; //make a temporary String
//if( strlen(str) + strlen(ss.str) < SZ )
{
temp.str= str; //copy this string to temp
strcat(temp.str , ss.str); //add the argument string======> And this line is problem
}
//else
//{ cout << "\nString overflow"; exit(1); }
return temp; //return temp String
}
};
////////////////////////////////////////////////////////////////
int main()
{
Strings s1 = "\nMerry Christmas! "; //uses constructor 2
Strings s2 = "Happy new year!"; //uses constructor 2
Strings s3; //uses constructor 1
s1.display(); //display strings
s2.display();
s3.display();
s3 = s1 + s2; //add s2 to s1,
//assign to s3
s3.display(); //display s3
cout << endl;
return 0;
}
You forgot to #include <string> , which is not the same as #include <string.h> (and which is #include <cstring> in modern C++.)
Last edited on
hi
I added that cstring but it still give an error :(.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <iostream>

class Strings
{
   private: std::string str ;

   public:
      Strings() {}
      Strings( const char* cstr ) : str(cstr) {}
      Strings( const std::string& s ) : str(s) {}

      void display() const { std::cout << str << '\n'; } //display the Strings

      Strings operator + ( const Strings& ss ) const
      { return Strings( str + ss.str ) ; }
};

int main()
{
    Strings s1 = "\nMerry Christmas! "; //uses constructor 2
    Strings s2 = "Happy new year!"; //uses constructor 2
    Strings s3; //uses constructor 1
    s1.display(); //display strings
    s2.display();
    s3.display();
    s3 = s1 + s2; //add s2 to s1, assign to s3
    s3.display(); //display s3
}

@JLBorges


Can i use here string type rather then char *?

Strings( const char* cstr ) : str(cstr) {}
like
Strings( const string cstr ) : str(cstr) {}
Last edited on
> Can i use here string type rather then char *?

Since std::string has a copy constructor, the implementation will synthesise such a (copy) constructor -
Strings( const Strings& ) ;

You need the constructor which takes a const char*,
or else this code will not compile:
Strings s1 = "\nMerry Christmas! "; // construct Strings s1 from a const char*
@JLBorges

thanks alot

btw Whatis the purpose of this line here

Strings( const char* cstr ) : str(cstr) {}

regards,
Strings( const char* cstr ) : str(cstr) {} is a constructor; it initialises a Strings object, given a sequence of characters in a c-style string as input. The constructor, in turn - str(cstr) - initialises the member str with cstr.

See: http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/constructors.html
Last edited on
@JLBorges

I mean if i remove that line i am still able to play with string so is this necessary?

thanks
> I mean if i remove that line i am still able to play with string so is this necessary?

It is convenient.

Or else, you would have to write:
1
2
3
Strings s1 = std::string( "\nMerry Christmas! " ) ;
Strings s2 = std::string( "Happy new year!" ) ;
// etc... 

thanks man for support!
Regards
Topic archived. No new replies allowed.