A
August1
i'm having difficulties with the execution of a program, i believe due
to something i am missing in the copy constructor implementation. When
i create one object of the class and run the program with that object
alone, there are no problems. however, when subsequently using the
copy constructor for a second object and/or a third object created on
the heap memory - i run into an execution problem and assert failure
dialog box (ignore, retry, abort).
the program simulates scores in a football game using an overloaded
unary prefix increment operator and overloaded increment postfix
operator. the prefix opr. increments an int variable by 6 (touchdown
and no PAT), the postfix opr. increments the variable by 7 (touchdown
and PAT). a private static int variable is used to keep track of the
number of touchdowns scored by all teams, i.e. other instances of the
class where the increment operators are used. however, the score
continues to accumulate from the previous objects' score total (note:
this is different from the static TD counter).
i have looked over the code, but this is something i am not familiar
with. can anyone readily identify the problem?
thanks as always,
anthony
//footballgame.h interface file
#if !defined(FOOTBALLGAME_H)
#define FOOTBALLGAME_H
class FootballGame
{
public:
FootballGame(void); //default constructor declaration
//parameterized constructor declaration
FootballGame(char* szName, char* szCity, char* szQB, char* szMascot);
FootBallGame(const FootballGame&); //copy constructor declaration
~FootballGame(void); //destructor declaration
FootballGame& operator = (const FootballGame&);//overloaded assignment
operator void operator++(); //prefix unary operator declaration
void operator++(int);//postfix unary operator declaration
void addFG();
void addSafety();
void add2PointConv();
void setTeamName(char* szName);
char* getTeamName() const;
void setCity(char* szCity);
char* getCity() const;
void setQB(char* szQB);
char* getQB() const;
void setMascot(char* szMascot);
char* getMascot() const;
static int getNumTDs();
int getScore() const;
private:
int iScore;
static int iNumTDs;
char* szTeamName;
char* szCityName;
char* szQBName;
char* szMascotName;
};//end class FootballGame
#endif
//footballgame.cpp implementation file
#include "footballgame.h"
#include <cstring>
int FootballGame::iNumTDs = 0;//intialize global static variable
FootballGame::FootballGame(void)
{//default constructor definition
szTeamName = new char[25];
strcpy(szTeamName,"");
szCityName = new char[25];
strcpy(szCityName,"");
szQBName = new char[25];
strcpy(szQBName,"");
szMascotName = new char[25];
strcpy(szMascotName,"");
iScore = 0;
}
FootballGame::FootballGame(char* szName, char* szCity, char* szQB,
char* szMascot)
{//parameterized constructor definition
szTeamName = new char[25];
strcpy(szTeamName,szName);
szCityName = new char[25];
strcpy(szCityName,szCity);
szQBName = new char[25];
strcpy(szQBName,szQB);
szMascotName = new char[25];
strcpy(szMascotName,szMascot);
iScore = 0;
}
FootballGame::FootBallGame(const FootballGame& sourceTeam)
{//copy constructor definition
szTeamName = new char[25];
strcpy(szTeamName,sourceTeam.szTeamName);
szCityName = new char[25];
strcpy(szCityName,sourceTeam.szCityName);
szQBName = new char[25];
strcpy(szQBName,sourceTeam.szQBName);
szMascotName = new char[25];
strcpy(szMascotName,sourceTeam.szMascotName);
iScore = sourceTeam.iScore;
iScore = 0;
}
FootballGame::~FootballGame(void)
{//destructor deinition
delete[] szTeamName;
delete[] szCityName;
delete[] szQBName;
delete[] szMascotName;
}
FootballGame& FootballGame:perator = (const FootballGame& operand)
{//defines overloaded assignment operator
if(this == &operand)
return *this;
else
{
szTeamName = new char[25];
strcpy(szTeamName,operand.szTeamName);
szCityName = new char[25];
strcpy(szCityName,operand.szCityName);
szQBName = new char[25];
strcpy(szQBName,operand.szQBName);
szMascotName = new char[25];
strcpy(szMascotName,operand.szMascotName);
iScore = operand.iScore;
iScore = 0;
return *this;
}
}
void FootballGame:perator ++()
{//defines prefix overloaded unary increment operator
iScore += 6;
iNumTDs = iNumTDs + 1;
}
void FootballGame:perator ++(int iScores)
{//defines postfix overloaded unary increment operator
iScore += 7;
iNumTDs = iNumTDs + 1;
}
void FootballGame::add2PointConv()
{
iScore += 2;
}
void FootballGame::addFG()
{
iScore += 3;
}
void FootballGame::addSafety()
{
iScore += 2;
}
void FootballGame::setCity(char* szCity)
{
strcpy(szCityName,szCity);
}
char* FootballGame::getCity() const
{
return szCityName;
}
void FootballGame::setMascot(char* szMascot)
{
strcpy(szMascotName,szMascot);
}
char* FootballGame::getMascot() const
{
return szMascotName;
}
void FootballGame::setQB(char* szQB)
{
strcpy(szQBName,szQB);
}
char* FootballGame::getQB() const
{
return szQBName;
}
void FootballGame::setTeamName(char* szName)
{
strcpy(szTeamName,szName);
}
char* FootballGame::getTeamName() const
{
return szTeamName;
}
int FootballGame::getScore() const
{
return iScore;
}
int FootballGame::getNumTDs()
{/*static function to return num of all TDs for all objects
of class where a team scores a TD*/
return iNumTDs;
}
//football.cpp user(client) source file
#include <iostream>
#include "footballgame.h"
using namespace std;
int main(void)
{
FootballGame game("Carolina Panthers", "Charlotte",
"Delhomme", "Sir Purr");
cout << "Team: " << game.getTeamName() << endl;
cout << "Mascot: " << game.getMascot() << endl;
cout << "Quarterback: " << game.getQB() << endl;
cout << "City: " << game.getCity() << endl;
++game; //calls prefix overloaded unary increment operator
cout << game.getScore() << endl;
++game;
cout << game.getScore() << endl;
++game;
game.add2PointConv();
cout << game.getScore() << endl;
game++; //calls postfix overloaded unary increment operator
cout << game.getScore() << endl;
game++;
cout << game.getScore() << endl;
game.addFG();
cout << game.getScore() << endl;
game.addSafety();
cout << game.getScore() << endl;
cout << "Number of touchdowns: " << game.getNumTDs() << endl;
//Problems occur here with use of stack object and then heap object
////////////////////////////////////////////////////////////////////
FootballGame cowboys(game);
cowboys.setTeamName("Dallas Cowboys");cowboys.setQB("Aiken");
cowboys.setCity("Dallas");cowboys.setMascot("Cheerleaders");
cout << cowboys.getTeamName() << endl;
cout << cowboys.getQB() << endl;
cout << cowboys.getCity() << endl;
cout << cowboys.getMascot() << endl;
cowboys++;
cout << cowboys.getScore() << endl;
cowboys++;
cout << cowboys.getScore() << endl;
cowboys.addFG();
cout << cowboys.getScore() << endl;
cout << cowboys.getNumTDs() << endl;
FootballGame* rams = new FootballGame;
rams->setTeamName("L.A. Rams");rams->setQB("Flutie");
rams->setCity("Los Angeles");rams->setMascot("Rambo");
cout << rams->getTeamName() << endl;
cout << rams->getQB() << endl;
cout << rams->getCity() << endl;
cout << rams->getMascot() << endl;
rams++;
rams->addFG();
++rams;
rams->add2PointConv();
cout << rams->getScore() << endl;
cout << FootballGame::getNumTDs() << endl;
delete rams;
//////////////////////////////////////////////////////////////////////
return EXIT_SUCCESS;
}//end main
to something i am missing in the copy constructor implementation. When
i create one object of the class and run the program with that object
alone, there are no problems. however, when subsequently using the
copy constructor for a second object and/or a third object created on
the heap memory - i run into an execution problem and assert failure
dialog box (ignore, retry, abort).
the program simulates scores in a football game using an overloaded
unary prefix increment operator and overloaded increment postfix
operator. the prefix opr. increments an int variable by 6 (touchdown
and no PAT), the postfix opr. increments the variable by 7 (touchdown
and PAT). a private static int variable is used to keep track of the
number of touchdowns scored by all teams, i.e. other instances of the
class where the increment operators are used. however, the score
continues to accumulate from the previous objects' score total (note:
this is different from the static TD counter).
i have looked over the code, but this is something i am not familiar
with. can anyone readily identify the problem?
thanks as always,
anthony
//footballgame.h interface file
#if !defined(FOOTBALLGAME_H)
#define FOOTBALLGAME_H
class FootballGame
{
public:
FootballGame(void); //default constructor declaration
//parameterized constructor declaration
FootballGame(char* szName, char* szCity, char* szQB, char* szMascot);
FootBallGame(const FootballGame&); //copy constructor declaration
~FootballGame(void); //destructor declaration
FootballGame& operator = (const FootballGame&);//overloaded assignment
operator void operator++(); //prefix unary operator declaration
void operator++(int);//postfix unary operator declaration
void addFG();
void addSafety();
void add2PointConv();
void setTeamName(char* szName);
char* getTeamName() const;
void setCity(char* szCity);
char* getCity() const;
void setQB(char* szQB);
char* getQB() const;
void setMascot(char* szMascot);
char* getMascot() const;
static int getNumTDs();
int getScore() const;
private:
int iScore;
static int iNumTDs;
char* szTeamName;
char* szCityName;
char* szQBName;
char* szMascotName;
};//end class FootballGame
#endif
//footballgame.cpp implementation file
#include "footballgame.h"
#include <cstring>
int FootballGame::iNumTDs = 0;//intialize global static variable
FootballGame::FootballGame(void)
{//default constructor definition
szTeamName = new char[25];
strcpy(szTeamName,"");
szCityName = new char[25];
strcpy(szCityName,"");
szQBName = new char[25];
strcpy(szQBName,"");
szMascotName = new char[25];
strcpy(szMascotName,"");
iScore = 0;
}
FootballGame::FootballGame(char* szName, char* szCity, char* szQB,
char* szMascot)
{//parameterized constructor definition
szTeamName = new char[25];
strcpy(szTeamName,szName);
szCityName = new char[25];
strcpy(szCityName,szCity);
szQBName = new char[25];
strcpy(szQBName,szQB);
szMascotName = new char[25];
strcpy(szMascotName,szMascot);
iScore = 0;
}
FootballGame::FootBallGame(const FootballGame& sourceTeam)
{//copy constructor definition
szTeamName = new char[25];
strcpy(szTeamName,sourceTeam.szTeamName);
szCityName = new char[25];
strcpy(szCityName,sourceTeam.szCityName);
szQBName = new char[25];
strcpy(szQBName,sourceTeam.szQBName);
szMascotName = new char[25];
strcpy(szMascotName,sourceTeam.szMascotName);
iScore = sourceTeam.iScore;
iScore = 0;
}
FootballGame::~FootballGame(void)
{//destructor deinition
delete[] szTeamName;
delete[] szCityName;
delete[] szQBName;
delete[] szMascotName;
}
FootballGame& FootballGame:perator = (const FootballGame& operand)
{//defines overloaded assignment operator
if(this == &operand)
return *this;
else
{
szTeamName = new char[25];
strcpy(szTeamName,operand.szTeamName);
szCityName = new char[25];
strcpy(szCityName,operand.szCityName);
szQBName = new char[25];
strcpy(szQBName,operand.szQBName);
szMascotName = new char[25];
strcpy(szMascotName,operand.szMascotName);
iScore = operand.iScore;
iScore = 0;
return *this;
}
}
void FootballGame:perator ++()
{//defines prefix overloaded unary increment operator
iScore += 6;
iNumTDs = iNumTDs + 1;
}
void FootballGame:perator ++(int iScores)
{//defines postfix overloaded unary increment operator
iScore += 7;
iNumTDs = iNumTDs + 1;
}
void FootballGame::add2PointConv()
{
iScore += 2;
}
void FootballGame::addFG()
{
iScore += 3;
}
void FootballGame::addSafety()
{
iScore += 2;
}
void FootballGame::setCity(char* szCity)
{
strcpy(szCityName,szCity);
}
char* FootballGame::getCity() const
{
return szCityName;
}
void FootballGame::setMascot(char* szMascot)
{
strcpy(szMascotName,szMascot);
}
char* FootballGame::getMascot() const
{
return szMascotName;
}
void FootballGame::setQB(char* szQB)
{
strcpy(szQBName,szQB);
}
char* FootballGame::getQB() const
{
return szQBName;
}
void FootballGame::setTeamName(char* szName)
{
strcpy(szTeamName,szName);
}
char* FootballGame::getTeamName() const
{
return szTeamName;
}
int FootballGame::getScore() const
{
return iScore;
}
int FootballGame::getNumTDs()
{/*static function to return num of all TDs for all objects
of class where a team scores a TD*/
return iNumTDs;
}
//football.cpp user(client) source file
#include <iostream>
#include "footballgame.h"
using namespace std;
int main(void)
{
FootballGame game("Carolina Panthers", "Charlotte",
"Delhomme", "Sir Purr");
cout << "Team: " << game.getTeamName() << endl;
cout << "Mascot: " << game.getMascot() << endl;
cout << "Quarterback: " << game.getQB() << endl;
cout << "City: " << game.getCity() << endl;
++game; //calls prefix overloaded unary increment operator
cout << game.getScore() << endl;
++game;
cout << game.getScore() << endl;
++game;
game.add2PointConv();
cout << game.getScore() << endl;
game++; //calls postfix overloaded unary increment operator
cout << game.getScore() << endl;
game++;
cout << game.getScore() << endl;
game.addFG();
cout << game.getScore() << endl;
game.addSafety();
cout << game.getScore() << endl;
cout << "Number of touchdowns: " << game.getNumTDs() << endl;
//Problems occur here with use of stack object and then heap object
////////////////////////////////////////////////////////////////////
FootballGame cowboys(game);
cowboys.setTeamName("Dallas Cowboys");cowboys.setQB("Aiken");
cowboys.setCity("Dallas");cowboys.setMascot("Cheerleaders");
cout << cowboys.getTeamName() << endl;
cout << cowboys.getQB() << endl;
cout << cowboys.getCity() << endl;
cout << cowboys.getMascot() << endl;
cowboys++;
cout << cowboys.getScore() << endl;
cowboys++;
cout << cowboys.getScore() << endl;
cowboys.addFG();
cout << cowboys.getScore() << endl;
cout << cowboys.getNumTDs() << endl;
FootballGame* rams = new FootballGame;
rams->setTeamName("L.A. Rams");rams->setQB("Flutie");
rams->setCity("Los Angeles");rams->setMascot("Rambo");
cout << rams->getTeamName() << endl;
cout << rams->getQB() << endl;
cout << rams->getCity() << endl;
cout << rams->getMascot() << endl;
rams++;
rams->addFG();
++rams;
rams->add2PointConv();
cout << rams->getScore() << endl;
cout << FootballGame::getNumTDs() << endl;
delete rams;
//////////////////////////////////////////////////////////////////////
return EXIT_SUCCESS;
}//end main