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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...
Back
Top