Copy Assignment Operator for CarCounter Class

Click For Summary
SUMMARY

The forum discussion focuses on implementing a copy assignment operator for the CarCounter class in C++. The correct implementation assigns the carCount from the source object to the target object and returns a reference to the target object. The provided code contains errors, including a missing closing brace and incorrect variable names. The final correct implementation is: const CarCounter& CarCounter::operator=(const CarCounter& objToCopy) { if (this != &objToCopy) { carCount = objToCopy.GetCarCount(); } return *this; }

PREREQUISITES
  • Understanding of C++ class structures
  • Familiarity with operator overloading in C++
  • Knowledge of memory management in C++
  • Basic understanding of object copying and assignment semantics
NEXT STEPS
  • Study C++ operator overloading techniques
  • Learn about copy constructors and their relationship to assignment operators
  • Explore C++ memory management practices, including dynamic memory allocation
  • Investigate best practices for implementing assignment operators in C++
USEFUL FOR

C++ developers, computer science students, and software engineers looking to deepen their understanding of operator overloading and memory management in C++.

kstewa17
Messages
1
Reaction score
0
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

Code:
#include <iostream>
using namespace std;

class CarCounter {
   public:
      CarCounter();
      CarCounter& operator=(const CarCounter& objToCopy);
      void SetCarCount(const int setVal) {
         carCount = setVal;
      }
      int GetCarCount() const {
         return carCount;
      }
   private:
      int carCount;
};

CarCounter::CarCounter() {
   carCount = 0;
   return;
}

// FIXME write copy assignment operator

/* Your solution goes here  */

[B]CarCounter& CarCounter::operator=(const CarCounter& objToCopy) {
  
   
   if (this != &objToCopy) {           
      delete setVal;                  
      setVal = new int;               
      *setVal = *(objToCopy.dataObj); 
   }
   
   return *this;[/B]

int main() {
   CarCounter frontParkingLot;
   CarCounter backParkingLot;

   frontParkingLot.SetCarCount(12);
   backParkingLot = frontParkingLot;

   cout << "Cars counted: " << backParkingLot.GetCarCount();

   return 0;
}
 
Technology news on Phys.org
Re: C/C++ (Copy Assignment)...What I have so far is is red, and it's confusing me a little.

Code:
CarCounter& CarCounter::operator=(const CarCounter& objToCopy) {
   if (this != &objToCopy) {
      delete setVal;
      setVal = new int;
      *setVal = *(objToCopy.dataObj);
   }
   return *this;
}

First, this post belongs in the computer science section. Your code is missing
a closing } so it can't compile. Furthermore, there is no field of CarCounter
that is named setval. Again compiler error. Apparently, you tried to mimic a
correct assignment operator for a different class. Your main setup is correct
but the details are all wrong.

An assignment operator = is meant to assign an exact copy of ObjToCopy to
the object on the left side of the =. So, for example, if you have two objects
car1 and car2 of class CarCounter, car1=car2 should have the effect of making
car1 an exact copy of car2.

Here's a correct assignment operator:

Code:
const CarCounter& CarCounter::operator=(const CarCounter& objToCopy) {
   if (this != &objToCopy) {
      carCount = objToCopy.getCarCount();
   }
   return *this;
  }
}

"Most" people want the assignment operator to return a const reference to
an object of the class, but there is some disagreement about this.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
53
Views
5K
Replies
10
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K