473,379 Members | 1,337 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,379 software developers and data experts.

Overloaded subtraction operator - memory problem

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

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

"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
">
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: -Steve- | last post by:
Okay I have a bunch of code below. Hope it comes across readable. The problem I'm having is that in the lines under main(): cout << a << endl; Is going into the code for IntArray(const...
2
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown at the bottom and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically...
2
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
13
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one...
9
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded...
3
by: sam.barker0 | last post by:
Hi, I have designed a class with an overloaded = operator. The problem is that whenever I invoke the method like const myclass<charastring=another_object;
3
by: jr.freester | last post by:
I have created to classes Matrix and System. System is made up of type matrix. ---------------------------------------------------------------------------------- class Matrix { private: int...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.