1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Blababa [14]
3 years ago
7

Write a copy assignment operator for CarCounter that assigns objToCopy.carCount to the new objects's carCount, then returns *thi

s. Sample output for the given program:

Computers and Technology
1 answer:
ollegr [7]3 years ago
3 0

Answer:

Here is the copy assignment operator for CarCounter:

CarCounter& CarCounter::operator=(const CarCounter& objToCopy) {

carCount = objToCopy.carCount;

return *this;  }

The syntax for copy assignment operator is:

ClassName& ClassName :: operator= ( ClassName& object_name)

In the above chunk of code class name Is CarCounter and object name is objToCopy.

Assignment operator = is called which assigns objToCopy.carCount to the new objects's carCount.

This operator basically is called when an object which is already initialized is assigned a new value from another current object.

Then return *this returns the reference to the calling object

Explanation:

The complete program is:

#include <iostream>  //to use input output functions

using namespace std;   //to access objects like cin cout

class CarCounter {  //class name

public:  // public member functions of class CarCounter

CarCounter();  //constructor of CarCounter

CarCounter& operator=(const CarCounter& objToCopy);  //copy assignment operator for CarCounter

void SetCarCount(const int setVal) {  //mutator method to set the car count value

carCount = setVal;  } //set carCount so setVal

int GetCarCount() const {  //accessor method to get carCount

return carCount;  }  

private:  //private data member of class

int carCount;  };    // private data field of CarCounter

CarCounter::CarCounter() {  //constructor

carCount = 0;  //intializes the value of carCount in constructor

return; }  

// FIXME write copy assignment operator  

CarCounter& CarCounter::operator=(const CarCounter& objToCopy){

/* copy assignment operator for CarCounter that assigns objToCopy.carCount to the new objects's carCount, then returns *this */

carCount = objToCopy.carCount;

return *this;}  

int main() {  //start of main() function

CarCounter frontParkingLot;  // creates CarCounter object

CarCounter backParkingLot;   // creates CarCounter object

frontParkingLot.SetCarCount(12);  // calls SetCarCount method using object frontParkingLot to set the value of carCount to 12

backParkingLot = frontParkingLot;  //assigns value of frontParkingLot to backParkingLot

cout << "Cars counted: " << backParkingLot.GetCarCount();  //calls accessor GetCarCount method to get the value of carCount and display it in output

return 0;  }

The output of this program is:

Cars counted = 12

You might be interested in
You are asked to write a program that prompts the user for the size of two integer arrays, user input for the size must not exce
Zielflug [23.3K]

Answer:

In Java:

import java.util.*;

public class MyClass{

public static void main(String[] args) {

 Scanner input = new Scanner(System.in);

 System.out.print("Length of array 1: ");

 int lent1 = input.nextInt();

 while(lent1 <= 0 || lent1 > 50){

     System.out.print("Length of array 1: ");

     lent1 = input.nextInt();

 }

 int[] array1 = new int[lent1];

 System.out.print("Length of array 2: ");

 int lent2 = input.nextInt();

 while(lent2 <= 0 || lent2 > 50){

     System.out.print("Length of array 2: ");

     lent2 = input.nextInt();

 }

 int[] array2 = new int[lent2];

 for(int i =0;i<lent1;i++){

     array1[i] = (int)(Math.random() * (lent1*2) + 1);

 }

 for(int i =0;i<lent2;i++){

     array2[i] = (int)(Math.random() * (lent2*2) + 1);

 }

 System.out.print("Array 1: ");

 for(int i =0;i<lent1;i++){

     System.out.print(array1[i]+" ");

 }

 System.out.println("Array 2: ");

 for(int i =0;i<lent2;i++){

     System.out.print(array2[i]+" ");

 }

}

}

Explanation:

This prompts the user for length of the first array

 System.out.print("Length of array 1: ");

This declares and gets input for the length of the first array

 int lent1 = input.nextInt();

This validates the length of first array

<em>  while(lent1 <= 0 || lent1 > 50){</em>

<em>      System.out.print("Length of array 1: ");</em>

<em>      lent1 = input.nextInt();  }</em>

This declares the first array

 int[] array1 = new int[lent1];

This prompts the user for length of the second array

 System.out.print("Length of array 2: ");

This declares and gets input for the length of the second array

 int lent2 = input.nextInt();

This validates the length of the second array

<em>  while(lent2 <= 0 || lent2 > 50){</em>

<em>      System.out.print("Length of array 2: ");</em>

<em>      lent2 = input.nextInt();  }</em>

This declares the second array

 int[] array2 = new int[lent2];

The following generates random integers between 1 and lent1*2 to array 1

<em>  for(int i =0;i<lent1;i++){</em>

<em>      array1[i] = (int)(Math.random() * (lent1*2) + 1);  }</em>

The following generates random integers between 1 and lent2*2 to array 2

<em>  for(int i =0;i<lent2;i++){</em>

<em>      array2[i] = (int)(Math.random() * (lent2*2) + 1);  }</em>

This prints the header Array 1

 System.out.print("Array 1: ");

The following iteration prints the content of the first array

<em> for(int i =0;i<lent1;i++){</em>

<em>      System.out.print(array1[i]+" ");</em>

<em>  }</em>

This prints the header Array 2

 System.out.println("Array 2: ");

The following iteration prints the content of the second array

<em>  for(int i =0;i<lent2;i++){</em>

<em>      System.out.print(array2[i]+" ");</em>

<em>  }</em>

<em />

4 0
2 years ago
How can people make sure they are using credit cards responsibly
Ray Of Light [21]
Set a buget based on monthly income
8 0
2 years ago
Read 2 more answers
Why is it a mistake to put email addresses of people who don't know each other in the "To:" field?
Alex Ar [27]
So they can know who it is going to . You cannot send an email without knowing who it goes to :)
3 0
3 years ago
Read 2 more answers
Select the correct answers
Mashutka [201]

Answer:

Use a mixture of External and Internal Links....

Explanation:

8 0
3 years ago
What is being measured using kbps, mbps, and gbps?
spayn [35]
Transfer rate.
/////////////////////

8 0
3 years ago
Other questions:
  • Write a program that loops one thousand times. add all the even numbers and display the results. add all the odd numbers and dis
    13·1 answer
  • Checking the ___________ will reduce the possibility of having to rebuild or replace the engine.
    9·2 answers
  • 18. Using the same formatting elements and designs across slides in a presentation is important to develop?
    5·1 answer
  • Two samples of dirt are collected from a suspect's tread in his shoe and a crime scene. The forensic investigator does a gross e
    6·2 answers
  • 2. Integer plot function (find a smart way to code big integers) Write a program BigInt(n) that displays an arbitrary positive i
    8·1 answer
  • Internet is for everyone but it wont be if its users cannot protect their privacy and the confidentiality of transactions conduc
    6·1 answer
  • Match the item to the type.
    11·1 answer
  • Complete the sentence.
    8·1 answer
  • When you pass a double variable to a method, the method receives ______.a) a copy of the memory addressb) the reference of the v
    12·1 answer
  • A ____________ is a collection of commands given a name.
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!