here is my class file:

#ifndef BANGER_H
#define BANGER_H

class Banger
{
private:

	static int objectCount;
	char* driver;
	char* car;
	int 	hits;
	bool  mobile;

public:

	Banger();
	
	Banger(char*,char*);

	Banger(const Banger &obj);
	  
	void setDriver( char* );
	
	void setCar( char* );
	
	void setHits( int);
	
	void setMobile( bool);
	
	char *getDriver();
	
	char *getCar();

	int getHits();

	bool getMobile();

	static int getObjectCount();

};

int Banger::objectCount=0;

#endif

these are my .cpp file:

#include "Banger.h"
#include<iostream>
#include<cstring>

using namespace std;



Banger::Banger()
{
	objectCount++;
}


Banger::Banger(char *name, char *body)
{
	this->driver= new char[strlen(name)+1];
	strcpy(driver,name);

	this->car= new char[strlen(body)+1];
	strcpy(car,body);
	objectCount++;	
}


Banger::Banger(const Banger &obj)
{
	driver= new char[strlen(obj.driver)+1];
	strcpy(driver,obj.driver);

	this->car= new char[strlen(obj.car)+1];
	strcpy(car,obj.car);
}

void Banger::setDriver(char *name)
{
	delete [] driver;
	driver= new char[strlen(name)+1];
	strcpy(driver,name);
}

void Banger::setCar(char *body)
{
	delete [] car;
	car= new char[strlen(body)+1];
	strcpy(car,body);
}

void Banger::setHits(int num)
{
	hits=num;
}

void Banger::setMobile(bool b)
{
	if(b==true)
	{
		mobile= true;
	}
	else
		mobile=false;
}

char* Banger:: getDriver()
{
	return driver;
}

char* Banger::getCar()
{
	return car;
}

 int Banger::getHits()
{
	return hits;
}

 bool Banger::getMobile()
{
	return mobile;

}

 int Banger::getObjectCount()
 {
	 return objectCount;
 }
#include<iostream>
#include<cstring>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include "Banger.h"

using namespace std;

const int MAX_CARS = 5;
const int MAX_LENGTH = 30;
const int STOP = 6;

int main()
{
	Banger* car[MAX_CARS];
	ifstream inFile("competitors.txt");
	
	//Making sure the file is open
	if(!inFile)
	{
		cout << "Error opening file" << endl;
		return EXIT_FAILURE;
	}

	cout << "The CSE 1384 Demolition Derby is now open for registration.\nAre there any takers?" << endl;
	//Testing default constructor and setters, creating first car (index = 0)
	car[0] = new Banger;
	car[0]->setDriver("Max Champ");
	car[0]->setCar("truck");
	car[0]->setHits(0);
	car[0]->setMobile(true);

	cout << "There is now " << Banger::getObjectCount() << " car in the derby." << endl;
	

	//Testing the parameter constructor, creating the rest of the cars
	for(int i = 1; i < MAX_CARS; i++)
	{
		char name[MAX_LENGTH], body[MAX_LENGTH];
		inFile.getline(name, MAX_LENGTH);
		inFile.getline(body, MAX_LENGTH);
		car[i] = new Banger(name, body);
		//Testing the static object count
		cout << "There are now " << Banger::getObjectCount() << " cars in the derby." << endl;
	}

	//Testing getDriver() and getCar()
	cout << "Registation is now closed. Here's who we have in our race:" << endl << endl;
	for(int i = 0; i < MAX_CARS; i++)
		cout << car[i]->getDriver() << "\t" << car[i]->getCar() << endl;	 

	//Testing Object Count
	cout << "And now it's time for the race!" << endl << endl;
	cout << endl << "Welcome ladies and gentlemen to the first ever CSE 1384 Demolition Derby.\nWe have ";
	cout << Banger::getObjectCount() << " cars in the arena to rock your world with all of the" << endl;
	cout << "smashing and crashing that you could ever hope for.  It promises to be an \nexciting night." 
			<< endl << endl;

	//Testing getters
	cout << "Our returning champion, " << car[0]->getDriver() << ", has come tonight with his newest ";
	cout << car[0]->getCar() << ".\nThe race hasn't started yet so he has " << car[0]->getHits() << " hits ";
	cout << "so far,";
	if(car[0]->getMobile())
		cout << " but is he ever mobile.\nYee-haa!  Look at him move.  Yessiree, it should be a fine show." 
			<<endl;
	


/*Start of commented out section
	
	cout << endl << "Let's start this game."
		<< endl << endl << "BANG!" << endl << endl;

	srand(time(0));

//The derby -- smashing and crashing.  Continues as long as there are more than one banger
	while(Banger::getObjectCount() > 1)
	{
		if(Banger::getObjectCount() < 5)
			cout << "We're down to ";
		else
			cout << "We still have ";
		cout << Banger::getObjectCount() << " cars in the race." << endl;

		int basher = rand()% MAX_CARS;
		//Make sure that the attacking car is still in the game
		while(car[basher] == 0)
			basher = rand()% MAX_CARS;

		int bashed = rand()% MAX_CARS;

		//Make sure that the car being attacked is a different one from the attacker
		//and that the car is still in the game
		while(basher == bashed || car[bashed] == 0)
		{
			bashed = rand() % MAX_CARS;
		}

		cout << car[basher]->getDriver() << " just hit " << car[bashed]->getDriver() <<
			" and...."<< endl;
		
		int hitHard = rand()%10;

		//if hit hard enough to immobilize the car
		if(hitHard > STOP)
		{
			(*car[basher])++;	//Testing overloaded postfix increment operator
			cout << "Yes!  It looks like he can't get his ol' " << car[bashed]->getCar() << " movin'."
				<< endl << "He's out of the game!" << endl;
			delete car[bashed]; //Testing destructor
			car[bashed] = 0;
		}
		else
		{
			++(*car[basher]);	//Testing overloaded prefix increment operator
			cout << "But he's still moving -- so he's still in the game." << endl;
		}
		cout << endl << endl;
	}
	
	cout << "The crowd is going WILD!  There's only one car left now. " << endl;

	//Testing Copy Constructor
	int i;
	for(i = 0; i < MAX_CARS; i++)
		if(car[i] != 0)
			break;
	Banger champion = *(car[i]);

	cout << "So with " << champion.getHits() << " hits in this game, our new champion is...." 
		<< "drum-roll please...." << endl << champion.getDriver() << "!!!" << endl <<
		endl << "Everybody be sure to say a big CONGRATULATIONS to "; 

	//Testing overloaded assignment
	Banger nextReturningChamp;
	nextReturningChamp = champion;

	cout << nextReturningChamp.getDriver() << " -- our champion" << endl;
	cout << "for the next year when we get together and do the whole thing over again." << endl;
	cout << "Until then, keep on crashin' and a-smashin'!" << endl << endl;
*/
	return EXIT_SUCCESS;
}

this is the error i got i dnt have a clue what it means?

1>Linking...
1>LINK : C:\Users\Ankit Arya\Documents\Visual Studio 2008\Projects\lab3\Debug\lab3.exe not found or not built by the last incremental link; performing full link
1>lab3.obj : error LNK2005: "private: static int Banger::objectCount" (?objectCount@Banger@@0HA) already defined in Banger.obj
1>C:\Users\Ankit Arya\Documents\Visual Studio 2008\Projects\lab3\Debug\lab3.exe : fatal error LNK1169: one or more multiply defined symbols found

and ya here is the txt file

Rarin Togo
sedan
Slugger Hard
station wagon
Rolin Over
volkswagon
Mini Van
station wagon
Max Military
jeep

Recommended Answers

All 5 Replies

my .txt file again:

Rarin Togo

sedan

Slugger Hard

station wagon

Rolin Over

volkswagon

Mini Van

station wagon

Max Military

jeep

You have to move the int Banger::objectCount=0; to the the banger.cpp file.

thanks alot it worked!!!!

now i am working on the commented part of my .cpp file


i am not able to figure out when i write definition for my increment operator ++.....how it would change car[bashed]???

i am not able to figure out when i write definition for my increment operator ++.....how it would change car[bashed]???

Sorry but I don't get what you are saying. Could you describe it in more detail?

Dont worry i managed it thanks neways!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.