Constrcutors and Destructors

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2005
Posts: 86
Reputation: vicky_dev is an unknown quantity at this point 
Solved Threads: 2
vicky_dev's Avatar
vicky_dev vicky_dev is offline Offline
Junior Poster in Training

Constrcutors and Destructors

 
0
  #1
Jul 18th, 2006
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.
  1.  
  2. #include <iostream>
  3. #include <cstring>
  4. #include <conio.h>
  5. using namespace std;
  6.  
  7. class STRING
  8. {
  9. char *str;
  10. int len;
  11.  
  12. public:
  13.  
  14. STRING( ) { cout<<"*** Default Constructor called. ***"<<endl; str = NULL, len = 0; }
  15. STRING( const char *s );
  16. STRING( const STRING& );
  17. ~STRING( );
  18.  
  19. friend ostream& operator<<( ostream &os, STRING s );
  20. STRING operator+( STRING &s );
  21. };
  22.  
  23.  
  24. STRING::STRING( const char *s )
  25. {
  26. cout<<"*** String constructor called. ***\n";
  27. len = strlen( s );
  28. str = new char[len+1];
  29. strcpy( str, s );
  30. }
  31.  
  32. STRING::STRING( const STRING& s )
  33. {
  34. cout<<"*** Copy constructor called. ***\n";
  35. len = s.len;
  36. str = new char[len+1];
  37. strcpy( str, s.str );
  38. }
  39.  
  40. STRING::~STRING( )
  41. {
  42. cout<<"*** Destuctor called. ***"<<endl;
  43. delete[] str;
  44. len = 0;
  45. }
  46.  
  47. ostream& operator<<( ostream &os, STRING s )
  48. {
  49. if( s.len == 0 )
  50. cout<<"(Empty)\n";
  51. else
  52. os<<s.str<<endl;
  53. return os;
  54. }
  55.  
  56. STRING STRING::operator+( STRING &s )
  57. {
  58. STRING res;
  59.  
  60. res.len = len + s.len;
  61. res.str = new char[res.len+1];
  62.  
  63. strcpy( res.str, str );
  64. strcat( res.str, s.str );
  65.  
  66. return res;
  67. }
  68.  
  69. int main( )
  70. {
  71. STRING s1("String1");
  72. STRING s2("String2");
  73. STRING s3;
  74.  
  75. s3 = s1+s2;
  76.  
  77. cout<<s1<<s2<<s3;
  78.  
  79. getch( );
  80. return 0;
  81. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Constrcutors and Destructors

 
0
  #2
Jul 18th, 2006
Here's what I think happens.
  1.  
  2. STRING s1("String1"); //string constructor called
  3. STRING s2("String2"); //string constructor called
  4. STRING s3; //default constructor called
  5.  
  6. s3 = s1+s2; //default constructor called within the + operator
  7. //copy constructor called by compiler to create temporary object from res returned from + operator
  8. //destructor called to remove res
  9. //assignment operator called using default assignment constructor provided by compiler
  10. //destructor called to remove temporary object
  11.  
  12. cout<<s1<<s2<<s3;
  13.  
  14. getch( );
  15. return 0; //destructor called to destroy s1, s2, and s3.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 86
Reputation: vicky_dev is an unknown quantity at this point 
Solved Threads: 2
vicky_dev's Avatar
vicky_dev vicky_dev is offline Offline
Junior Poster in Training

Re: Constrcutors and Destructors

 
-1
  #3
Jul 18th, 2006
If that is how it happens, how come I dont get correct answer for s3 ( the result is shown blank, it must be String1String2 )
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Constrcutors and Destructors

 
0
  #4
Jul 19th, 2006
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.



#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*
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 86
Reputation: vicky_dev is an unknown quantity at this point 
Solved Threads: 2
vicky_dev's Avatar
vicky_dev vicky_dev is offline Offline
Junior Poster in Training

Re: Constrcutors and Destructors

 
0
  #5
Jul 19th, 2006
I dont think the code will work if you write it as: ( in main )

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Constrcutors and Destructors

 
0
  #6
Jul 19th, 2006
Originally Posted by vicky_dev
I dont think the code will work if you write it as: ( in main )

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.
Couldn't you just overload the equals operator then? I don't know, it's got to be something simple right?
Last edited by iamthwee; Jul 19th, 2006 at 11:32 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Constrcutors and Destructors

 
0
  #7
Jul 19th, 2006
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.

  1. #include <iostream>
  2. #include <cstring>
  3. #include <conio.h>
  4. using namespace std;
  5.  
  6. class STRING
  7. {
  8. char *str;
  9. int len;
  10.  
  11. public:
  12.  
  13. STRING( ) { cout<<"*** Default Constructor called. ***"<<endl; str = NULL, len = 0; }
  14. STRING( const char *s );
  15. STRING( const STRING& );
  16. ~STRING( );
  17.  
  18. friend ostream& operator<<( ostream &os, const STRING & s ); //slight change to syntax here
  19. STRING operator+( STRING &s );
  20. };
  21.  
  22.  
  23. STRING::STRING( const char *s )
  24. {
  25. cout<<"*** String constructor called. ***\n";
  26. len = strlen( s );
  27. str = new char[len+1];
  28. strcpy( str, s );
  29. }
  30.  
  31. STRING::STRING( const STRING& s )
  32. {
  33. cout<<"*** Copy constructor called. ***\n";
  34. len = s.len;
  35. str = new char[len+1];
  36. strcpy( str, s.str );
  37. }
  38.  
  39. STRING::~STRING( )
  40. {
  41. cout<<"*** Destuctor called. ***"<<endl;
  42. delete[] str;
  43. len = 0;
  44. }
  45.  
  46. ostream& operator<<( ostream &os, const STRING & s ) //slight change here
  47. {
  48. if( s.len == 0 )
  49. os<<"(Empty)"; //slight change here
  50. else
  51. os<<s.str; //slight change here
  52. return os;
  53. }
  54.  
  55. STRING STRING::operator+( STRING &s )
  56. {
  57. STRING res;
  58. char ch;
  59.  
  60. cout << "concatenation opereator called" << endl;
  61.  
  62. res.len = len + s.len;
  63. res.str = new char[res.len+1];
  64.  
  65. strcpy( res.str, str );
  66.  
  67. cout << "res.str now is" << res.str << endl;
  68. cin >> ch;
  69.  
  70. strcat( res.str, s.str );
  71.  
  72. cout << "res.str now is " << res.str << endl;
  73. cin >> ch;
  74.  
  75. cout << "end concatenation operator" << endl;
  76.  
  77. return res;
  78. }
  79.  
  80. int main( )
  81. {
  82. STRING s1("String1");
  83. STRING s2("String2");
  84. STRING s3;
  85.  
  86. //confirm expected values
  87. cout << s1 << endl;
  88. cout << s2 << endl;
  89. cout << s3 << endl;
  90.  
  91. s3 = s1+s2;
  92.  
  93. //determine if assignment operator worked.
  94. cout << s3;
  95. cout << endl;
  96.  
  97. cout << s1 << endl << s2 << endl << s3; //slight change in syntax
  98.  
  99. getch( );
  100. return 0;
  101. }
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 86
Reputation: vicky_dev is an unknown quantity at this point 
Solved Threads: 2
vicky_dev's Avatar
vicky_dev vicky_dev is offline Offline
Junior Poster in Training

Re: Constrcutors and Destructors

 
0
  #8
Jul 19th, 2006
Originally Posted by iamthwee
Couldn't you just overload the equals operator then? I don't know, it's got to be something simple right?
Overloading the = operator is same as writing a copy constructor, the same thing will be called when

s3 = s1 + s2;

is executed.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Constrcutors and Destructors

 
0
  #9
Jul 19th, 2006
Overloading the = operator is same as writing a copy constructor
The implementation of copy constructors and assignment operators is frequently similar, but they are not the same.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Constrcutors and Destructors

 
0
  #10
Jul 19th, 2006
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.
hmm, yeah I was just taking a stab at what might work. Probably overloading the equals operator won't make much difference if any?

Question: Has lerner's program helped in bringing you closer to a solution that you want?

To change :

  1. string d = a + b + c

to:-
  1. string d;
  2. 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*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC