I'm writing a program that has execution problems when using a destructor. I have provided a default constructor, parameterized constructor, copy constructor, and overloaded assignment operator. The destructor is used because of variables on the heap memory.
I've looked over everything a number of times for syntactical errors, but this is something i'm not familiar with, or am overlooking. Could someone more familiar with C++ point out to me what is readily apparent to them?
Thanks,
anthony
Code://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 ~FootballGame(void);//destructor declaration FootballGame& operator = (const FootballGame&); friend FootballGame operator ++ (const FootballGame&); 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 getScore(); private: static int iScore; char* szTeamName; char* szCityName; char* szQBName; char* szMascotName; };//end class FootballGame #endif /////////////////////////////////////////// //footballgame.cpp implementation file #include "footballgame.h" #include <cstring> #include <ostream.h> int FootballGame::iScore = 0; 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,""); } 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); } 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; } FootballGame::~FootballGame(void) {//destructor definition delete[] szTeamName; delete[] szCityName; delete[] szQBName; delete[] szMascotName; } FootballGame& FootballGame:: operator = (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; return *this; } } FootballGame operator ++ (const FootballGame& operand) {//defines overloaded increment operator FootballGame game; game.szCityName = new char[25]; strcpy(game.szCityName,operand.szCityName); game.szMascotName = new char[25]; strcpy(game.szMascotName, operand.szMascotName); game.szQBName = new char[25]; strcpy(game.szQBName, operand.szQBName); game.szTeamName = new char[25]; strcpy(game.szTeamName, operand.szTeamName); game.iScore = operand.iScore + 6; return game; } 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() { return iScore; } ////////////////////////////////////// //user.cpp client source file #include <iostream> #include <cstring> #include "footballgame.h" using namespace std; int main(void) { FootballGame game("Green Bay Packers", "Green Bay", "Favre", "Cheese Heads"); cout << "Team: " << game.getTeamName() << endl; cout << "Mascot: " << game.getMascot() << endl; cout << "Quarterback: " << game.getQB() << endl; cout << "City: " << game.getCity() << endl; game = ++game; game = ++game; cout << game.getScore() << endl; game = ++game; cout << game.getScore() << endl; game = ++game; cout << game.getScore() << endl; return EXIT_SUCCESS; }//end main



LinkBack URL
About LinkBacks


