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?
 
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...

Similar threads

Back
Top