Operator function not working

Y

Yang Lee

Hi,
Could you please try to run the following program
it doesnt work for operator overloading function.
It doesn't print peoper empName for emp2 object. It prints junk.
Just what could be the cause.
I get values for emp2 object from following

DeltaEmp emp2=*emp1+10;

where emp1 is a pointer to object of class DeltaEmp.
In operator function I just want to add 10 to empNo
and retain the same employee no for emp2 object.

Yes I would have taken emp1 not as a pointer then
it would have worked but I wanted the program work in
the same way with emp1 as pointer.

Thank you.
Lee


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


class DeltaEmp
{
int empNo;
char *empName;

public:
DeltaEmp();
DeltaEmp(int a, char *s);
void display();
friend void changeEmpRecord(DeltaEmp &,int, char *);
DeltaEmp operator + (int);
~DeltaEmp();
};

DeltaEmp::DeltaEmp()
{
}

DeltaEmp DeltaEmp::eek:perator + (int a)
{
DeltaEmp temp(empNo+a,empName);
return temp;
}

void DeltaEmp::display()
{
cout<<"empNo:: "<<empNo<<" empName:: "<<empName<<endl;
}


DeltaEmp::DeltaEmp(int a, char *s)
{
empNo=a;
empName=new char[strlen(s) + 1];
strcpy(empName,s);
}

DeltaEmp::~DeltaEmp()
{
delete []empName;
}

void changeEmpRecord(DeltaEmp &rEmp,int empNo, char *newEmpName)
{
rEmp.empNo=empNo;
delete []rEmp.empName;
rEmp.empName=new char[strlen(newEmpName) + 1];
strcpy(rEmp.empName,newEmpName);
}

int main()
{
DeltaEmp *emp1;

emp1=new DeltaEmp (5, "Sambhe");
emp1->display();
DeltaEmp emp2=*emp1+10;
emp2.display();
delete emp1;
return 0;
}
 
D

David White

Yang Lee said:
Hi,
Could you please try to run the following program
it doesnt work for operator overloading function.
It doesn't print peoper empName for emp2 object. It prints junk.
Just what could be the cause.
I get values for emp2 object from following

DeltaEmp emp2=*emp1+10;

where emp1 is a pointer to object of class DeltaEmp.
In operator function I just want to add 10 to empNo
and retain the same employee no for emp2 object.

Yes I would have taken emp1 not as a pointer then
it would have worked but I wanted the program work in
the same way with emp1 as pointer.

Your problem has nothing to do with emp1 being a pointer.
Thank you.
Lee


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


class DeltaEmp
{
int empNo;
char *empName;

public:
DeltaEmp();

Since you have a pointer member, you need a user-defined copy constructor
and assignment operator.
DeltaEmp(int a, char *s);
void display();
friend void changeEmpRecord(DeltaEmp &,int, char *);
DeltaEmp operator + (int);
~DeltaEmp();
};

DeltaEmp::DeltaEmp()
{

You aren't initializing the members here (see destructor).
}

DeltaEmp DeltaEmp::eek:perator + (int a)
{
DeltaEmp temp(empNo+a,empName);
return temp;
}

void DeltaEmp::display()
{
cout<<"empNo:: "<<empNo<<" empName:: "<<empName<<endl;
}


DeltaEmp::DeltaEmp(int a, char *s)
{
empNo=a;
empName=new char[strlen(s) + 1];
strcpy(empName,s);
}

DeltaEmp::~DeltaEmp()
{
delete []empName;

Big trouble here if the default constructor was used.
}

void changeEmpRecord(DeltaEmp &rEmp,int empNo, char *newEmpName)
{
rEmp.empNo=empNo;
delete []rEmp.empName;

And here.
rEmp.empName=new char[strlen(newEmpName) + 1];
strcpy(rEmp.empName,newEmpName);
}

int main()
{
DeltaEmp *emp1;

emp1=new DeltaEmp (5, "Sambhe");
emp1->display();
DeltaEmp emp2=*emp1+10;

This expression leaves emp2 with empName pointing to deleted memory, because
the 'temp' object in your operator+ has now been destroyed, deleting the
storage that empName points to. You need a copy constructor and assignment
operator that handles the pointer member correctly.
emp2.display();

For me the output was:
empNo:: 5 empName:: Sambhe
empNo:: 15 empName:: Sambhe

But that was just unlucky (a crash or junk output is better because then you
know there's a problem, as you've discovered).
delete emp1;
return 0;
}

DW
 
C

Chris Mantoulidis

#include said:
#include <string.h>

first of all, since this is a C++ newsgroup I'd say you use <iostream>
and said:
class DeltaEmp
{
int empNo;
char *empName;

why use char *?? Use s
public:
DeltaEmp();
DeltaEmp(int a, char *s);
void display();
friend void changeEmpRecord(DeltaEmp &,int, char *);
DeltaEmp operator + (int);
~DeltaEmp();
};

Well, what about the following

class DeltaEmp {
friend ostream & operator << (ostream & os, DeltaEmp & parDEmp) {
parDEmp.display(os);
return os;
}
private:
int empNo;
string empName;
public:
DeltaEmp() { empNo = 0; empName = ""; }
DeltaEmp(int parEmpNo, string parEmpName) {
empNo = parEmpNo;
empName = parEmpName;
}
~DeltaEmp() { /* ..... */ }
void SetNo(int parNo) { empNo = parNo; }
int GetNo() { return empNo; }
void SetName(string parName) { empName = parName; }
string GetName() { return empName; }
void display(ostream & os) {
os << "EmpNo: " << empNo << "\nEmpName: " << empName << endl;
}
};

Sorry for the crowded code :) Now, you can use this like...

int main() {
DeltaEmp *de1 = new DeltaEmp(1, "pointer");
cout << *de1;

DeltaEmp de2(de1->GetNo() + 10, "someone else");
cout << de2;

delete de1;

return 0;
}
 
C

Chris Mantoulidis

class DeltaEmp
{
int empNo;
char *empName;

I commented something before here, something like "don't use char *,
use s"... That was a typo, instead of "s" I meant "string"...

cmad
 
M

Michael Mellor

Chris said:
first of all, since this is a C++ newsgroup I'd say you use <iostream>



why use char *?? Use s




Well, what about the following

class DeltaEmp {
friend ostream & operator << (ostream & os, DeltaEmp & parDEmp) {
parDEmp.display(os);
return os;
}
Why have you made this a friend?


It would be better to make the second argument constant:
ostream & operator << (ostream & os, const DeltaEmp & parDEmp) {
parDEmp.display(os);
return os;
}
and make the display method const as well:
void display(ostream & os) const {
os << "EmpNo: " << empNo << "\nEmpName: " << empName << endl;
}
so that constant objects can be displayed.

Mike
 
C

Chris Mantoulidis

Why have you made this a friend?
It would be better to make the second argument constant:
ostream & operator << (ostream & os, const DeltaEmp & parDEmp) {
parDEmp.display(os);
return os;
}
and make the display method const as well:
void display(ostream & os) const {
os << "EmpNo: " << empNo << "\nEmpName: " << empName << endl;
}
so that constant objects can be displayed.

Mike

The thing's that I haven't used C++ to make classes for some time...
So I forgot to do "void display(ostream & os) _*const*_ {" (I didn't
add const) so I would get an error message if I would try to compile
it with "const DeltaEmp & parEmp"... So thanks, you reminded me... heh

Why did I make that a friend eh? Donno... lol...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top