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