By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,795 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,795 IT Pros & Developers. It's quick & easy.

Overloaded subtraction operator - memory problem

P: n/a
I've written an interface and implementation file along with a client
source file that allows the use of an overloaded subtraction operator.
However, when
using the program, I'm running into a memory problem that I'm not
readily seeing that lies within the overloaded operator - I think
pertaining to the temporary class object that is created and used to
return a value of the operands and the pointer variable of the class.
Could someone point out what I'm failing to see - any addtional
information or approaches would be useful and well-received. Thank
you as always...
Anthony

//Stocks.h interface file
#if !defined(STOCKS_H)
#define STOCKS_H

class Stocks
{
public:
Stocks(); //default constructor
Stocks(char* szName); //parameterized constructor
Stocks(const Stocks&);//copy constructor declaration
~Stocks(); //destructor

//accessor function declarations
void setStockName(char* szName);
void setNumShares(int);
void setPricePerShare(double);
char* getStockName() const;
int getNumShares() const;
double getPricePerShare() const;
double calcTotalValue();
friend Stocks operator - (const Stocks& operand1, const Stocks&
operand2);
private:
//declare private data members
char* szStockName
int iNumShares;
double dCurrentValue;
double dPricePerShare;
};//end class Stocks.h

#endif

/////////////////////////////////////////////////////
//Stocks.cpp implementation file
#include "Stocks.h"
#include <cstring>
#include <ostream.h>

Stocks::Stocks()//defines default constructor
{
szStockName = new char[25];
strcpy(szStockName,"");
iNumShares = 0;
dCurrentValue = 0;
dPricePerShare = 0;
};

Stocks::Stocks(char* szName)//defines parameterized constructor
{
szStockName = new char[25];
strcpy(szStockName,szName);
iNumShares = 0;
dCurrentValue = 0;
dPricePerShare = 0;
};

Stocks::Stocks(const Stocks& sourceStock)//copy constructor definition
{
szStockName = new char[25];
strcpy(szStockName, sourceStock.szStockName);
iNumShares = 0;
dCurrentValue = 0;
dPricePerShare = 0;
};

Stocks::~Stocks()//DESTRUCTOR
{
delete[] szStockName;
cout << "Destructor called." << endl;

}

//OVERLOADED SUBTRACTION OPERATOR
Stocks operator - (const Stocks& operand1, const Stocks& operand2)
{
Stocks shares;
shares.iNumShares = operand1.iNumShares - operand2.iNumShares;
return shares;

}
void Stocks::setStockName(char* szName)
{
strcpy(szStockName,szName);
}

void Stocks::setNumShares(int iShares)
{
iNumShares = iShares;
}

void Stocks::setPricePerShare(double dPrice)
{
dPricePerShare = dPrice;
}

char* Stocks::getStockName() const
{
return szStockName;
}

int Stocks::getNumShares(void)const
{
return iNumShares;
}

double Stocks::getPricePerShare()const
{
return dPricePerShare;
}

double Stocks::calcTotalValue()
{
dCurrentValue = iNumShares * dPricePerShare;
return dCurrentValue;
}

/////////////////////////////////////////////
//Shares.cpp - client source file

#include "Stocks.h"
#include <iostream>
#include <cstring>
#include <iomanip>//for use of setprecision() and setiosflags()
using namespace std;

int main()
{
cout << "\t\t\t\t\b\bStock Portfolio" << endl << endl
<< "This program displays the current values of stock you have
purchased for" << endl
<< "both Cisco and Lucent Technologies." << endl << endl;

//declares class object variable and passes value to parameterized
constructor
Stocks stockPick1("Cisco");
stockPick1.setNumShares(300);
stockPick1.setPricePerShare(68.875);
cout << stockPick1.getNumShares() << endl;
Stocks stockPick2(stockPick1);//uses copy constructor
stockPick2.setStockName("Lucent");
stockPick2.setNumShares(200);
stockPick2.setPricePerShare(59.5);

//format numeric output in fixed notation with two decimals
cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint);

cout << "The current value of your stock in " <<
stockPick1.getStockName()
<< " is $" << stockPick1.calcTotalValue() << "." << endl;
cout << "The current value of your stock in " <<
stockPick2.getStockName()
<< " is $" << stockPick2.calcTotalValue() << "." << endl;

Stocks shares;
shares = stockPick1 - stockPick2;//calls overloaded subtraction opr.

cout << "Number of stocks after deduction: "
<< shares.getNumShares() << endl;

return EXIT_SUCCESS;
}//end main
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"August1" <an*********@hotmail.com> wrote in message
news:fc**************************@posting.google.c om...
I've written an interface and implementation file along with a client
source file that allows the use of an overloaded subtraction operator.
However, when
using the program, I'm running into a memory problem that I'm not
readily seeing that lies within the overloaded operator - I think
pertaining to the temporary class object that is created and used to
return a value of the operands and the pointer variable of the class.


No, the problem is that you haven't not defined an assignment operator for
your class. You use it right after the operator- but the compiler generated
on you are using does not do the right thing.

Whenever you have a destructor, you almost always need to provide a copy
constructor AND an assignment operator. This is called the 'rule of three'.

john
Jul 22 '05 #2

P: n/a
"> No, the problem is that you haven't not defined an assignment
operator for
your class. You use it right after the operator- but the compiler generated
on you are using does not do the right thing.

Whenever you have a destructor, you almost always need to provide a copy
constructor AND an assignment operator. This is called the 'rule of three'.

john


Hi John,

Thanks for the response. I added an overloaded assignment operator
to see what would take place, and this did resolve the problem with
program execution. However, it still does not perform as intended.
Specifically, the value derived from subtracting one operand from the
other does not appear to be assigned to the data member within the
overloaded subtraction operator that is assigned to the temporary
class object and returned. When I use the shares.getNumShares()
function at the end of the client (user) source file, I always get a 0
value which is incorrect.
Also, the information concerning the rule of 3 is very helpful. I
would think that the more detail that is added to a class to offer the
client greater diversity would routinely necessitate the use of copy
constructors, destructors for pointer variables specifically among
other cleanup details, in addition to overloaded assignment,
subtraction, addition, increment, and decrement operator functions
among others. I think it is a course that I will take with most
subsequent programs.
Unfortunately, I find myself again becoming more disappointed in
both the text I am using and the author. The rule of 3 is never
mentioned, nor any of the reasons for this that you have begun to
signify. There are some good and useful things in the text, but I
think that most people in general have a higher standard of
expectation.

anthony
Jul 22 '05 #3

P: n/a

"August1" <an*********@hotmail.com> wrote in message
news:fc*************************@posting.google.co m...
"> No, the problem is that you haven't not defined an assignment
operator for
your class. You use it right after the operator- but the compiler generated on you are using does not do the right thing.

Whenever you have a destructor, you almost always need to provide a copy
constructor AND an assignment operator. This is called the 'rule of three'.
john
Hi John,

Thanks for the response. I added an overloaded assignment operator
to see what would take place, and this did resolve the problem with
program execution. However, it still does not perform as intended.
Specifically, the value derived from subtracting one operand from the
other does not appear to be assigned to the data member within the
overloaded subtraction operator that is assigned to the temporary
class object and returned. When I use the shares.getNumShares()
function at the end of the client (user) source file, I always get a 0
value which is incorrect.


Possibly you did not implement the assignment operator correctly. I notice
that your copy constructor is not implemented correctly.

Stocks::Stocks(const Stocks& sourceStock)//copy constructor definition
{
szStockName = new char[25];
strcpy(szStockName, sourceStock.szStockName);
iNumShares = 0;
dCurrentValue = 0;
dPricePerShare = 0;
};

It should be

Stocks::Stocks(const Stocks& sourceStock)//copy constructor definition
{
szStockName = new char[25];
strcpy(szStockName, sourceStock.szStockName);
iNumShares = sourceStock.iNumShares ;
dCurrentValue = sourceStock.dCurrentValue ;
dPricePerShare = sourceStock.dPricePerShare ;
};

In other words the only thing being copied in the copy constructor was the
stock name, everything else was being set to zero. Maybe you made the same
mistake in the assignment operator.
Also, the information concerning the rule of 3 is very helpful. I
would think that the more detail that is added to a class to offer the
client greater diversity would routinely necessitate the use of copy
constructors, destructors for pointer variables specifically among
other cleanup details, in addition to overloaded assignment,
subtraction, addition, increment, and decrement operator functions
among others. I think it is a course that I will take with most
subsequent programs.
Unfortunately, I find myself again becoming more disappointed in
both the text I am using and the author.
Which book are you using?
The rule of 3 is never
mentioned, nor any of the reasons for this that you have begun to
signify. There are some good and useful things in the text, but I
think that most people in general have a higher standard of
expectation.

anthony


john
Jul 22 '05 #4

P: n/a
">
Possibly you did not implement the assignment operator correctly. I notice
that your copy constructor is not implemented correctly.

Stocks::Stocks(const Stocks& sourceStock)//copy constructor definition
{
szStockName = new char[25];
strcpy(szStockName, sourceStock.szStockName);
iNumShares = 0;
dCurrentValue = 0;
dPricePerShare = 0;
};

It should be

Stocks::Stocks(const Stocks& sourceStock)//copy constructor definition
{
szStockName = new char[25];
strcpy(szStockName, sourceStock.szStockName);
iNumShares = sourceStock.iNumShares ;
dCurrentValue = sourceStock.dCurrentValue ;
dPricePerShare = sourceStock.dPricePerShare ;
};

In other words the only thing being copied in the copy constructor was the
stock name, everything else was being set to zero. Maybe you made the same
mistake in the assignment operator.

Unfortunately, I find myself again becoming more disappointed in
both the text I am using and the author.


Which book are you using?
The rule of 3 is never
mentioned, nor any of the reasons for this that you have begun to
signify. There are some good and useful things in the text, but I
think that most people in general have a higher standard of
expectation.

anthony


john

hi john,

Your observation was correct. The definition of the copy
constructor was the problem. I should have seen that. Sometimes the
eyes are weary. The overloaded assignment operator and overloaded
subtraction operator were declared and defined correctly.

The book is an older book, but still practical. Not something I
would recommend though as being a help. There are too many questions
the user could pose that aren't being answered (The Rule of 3 is a
glaring early omission). Syntactical errors I have observed could also
be confusing to the user depending on their skill level. The title of
the book is "Microsoft Visual C++ 6.0" and it was published in 2001 by
Course Technology in association with Thomson Learning. The author is
Don Gosselin from Napa, Ca. The ISBN # for the book is 0-619-03488-2.

Thanks again,
anthony
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.