C/C++ Copy Assignment Operator for CarCounter Class

AI Thread Summary
The discussion focuses on implementing a copy assignment operator for the CarCounter class in C++. The operator should assign the carCount from one object to another and return the current object. The initial code provided contains several errors, including a missing closing brace and incorrect variable references. The correct implementation checks for self-assignment and directly assigns the carCount from the source object to the destination object. There is a consensus that the assignment operator should typically return a const reference to the object, although opinions vary on this point. The main goal is to ensure that the assignment operator effectively makes one CarCounter object an exact copy of another.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top