473,395 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Operator function not working

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::operator + (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;
}
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Jul 22 '05 #1
5 1654
"Yang Lee" <ya*******@ausi.com> wrote in message
news:64************************************@mygate .mailgate.org...
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::operator + (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

Jul 22 '05 #2
> #include <iostream.h>
#include <string.h>
first of all, since this is a C++ newsgroup I'd say you use <iostream>
and <string>... and then do "using namespace std;"...
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;
}

---

cmad
Jul 22 '05 #3
> 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
Jul 22 '05 #4
Chris Mantoulidis wrote:
#include <iostream.h>
#include <string.h>

first of all, since this is a C++ newsgroup I'd say you use <iostream>
and <string>... and then do "using namespace std;"...

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;
}

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
Jul 22 '05 #5
> 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...
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
18
by: Joe Seigh | last post by:
Is there a good write on this. The textbooks I have fluff over on this? Specifically, I trying to dereference with 2 levels of type conversion not 1, i.e. X<T> -> z => Y<T> -> z => T* ->...
3
by: glen stark | last post by:
Hi all. I'm working with an array of member function pointers (they are all get function of the class Bead). The typedef is: typedef _real (Bead::*_beadGfp)(void); I have a class System...
3
by: KK | last post by:
Hi, im working on this bigInt class. Need help writing algorithm for the operator*, andy help will be appreciated. Thanks in advance bigInt.h...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
0
by: Pedro | last post by:
Hello pythonians! ;-D , I have a little problem when I expose (assisted by boost.python) classes with virtual functions, specially with operator(). In the C++ code below I test two different...
1
by: acheron05 | last post by:
Hi, I've been writing a program for another school assignment but I am having trouble working out how to overload the < and << operators. The program is designed to read data from a file, create...
7
by: Stephan Rose | last post by:
I am currently working on an EDA app and heavily working on squeezing the last bits of performance out of it. Going as far as sending batches of geometry to the video card while still processing...
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.