#include<iostream>
#include<string>
#include<vector>
usingnamespace std;
//Car class
class Car
{
private:
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public:
//Default constructor
Car()
{
}
//Parameterized constructor
Car(string _reportingMark,int _carNumber,string _kind,bool _loaded,string _destination)
{
reportingMark = _reportingMark;
carNumber = _carNumber;
kind = _kind;
loaded = _loaded;
destination = _destination;
}
//Destructor
~Car()
{
}
//Copy constructor
Car( const Car &other)
{
reportingMark = other.reportingMark;
carNumber = other.carNumber;
kind = other.kind;
loaded = other.loaded;
destination = other.destination;
}
Car operator=(Car other)
{
reportingMark = other.reportingMark;
carNumber = other.carNumber;
kind = other.kind;
loaded = other.loaded;
destination = other.destination;
return *this;
}
friendbooloperator==(Car c1,Car c2);
void setUp()
{
cout<<"Implementing Progress\n";
}
void output()
{
cout<<"Reporting Mark: "<< reportingMark<<endl;
cout<<"Card Number: "<< carNumber<<endl;
cout<<"Kind: "<< kind<<endl;
cout<<"Loaded: "<< loaded<<endl;
cout<<"Destination: "<< destination<<endl;
}
};
//Friend function
booloperator==(Car c1,Car c2)
{
if(c1.reportingMark==c2.reportingMark
&& c1.carNumber == c2.carNumber
&& c1.kind == c2.kind
&& c1.loaded == c2.loaded
&& c1.destination == c2.destination)
{
returntrue;
}
else
{
returnfalse;
}
}
/* ********** StringOfCars member functions ********** */
class StringOfCar
{
private:
Car *cars; //pointer of cars
int carCount; //defined additional data member carCount
public:
//Default constructor
StringOfCar()
{
cars=NULL; //set initially NULL and 0
carCount=0;
}
//Parameterized constructor
StringOfCar(Car *_cars)
{
cars = _cars;
carCount=0;
}
//Copy construtor
StringOfCar( const StringOfCar &obj)
{
cars = obj.cars;
carCount=0;
}
//Destructor
~StringOfCar()
{
//deleting each car allocated
for(int i=0;i<carCount;i++)
delete cars;
}
int getCount() //UFD here defined here
{return carCount;}
void output()
{
for(int i=0; i<carCount; i++)
{
cars[i].output();
}
}
void push(Car car)
{
cars[carCount++]=car;
}
Car pop()
{
Car lastObj = cars[--carCount]; //getting top of stack of cars
return lastObj;
}
void input()
{
cout<<"In progress!\n";
}
};
int main()
{
/* ********Create a Car object named car1 with the following constants as initial values:****/
Car car1("SP",34567 ,"business",true,"Salt Lake City ");
cout<<"---------- TEST1 car1 object content--------------\n";
car1.output();
/* ******** Create a Car object named car2 using the default constructor. */
Car car2;
//Use the = operator to copy the data from car1 to car 2.
car2 = car1;
cout<<"\n----------car2 object content--------------\n";
//Print car2
car2.output();
//Create a default StringOfCars object named string1.
StringOfCar string1;
string1.push(car1);
//Print: STRING 1
cout<<"\n---------- TEST2 string1 object content--------------\n";
string1.output();
//Create a car named car3.
Car car3;
car3 = string1.pop();
//Print car3.
cout<<"\n----------TEST3 car3 object content--------------\n";
car3.output();
//Then print the contents of string1 again
cout<<"\n----------string1 object content--------------\n";
string1.output();
return 0;
}
When I try to run it in codeblocks it pauses as if the application had an error, I think it has something to do with push but I'm not exactly sure. Any ideas?
Your program crashes on line 175 -> 141 -> cars is NULL. Nonetheless you are trying to assign a car. You need to do more in the push(...) function. Better use a std::vector.