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: " << backParkingLot.GetCarCount(); 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 12 Your output Cars counted: 15 Testing carCount assigned 15 Your output Cars counted: 15 Test aborted Exited with return code -6 (SIGABRT). double free or corruption (fasttop)
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 <iostream>
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: " << backParkingLot.GetCarCount();
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;
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images