Thread: Destructor - Execution problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    15

    Destructor - Execution problem

    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
    Last edited by Salem; 09-25-2004 at 12:01 AM. Reason: tagged

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM