| | |
Constrcutors and Destructors
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hi...
I am trying to write a string class...this part is supposed to concatenate two strings, but I am having trouble with constructors/destructors.:rolleyes:
Please explain the exact sequence of calls and the correct way to code.
I am trying to write a string class...this part is supposed to concatenate two strings, but I am having trouble with constructors/destructors.:rolleyes:
Please explain the exact sequence of calls and the correct way to code.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> #include <conio.h> using namespace std; class STRING { char *str; int len; public: STRING( ) { cout<<"*** Default Constructor called. ***"<<endl; str = NULL, len = 0; } STRING( const char *s ); STRING( const STRING& ); ~STRING( ); friend ostream& operator<<( ostream &os, STRING s ); STRING operator+( STRING &s ); }; STRING::STRING( const char *s ) { cout<<"*** String constructor called. ***\n"; len = strlen( s ); str = new char[len+1]; strcpy( str, s ); } STRING::STRING( const STRING& s ) { cout<<"*** Copy constructor called. ***\n"; len = s.len; str = new char[len+1]; strcpy( str, s.str ); } STRING::~STRING( ) { cout<<"*** Destuctor called. ***"<<endl; delete[] str; len = 0; } ostream& operator<<( ostream &os, STRING s ) { if( s.len == 0 ) cout<<"(Empty)\n"; else os<<s.str<<endl; return os; } STRING STRING::operator+( STRING &s ) { STRING res; res.len = len + s.len; res.str = new char[res.len+1]; strcpy( res.str, str ); strcat( res.str, s.str ); return res; } int main( ) { STRING s1("String1"); STRING s2("String2"); STRING s3; s3 = s1+s2; cout<<s1<<s2<<s3; getch( ); return 0; }
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
Here's what I think happens.
C++ Syntax (Toggle Plain Text)
STRING s1("String1"); //string constructor called STRING s2("String2"); //string constructor called STRING s3; //default constructor called s3 = s1+s2; //default constructor called within the + operator //copy constructor called by compiler to create temporary object from res returned from + operator //destructor called to remove res //assignment operator called using default assignment constructor provided by compiler //destructor called to remove temporary object cout<<s1<<s2<<s3; getch( ); return 0; //destructor called to destroy s1, s2, and s3.
Hi I just made one now, this appears to work although it's more of a trial and error thing.
I've highlighted the part that may be suspect in blue. Someone who knows what they're doing can tell you if my coding is logical and safe.
I've highlighted the part that may be suspect in blue. Someone who knows what they're doing can tell you if my coding is logical and safe.
#include <iostream>
#include <iomanip>
#include <cstring>
class String
{
private:
char *mem;
int size;
public:
String ( const char *p );
~String();
friend std::ostream& operator<< ( std::ostream& os, const String& s );
String operator+(String); //Overloaded + operator
};
String::String ( const char *p )
{
size = strlen ( p );
mem = new char[size + 1];
strcpy ( mem, p );
}
String::~String()
{
delete [] mem;
}
std::ostream& operator<< ( std::ostream& os, const String& s )
{
return os << s.mem;
}
String String::operator+(String c)
{
char *crap;
crap = new char[size + c.size+1];
strcpy(crap,mem);
strcat(crap,c.mem);
String temp = crap;
return temp;
}
int main()
{
String a = "My cool string";
String b = " is so crappy";
String c = " ya know";
String d = a + b + c;
std::cout << d << '\n';
std::cin.get();
return 0;
} *Voted best profile in the world*
I dont think the code will work if you write it as: ( in main )
This makes all the difference.
String a = "My cool string";
String b = " is so crappy";
String c = " ya know";
String d;
d = a + b + c ;
This makes all the difference.
Last edited by vicky_dev; Jul 19th, 2006 at 11:08 am.
•
•
•
•
Originally Posted by vicky_dev
I dont think the code will work if you write it as: ( in main )
This makes all the difference.String a = "My cool string"; String b = " is so crappy"; String c = " ya know"; String d; d = a + b + c ;
Last edited by iamthwee; Jul 19th, 2006 at 11:32 am.
*Voted best profile in the world*
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
Run time debugging is cumbersome. Here's how I do it; lots of output statements with pauses to see that status is as I want it to be. I suspect everybody does it differently.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> #include <conio.h> using namespace std; class STRING { char *str; int len; public: STRING( ) { cout<<"*** Default Constructor called. ***"<<endl; str = NULL, len = 0; } STRING( const char *s ); STRING( const STRING& ); ~STRING( ); friend ostream& operator<<( ostream &os, const STRING & s ); //slight change to syntax here STRING operator+( STRING &s ); }; STRING::STRING( const char *s ) { cout<<"*** String constructor called. ***\n"; len = strlen( s ); str = new char[len+1]; strcpy( str, s ); } STRING::STRING( const STRING& s ) { cout<<"*** Copy constructor called. ***\n"; len = s.len; str = new char[len+1]; strcpy( str, s.str ); } STRING::~STRING( ) { cout<<"*** Destuctor called. ***"<<endl; delete[] str; len = 0; } ostream& operator<<( ostream &os, const STRING & s ) //slight change here { if( s.len == 0 ) os<<"(Empty)"; //slight change here else os<<s.str; //slight change here return os; } STRING STRING::operator+( STRING &s ) { STRING res; char ch; cout << "concatenation opereator called" << endl; res.len = len + s.len; res.str = new char[res.len+1]; strcpy( res.str, str ); cout << "res.str now is" << res.str << endl; cin >> ch; strcat( res.str, s.str ); cout << "res.str now is " << res.str << endl; cin >> ch; cout << "end concatenation operator" << endl; return res; } int main( ) { STRING s1("String1"); STRING s2("String2"); STRING s3; //confirm expected values cout << s1 << endl; cout << s2 << endl; cout << s3 << endl; s3 = s1+s2; //determine if assignment operator worked. cout << s3; cout << endl; cout << s1 << endl << s2 << endl << s3; //slight change in syntax getch( ); return 0; }
•
•
•
•
Originally Posted by vicky_dev
Overloading the = operator is same as writing a copy constructor, the same thing will be called when
s3 = s1 + s2;
is executed.
Question: Has lerner's program helped in bringing you closer to a solution that you want?
To change :
C++ Syntax (Toggle Plain Text)
string d = a + b + c
to:-
C++ Syntax (Toggle Plain Text)
string d; d = a + b + c
Seems such a trivial exercise however, I can't find any examples on the net. Surely someone else knows how to do it?
*Voted best profile in the world*
![]() |
Other Threads in the C++ Forum
- Previous Thread: need help w/ 'invalid conversion'
- Next Thread: File Mainipulation
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






