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 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
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...
Back
Top