C/C++ How to Write a Copy Assignment Operator for CarCounter in C++?

  • Thread starter Thread starter EvanET
  • Start date Start date
  • Tags Tags
    Writing
AI Thread Summary
The discussion revolves around implementing a copy assignment operator for the CarCounter class in C++. The operator should assign the carCount from one CarCounter object to another and return the current object. The initial code includes a constructor and methods to set and get the car count, but the copy assignment operator is not yet implemented. A user expresses difficulty in solving the code and requests assistance. The forum emphasizes the importance of sharing progress or attempts to facilitate effective help from others, encouraging users to provide context for their questions.
EvanET
Messages
10
Reaction score
0
I am given a prompt to: Write a copy assignment operator for CarCounter that assigns objToCopy.carCount to the new objects's carCount, then returns *this.

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  */

int main() {
   CarCounter frontParkingLot;
   CarCounter backParkingLot;

   frontParkingLot.SetCarCount(12);
   backParkingLot = frontParkingLot;

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

   return 0;
}
I cannot solve the code for it, I am stuck.
 
Technology news on Phys.org
Hi EvanET! :D

We ask that our users show their progress (work thus far or thoughts on how to begin) when posting questions. This way our helpers can see where you are stuck or may be going astray and will be able to post the best help possible without potentially making a suggestion which you have already tried, which would waste your time and that of the helper.

Can you post what you have done so far?
 
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...

Similar threads

Back
Top