Solved

a string class problem

Posted on 1998-02-05
9
266 Views
Last Modified: 2010-04-10
Following code gave wrong output, can you explain why. Any comments for the "correct" part of the code also wellcome :-)

#include <iostream.h>
#include <string.h>

class Mystring
{
private:
char *pstring;
int length;
public:
Mystring()
{  length = 0;
   pstring = NULL;
}
Mystring(char *pchar)
{  if (pstring!=NULL) delete pstring;
   length = strlen(pchar);
   pstring = new char[length + 1];
   strcpy(pstring, pchar);
}
Mystring(Mystring &mystring1)
{  if (pstring!=NULL) delete pstring;
   length = mystring1.length;
   pstring = new char [length + 1];
   strcpy (pstring, mystring1.pstring);
}
~Mystring()
{  delete pstring;
}
int Len (void)
{  return length;
}
void Show (void)
{  cout<<pstring<<endl;
}
};

int main ()
{  Mystring Mystring1;
   Mystring Mystring2("a string");
   Mystring Mystring3(Mystring2);     //Remove these 2 lines  
   Mystring2.Show(); Mystring3.Show();//the output become OK    
   Mystring1="another string";
   Mystring1.Show();
   return 0;
}

Without operator overloading, Do you think Mystring1="another string" is OK?
0
Comment
Question by:learn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 

Author Comment

by:learn
ID: 1180696
Edited text of question
0
 
LVL 3

Accepted Solution

by:
q2guo earned 50 total points
ID: 1180697
I don't see anything wrong except you should make the
following changes

~Mystring()
{
      if (pstring !=NULL) delete pstring;
}  

also, you should move your implementation of the member functions
out of the class definition.
e.g.
void MyString::show(void)
{
      .....
}

I don't think Mystring1="another string" is ok
since what this line actually does is
creates a temperory Mystring Object that contains "another string" and assigns this temmperory object to MyString1
because your MyString class contains pointer type data members
so, =operator will have to be overloaded.

Tell me if this helps

Terry
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1180698
Forget to ask you, what output are you getting anyway?
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:learn
ID: 1180699
Hi Q2guo,

1. The output from my code is something like "ano..er string" including some strange characters!

2. There is no operator (=) overloading in the code, how the string "another string" can go into the class and gave its value to the pointer pstring for Mystring1?

Cheers
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1180700
C++ compiler automatically creates the =operator function
for every object.  But, in this case, you don't want to use
the =operator provided by the compiler 'cause it will generate a bug in your program.

DOes mystring2.show() output ok in your program above?
0
 

Author Comment

by:learn
ID: 1180701
Yes, both Mystring2.Show() and Mystring3.Show() are OK.
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1180702
You are saying both Mystring2.show() and Mystring3.show()
work ok or are you saying they both give the correct output?
0
 

Author Comment

by:learn
ID: 1180703
Both outputs for Mystring2 and Mystring3 were correct.
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1180704
I tried out the program on my computer

the only problem I have is the
Mystring1="another string";
line, other than this everything works out fine


0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question