Question

In: Computer Science

Please code by C++ The required week2.cpp file code is below Please ask if you have...

Please code by C++
The required week2.cpp file code is below
Please ask if you have any questions

instruction:

1. Implement the assignment operator: operator=(Vehicle &)
This should make the numWheels and numDoors equal to those of the object that are being passed in. It is similar to a copy constructor, only now you are not initializing memory. Don’t forget that the return type of the function should be Vehicle& so that you can write something like:
veh1 = veh2 = veh3 = veh4;
This makes veh3 = veh4, but then returns veh4 so that it is used for the next assignment: veh2 = veh4;
Also, veh4 shouldn’t change, so make the Vehicle parameter to be a const object.
2. Implement the comparison operator: operator==(const Vehicle &)
This function should return true if the numDoors and numWheels are both equal to the parameter’s variables.
3. Implement the inequality operator: operator!=(const Vehicle&). This should just return the negation of the comparison operator of step #3.
4. Implement the prefix and postfix increment and decrement operators:
a. operator++()
b. operator++(int i)
c. operator--( )
d. operator--(int i)
For a Vehicle, you should implement increment so that all of the variables are incremented by 1. So Vehicle(2, 2) would be incremented to: Vehicle(3, 3)
Don’t forget that postfix increment returns a copy of the original values before incrementing. So
Vehicle veh(4, 2); veh++; would make veh = (5, 3), but return Vehicle(4, 2)

5. Implement the output operator: ostream& operator<<( ostream&, const Vehicle &v) so that it outputs the variables to the output stream that is passed in. It should work like the printVehicle() that you have already implemented.
You should declare the output operator as a friend of the Vehicle class and directly access the private variables of the Vehicle class when writing them to the stream.
6. Change your main function so that it looks like this:
int main(int argc, char **argv)
{
Vehicle original;
Vehicle copy(original); // copy constructor by reference
cout << "Original is: " << original << " copy is: " << copy << endl;
  
cout << "Increment original: " << original++ << endl;
cout << "Increment copy:" << ++copy<< endl;
cout << "Decrement original:" << --original << endl;
cout << "Decrement copy:" << copy-- << endl;
// should be true :
cout << "Compare equality 1: " << (original == copy) << endl;
//should be false:
cout << "Compare equality 2: " << (--original == ++copy) << endl;
//should be true:
cout << "Compare inequality: " << (original != copy) << endl;
//This should make original = copy, and then return a Vehicle for output:
cout << "Assignment operator: " << (original = copy) << endl;
return 0;
}

reference code:
.cpp file:
#include <iostream>

using namespace std;

class Fraction {
int numerator, denominator;

public:
Fraction(int n, int d) {
setNumerator(n);
setDenominator(d);
}

void setNumerator(int n){ numerator = n; }
int getNumerator(void) { return numerator; }

void setDenominator(int d) { denominator = d; }
int getDenominator(void) { return denominator; }

float asFloat(void) {
return (float)getNumerator() / getDenominator();
}
};

void printFraction(Fraction f)
{
cout << "Fraction at address:" << &f
<< " Numerator:" << f.getNumerator()
<< " Denominator:" << f.getDenominator() << endl;
}

int main()
{
Fraction f(5, 1);
printFraction(f);

return 0;
}

Solutions

Expert Solution

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <iostream>

using namespace std;

class Vehicle{
  
     int numWheels;
     int numDoors;
   
     public:
   
    Vehicle(int numWheels=0,int numDoors=0)
    {
        this->numWheels=numWheels;
        this->numDoors=numDoors;
    }
  
    Vehicle(const Vehicle& v)
    {
        this->numWheels=v.numWheels;
        this->numDoors=v.numDoors;
      
    }
  
    //1
    Vehicle operator=(const Vehicle& v)
    {
        this->numWheels=v.numWheels;
        this->numDoors=v.numDoors;
      
        return v;
    }
  
    //2
    Vehicle operator==(const Vehicle & v)
    {
        if(this->numWheels==v.numWheels && this->numDoors==v.numDoors)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
  
    //3
    Vehicle operator!=(const Vehicle & v)
    {
        if(this->numWheels!=v.numWheels && this->numDoors!=v.numDoors)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
  
    //4.a
    Vehicle operator++()
    {
        Vehicle v;
        v.numWheels = ++numWheels;
        v.numDoors = ++numDoors;
      
        return v;
    }
  
    //4.b
    Vehicle operator++(int)
    {
        Vehicle v;
        v.numWheels = numWheels++;
        v.numDoors = numDoors++;
      
        return v;
    }
  
    //4.c
    Vehicle operator--()
    {
        Vehicle v;
        v.numWheels = --numWheels;
        v.numDoors = --numDoors;
      
        return v;
    }
  
    //4.d
    Vehicle operator--(int)
    {
        Vehicle v;
        v.numWheels = numWheels--;
        v.numDoors = numDoors--;
      
        return v;
    }
  
    friend ostream& operator<<( ostream& out, const Vehicle &v) ;
};


ostream& operator<<( ostream& out, const Vehicle &v)
{
    out<<"numWheels is : "<<v.numWheels;
    out<<"numDoors is : "<<v.numDoors;
}


int main()
{
Vehicle original(2,4);
Vehicle copy(original); // copy constructor by reference
cout << "Original is: " << original << " copy is: " << copy << endl;

cout << "Increment original: " << original++ << endl;
cout << "Increment copy:" << ++copy<< endl;
cout << "Decrement original:" << --original << endl;
cout << "Decrement copy:" << copy-- << endl;
// should be true :
cout << "Compare equality 1: " << (original == copy) << endl;
//should be false:
cout << "Compare equality 2: " << (--original == ++copy) << endl;
//should be true:
cout << "Compare inequality: " << (original != copy) << endl;
//This should make original = copy, and then return a Vehicle for output:
cout << "Assignment operator: " << (original = copy) << endl;
return 0;
}


Related Solutions

answer in c++ First – take your Box02.cpp file and rename the file Box03.cpp Did you...
answer in c++ First – take your Box02.cpp file and rename the file Box03.cpp Did you ever wonder how an object gets instantiated (created)? What really happens when you coded Box b1; or Date d1; or Coord c1; I know you have lost sleep over this. So here is the answer………. When an object is created, a constructor is run that creates the data items, gets a copy of the methods, and may or may not initialize the data items....
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; //...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; // PLEASE PUT YOUR FUNCTIONS BELOW THIS LINE // END OF FUNCTIONS void printArray(int array[], int count) {    cout << endl << "--------------------" << endl;    for(int i=1; i<=count; i++)    {        if(i % 3 == 0)        cout << " " << array[i-1] << endl;        else        cout << " " << array[i-1];    }    cout...
C++ Code while loops. 1. Ask for file name and open file. 2. Do a priming...
C++ Code while loops. 1. Ask for file name and open file. 2. Do a priming read and make a while loop that A) Reads in numbers B) Sums them up. C) Counts how many there are. D) Prints "With 10 numbers, the average is blank.." every 10 numbers. So with every 10 numbers read in print the average. 3. After the loop calculate average of all numbers and print it. So for example once the file is open the...
PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h...
PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h and answer the questions in cpp files. Array is used, not linked list. It would be nice if you could comment on the code so I can understand how you wrote it employee.h file #include <string> using namespace std; class Employee { private:    string name;    int ID, roomNumber;    string supervisorName; public:    Employee();       // constructor    void setName(string name_input);   ...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of complex Class The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features. Constructor Only one constructor with default value for Real...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork class. The features of an ArtWork are: Artist name (e.g. Vincent vanGogh or Stan Getz) Medium (e.g. oil painting or music composition) Name of piece (e.g. Starry Night or Late night blues) Year (e.g. 1837 or 1958)
Would you make separated this code by one .h file and two .c file? **********code************* #include...
Would you make separated this code by one .h file and two .c file? **********code************* #include <stdlib.h> #include <stdbool.h> #include <stdio.h> #include<time.h> // Prints out the rules of the game of "craps". void print_game_rule(void) { printf("Rules of the game of CRAPS\n"); printf("--------------------------\n"); printf("A player rolls two dice.Each die has six faces.\n"); printf("These faces contain 1, 2, 3, 4, 5, and 6 spots.\n"); printf("After the dice have come to rest, the sum of the spots\n on the two upward faces is...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
c++ ///////////////////////////////////////////////////////////////////////// need to be named Subsequences.h and pass test cpp file SubsequenceTester.cpp at the end...
c++ ///////////////////////////////////////////////////////////////////////// need to be named Subsequences.h and pass test cpp file SubsequenceTester.cpp at the end of this question. This assignment will help you solve a problem using recursion Description A subsequence is when all the letters of a word appear in the relative order of another word. Pin is a subsequence of Programming ace is a subsequence of abcde bad is NOT a subsequence abcde as the letters are not in order This assignment you will create a Subsequence...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT