A
August1
This is a short program that I have written from a text that
demonstrates a class object variable created on the stack memory and
another class object variable created on the heap memory. By way of
the text, the program is supposed to demonstrate the use of a copy
constructor function for the object created on the heap (because there
is a char* pointer variable in the instantiated class) and how you
would use the delete keyword with the object and to also use a
destructor for the char* pointer variable which is also placed on the
heap memory when instantiated - this being becuase the heap memory
will not be freed when the main function goes out of scope, as with
the class object variable declared on the stack memory does by default
constructor.
The program is exactly as demonstrated in the text - WHY the problem?
Thank you in advance.
//interface file Stocks.h
#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();
private:
//declare private data members
char* szStockName;//declares character array pointer
int iNumShares;
double dCurrentValue;
double dPricePerShare;
};
#endif
//end class Stocks.h
///////////////////////////////////////////
//implementation file Stocks.cpp
#include "Stocks.h"
#include <string.h>
#include <ostream.h>
Stocks::Stocks()//defines default constructor
{
char* szStockName = new char[25];
strcpy(szStockName,"");
iNumShares = 0;
dCurrentValue = 0;
dPricePerShare = 0;
};
Stocks::Stocks(char* szName)//defines parameterized constructor
{
char* 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);
};
Stocks::~Stocks()//destructor definition
{
delete[] szStockName;
cout << "Destructor called." << endl;
};
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;
}
//end Stocks.cpp
//////////////////////////////////
//client source file used to instantiate custom class objects
//Shares.cpp
#include "Stocks.h"
#include <iostream>
#include <string>
#include <iomanip>//for use of setprecision() and setiosflags()
using namespace std;
void 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");
//heap memory object that passes literal string to parameterized
constructor
Stocks* stockPick2 = new Stocks("Lucent");
/*use indirection operator associated with heap memory object to
access
class method and pass literal numeric value*/
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;
delete stockPick2;//manually delete the object from the heap memory
}//end main
demonstrates a class object variable created on the stack memory and
another class object variable created on the heap memory. By way of
the text, the program is supposed to demonstrate the use of a copy
constructor function for the object created on the heap (because there
is a char* pointer variable in the instantiated class) and how you
would use the delete keyword with the object and to also use a
destructor for the char* pointer variable which is also placed on the
heap memory when instantiated - this being becuase the heap memory
will not be freed when the main function goes out of scope, as with
the class object variable declared on the stack memory does by default
constructor.
The program is exactly as demonstrated in the text - WHY the problem?
Thank you in advance.
//interface file Stocks.h
#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();
private:
//declare private data members
char* szStockName;//declares character array pointer
int iNumShares;
double dCurrentValue;
double dPricePerShare;
};
#endif
//end class Stocks.h
///////////////////////////////////////////
//implementation file Stocks.cpp
#include "Stocks.h"
#include <string.h>
#include <ostream.h>
Stocks::Stocks()//defines default constructor
{
char* szStockName = new char[25];
strcpy(szStockName,"");
iNumShares = 0;
dCurrentValue = 0;
dPricePerShare = 0;
};
Stocks::Stocks(char* szName)//defines parameterized constructor
{
char* 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);
};
Stocks::~Stocks()//destructor definition
{
delete[] szStockName;
cout << "Destructor called." << endl;
};
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;
}
//end Stocks.cpp
//////////////////////////////////
//client source file used to instantiate custom class objects
//Shares.cpp
#include "Stocks.h"
#include <iostream>
#include <string>
#include <iomanip>//for use of setprecision() and setiosflags()
using namespace std;
void 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");
//heap memory object that passes literal string to parameterized
constructor
Stocks* stockPick2 = new Stocks("Lucent");
/*use indirection operator associated with heap memory object to
access
class method and pass literal numeric value*/
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;
delete stockPick2;//manually delete the object from the heap memory
}//end main