Write a copy assignment operator for CarCounter that assigns objToCopy.carCount to the new objects's carCount, then returns *this. Sample output for the given program:Cars counted: 12
#include using namespace std;class CarCounter { public: CarCounter(); ~CarCounter(); CarCounter& operator=(const CarCounter& objToCopy); void SetCarCount(const int setVal) { *carCount = setVal; } int GetCarCount() const { return *carCount; } private: int* carCount;};CarCounter::CarCounter() { carCount = new int; *carCount = 0;}CarCounter::~CarCounter() { delete carCount;}// FIXME write copy assignment operator/* Your solution goes here */int main() { CarCounter frontParkingLot; CarCounter backParkingLot; int count; cin >> count; frontParkingLot.SetCarCount(count); backParkingLot = frontParkingLot; cout < "cars="" counted:="" "=""><> return 0;}error:(i tried to type this in but gets an error from down below)CarCounter& CarCounter::operator=(const CarCounter& objToCopy) { carCount = objToCopy.carCount; return *this;}Testing carCount assigned 12Your outputCars counted: 15Testing carCount assigned 15Your outputCars counted: 15Test abortedExited with return code -6 (SIGABRT).double free or corruption (fasttop)
class CarCounter { public: CarCounter(); ~CarCounter(); CarCounter& operator=(const CarCounter& objToCopy); void SetCarCount(const int setVal) { *carCount = setVal; } int GetCarCount() const { return *carCount; } private: int* carCount;};
CarCounter::CarCounter() { carCount = new int; *carCount = 0;}
CarCounter::~CarCounter() { delete carCount;}
// FIXME write copy assignment operator
/* Your solution goes here */
int main() { CarCounter frontParkingLot; CarCounter backParkingLot; int count;
cin >> count;
frontParkingLot.SetCarCount(count); backParkingLot = frontParkingLot;
cout < "cars="" counted:="" "=""><>
return 0;}
error:
(i tried to type this in but gets an error from down below)
CarCounter& CarCounter::operator=(const CarCounter& objToCopy) { carCount = objToCopy.carCount; return *this;}
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here