hello c++ pros,
iam a c++ beginner please help me with this code..iam unable to get the proper out put.In the above class i have tried to imitate String class and overloaded few operators. I got strucked up as show() method above was unable to print "t3" string properly.Please help me out.thanx in advance for the one who comes up with solution.

Edited 6 Years Ago by WaltP: This is not a code snippet. The is a request for help. Post as a normal post.

You don't need to pass in another string to your show() method as you already have access to the one within the object. It should just be cout <<p; or you can #include <cstdio> and use puts() P.S. When you are asking for help in the forums post it as a question not as a code snippet please.

but why only with string "t3" ,why that error didnt occured when i use "t2" or "t1" string??

I overlooked that show was a friend function and not a member but that's not where the error is.

In performing the + operation you need to return a reference to the calling object otherwise you're getting no result back so it's left with junk in the value on the other side of the equal sign.

Use this as the beginning of your method:

string & operator+(const string &s,const string &t)
{
	string* temp = new string();

you'll need to make the changes to account for the fact that temp is a pointer (hint: use the -> operator for all of temp's members) and at the end return *temp; so that you return the proper reference.

Edited 6 Years Ago by jonsca: n/a

#include<iostream.h>
class complex 
{
	float x;
	float y;
	public:
	   complex(){}
	   complex(float real,float imag)
	   {
   		x= real,y=imag;
   	}
  complex & operator+(complex);
   	   void display(void);
};
 complex & complex::operator+(complex c)
 {
 	complex *temp;
 	temp->x=x+c.x;
 	temp->y=y+c.y;
 	 return(*temp);
 }
 void complex::display(void)
 {
 	cout<<x<<"+j"<<y<<"\n";
 }
 int main()
 {
 	complex c1,c2,c3;
 	c1=complex(2.5,3.5);
 	c2=complex(1.6,2.7);
 	c3=c2+c1;
 	cout<<"c1=";c1.display();
 	cout<<"c2=";c2.display();
 	cout<<"c3=";c3.display();
 	return 0;
 }

iam unable to get the appropriate out put for this code.I think the problem is with the operator overloading.Could any one come up with solution??

Edited 6 Years Ago by srinath3: n/a

You need to instantiate your temp variable. Change line 17 to complex *temp = new complex(); otherwise you're writing to an uninitialized pointer.

iam unable to get the appropriate out put for this code.I think the problem is with the operator overloading.Could any one come up with solution??

The main problem I can see is in your operator overload at line 17. The pointer 'temp' is uninitialised, it doesn't point to anything.
You need to use:

complex *temp = new complex();

Which creates a new instance of the complex class and assigns the pointer to point to it. Now your calculation should work.

I also noticed that you're not qualifying the std::namespace for cout. I'm not sure whether or not your compiler needs the qualification, I'm assuming your one of those unfortunate students who've been forced by your institution to use an old borland compiler. It's been donkeys years since I used any of Borlands old compilers, so I really can't remember. But on the off-chance that it does, it might be an idea to put the following line of code underneath the #include:

using std::cout;

(Obviously if you aren't getting error messages about it, your compiler doesn't need it so don't bother!)

Looking at your code, you might also get some warnings about truncation from double to float at lines 29 and 30. (Again, if you're using an old Borland compiler you might not get the warnings. My memory doesn't stretch back that far!)
On the offchance that you are getting warnings, you can get rid of them by doing this:

c1 = complex(2.5f, 3.5f);
    c2 = complex(1.6f, 2.7f);

When you pass a floating point value into a function as a literal value, most modern compilers will automatically treat it as a double. If a float is required, you can tell the compiler that the value is a float by putting an f at the end of each value.
As your functions take floats as parameters, the literal values should have an f appended at the end.
Again, I'm not sure whether the old Borland compilers had this behaviour or not. If not, don't worry about it. But it's something to bear in mind for when you eventually do start using a more up to date compiler.

I hope this helps!
Cheers for now,
Jas.

[edit: oops. Jonsca got in there while I was rambling...Sorry Jonsca, didn't see you were online!]

Edited 6 Years Ago by JasonHippy: n/a

Comments
Above and beyond

but in the case of the first code ,i havent initialized the string,but still it worked.

You must have, this was the change that I had recommended.

string & operator+(const string &s,const string &t)
{
	string* temp = new string();

for posterity: string was a class built using cstring functions not the std::string (changing the post from snippet to regular separated the code somehow)

Edited 6 Years Ago by jonsca: n/a

@jason hippy:yes me using the old compiler :-p but im the begginer iam working on basic c++ programs.As of now iam not getting any errors as u have told i need not take any data conversions!

Cool. I thought maybe it had gotten lost in the communication before. Even though you are using the older compiler you should still be very much aware of what has been standardized since then. In addition to what JH pointed out, you should use #include <iostream> instead of iostream.h to get the standard header. Same goes for string.h which has become #include<cstring> with the standard. Just pointing it out so you don't get a culture shock should you have to change.

Jonsca PM'ed me and brought something to my attention that raises a very good point regarding this thread.

We've both recommended that the OP uses 'new' to create a new instance of the complex class in the operator+, but this would actually cause a memory leak as the memory allocated by new never gets deleted.

After a few messages back and forth, I came up with this solution which should completely avoid a memory leak.... I think!

#include<iostream>

class complex 
{
	float x;
	float y;
	public:
	   complex(){}
	   complex(float real,float imag)
	   {
   		x= real,y=imag;
   	}
  complex operator+(const complex)const;
   	   void display(void);
};
 complex complex::operator+(const complex c)const
 {
	complex temp(*this);
	temp.x+=c.x;
	temp.y+=c.y;
 	 return(temp);
 }
 void complex::display(void)
 {
	 std::cout<<x<<"+j"<<y<<"\n";
 }
 int main()
 {
 	complex c1,c2,c3;
 	c1=complex(2.5f,3.5f);
 	c2=complex(1.6f,2.7f);

 	c3=c2+c1;
	std::cout<<"c1=";c1.display();
	std::cout<<"c2=";c2.display();
	std::cout<<"c3=";c3.display();
 	return 0;
 }

So the major changes there are in the operator+. Instead of returning a pointer or a reference to a complex object, I've changed the signature to return a complex object by value. For the sake of const correctness I've also made the function and the parameter to the function const.

In the operator+ function, instead of creating a pointer to a new complex object, I'm using a temporary local complex object created using the this pointer. Effectively, that makes temp a copy of the left hand operand in the addition operation. We then add the passed in object (the right hand operand in the addition) to temp and return temp by value, so a copy of temp is returned.

Note: If we returned a pointer or a reference to temp, it would end up pointing to something that was no longer in scope, hence the reason for passing by value!

So downgrading the above code to allow it to compile on an old borland compiler, the OPS code should look something like this:

#include<iostream.h>

class complex 
{
	float x;
	float y;
	public:
	   complex(){}
	   complex(float real,float imag)
	   {
   		x= real,y=imag;
   	}
  complex operator+(const complex)const;
   	   void display(void);
};
 complex complex::operator+(const complex c)const
 {
	complex temp(*this);
	temp.x+=c.x;
	temp.y+=c.y;
 	return(temp);
 }
 void complex::display(void)
 {
	 cout<<x<<"+j"<<y<<"\n";
 }
 int main()
 {
 	complex c1,c2,c3;
 	c1=complex(2.5f,3.5f);
 	c2=complex(1.6f,2.7f);

 	c3=c2+c1;
	cout<<"c1=";c1.display();
	cout<<"c2=";c2.display();
	cout<<"c3=";c3.display();
 	return 0;
 }

Cheers for now,
Jas. (in collaboration with Jonsca!)

oh jason hippy.Thats great.And iam happy about your interest in solving this perticular thread.but one doubt.Uve said that u used CONST for correctness.I justwant to know the exact reason about it usage.

Comments
In English we add a SPACE after the '.' at the end of a sentence. Please use them.

Read through at least the first few paragraphs of this (the rest starts to get into some specifics you may not need right now).

This article has been dead for over six months. Start a new discussion instead.