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:eltaEmp()
{
}
DeltaEmp DeltaEmp:perator + (int a)
{
DeltaEmp temp(empNo+a,empName);
return temp;
}
void DeltaEmp::display()
{
cout<<"empNo:: "<<empNo<<" empName:: "<<empName<<endl;
}
DeltaEmp:eltaEmp(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;
}
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:eltaEmp()
{
}
DeltaEmp DeltaEmp:perator + (int a)
{
DeltaEmp temp(empNo+a,empName);
return temp;
}
void DeltaEmp::display()
{
cout<<"empNo:: "<<empNo<<" empName:: "<<empName<<endl;
}
DeltaEmp:eltaEmp(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;
}