Overloaded subtraction operator - memory problem

A

August1

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
 
J

John Harrison

August1 said:
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
 
A

August1

"> 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
 
J

John Harrison

August1 said:
"> No, the problem is that you haven't not defined an assignment
operator for

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
 
A

August1

">
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
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top