Question

In: Computer Science

(C++) Write version 1.0 of the Rider class for the elevator simulation. Riders will "know" their...

(C++) Write version 1.0 of the Rider class for the elevator simulation. Riders will "know" their starting floor and their destination floor. But instead of using pointers or reference variables as data members, they use whole number int s. (The simulation will have an array of floor objects to which all classes will have access, so all that's needed to refer to a floor is its index in the global array.)

Here's its specification:

  1. As the only data members, two constant whole numbers representing the start and destination floors. Name then to and from , but as int s. (Two more data members will be added later.)

  2. One constructor with two parameters -- the indexes of the start and destination floors, in that order, to be copied to the data members in an initializer list.

  3. An overloaded assignment operator, so objects can be added to STL vectors and queues.

  4. Write an H and a CPP. Don't write the constructor inline (because we'll add to it in a later version).

  5. Since all members are public, write this as a struct .

The Rider class declaration for Rider.h,

Test it with this _testRider.cpp. Compile like this:

  c++ -std=c++11 _testRider.cpp Rider.cpp -Wall

This is not the first full and complete "building block" in our elevator simulation, but it's a start.

Submit Rider.cpp and Rider.h.

Solutions

Expert Solution

Summary

Solution provided with Notes , code for Rider.h and Rider.cpp and output .

Notes :

Rider struct is implemented with given requirements along with added default constructor , copy constructor and implemented "<<" operator for Rider as well.

As part of the testing , all the constructors are exercised along with assignemtn and "<<" operator .

Created a list of Riders as well .

################ Code ##################

#include <ostream>
using namespace std;

#ifndef RIDER_H_
#define RIDER_H_

struct Rider
{
int start;
int destination;

Rider();
Rider(int s , int d );

Rider(const Rider& other );

Rider& operator=( const Rider& rhs );
friend ostream& operator << ( ostream & out , const Rider& rd );
};

#endif /* RIDER_H_ */

>>> Rider.cpp <<<

#include <ostream>
#include "Rider.h"

using namespace std;

Rider::Rider()
{
start = 0 ;
destination = 0;
}
Rider::Rider(int s , int d )
{
    start = s;
    destination = d;
}

Rider::Rider(const Rider& other )
{
    start = other.start;
    destination = other.destination;
}

Rider& Rider::operator=( const Rider& rhs )
{
   this->start = rhs.start;
   this->destination = rhs.destination;

   return *this;
}


ostream& operator << ( ostream & out , const Rider& rd )
{
   out << " Source : " << rd.start << " Destination : " << rd.destination ;
   return out;
}


>>> testRider.cpp <<

#include <ostream>
#include "Rider.h"

using namespace std;

Rider::Rider()
{
start = 0 ;
destination = 0;
}
Rider::Rider(int s , int d )
{
    start = s;
    destination = d;
}

Rider::Rider(const Rider& other )
{
    start = other.start;
    destination = other.destination;
}

Rider& Rider::operator=( const Rider& rhs )
{
   this->start = rhs.start;
   this->destination = rhs.destination;

   return *this;
}


ostream& operator << ( ostream & out , const Rider& rd )
{
   out << " Source : " << rd.start << " Destination : " << rd.destination ;
   return out;
}


>>>> testRider.cpp <<<

#include <iostream>
#include "Rider.h"

int main()
{
// exercise overloaded constructor with params
Rider rider1(3,4);
Rider rider2(5,6);

// exercise default constructor
Rider rider3 ;

std::cout << "rider1 :" << rider1 << "\n";
std::cout << "rider2 :" << rider2 << "\n";

// exercise assignment constructor
rider3 = rider2 ;

std::cout << "rider3 :" << rider3 << "\n";

// exercise copy constructor
Rider rider4(rider1);

std::cout << "rider4 :" << rider4 << "\n";

// Define array of riders which uses default constructor to initialize the rider objects
Rider riderList[3];

// assign random value sto start and destinations
for ( int i=0 ; i < 3 ; i++ )
{
    riderList[i].start = i + 2 ;
    riderList[i].destination = i + 5 ;
}

// Print the riders to output
for ( int i=0 ; i < 3 ; i++ )
{
    cout << " rider( " << i << ") : " << riderList[i] << "\n";
}
}


####################### Output ##################


Related Solutions

Elevator (C++) Following the diagram shown below, create the class Elevator. An Elevator represents a moveable...
Elevator (C++) Following the diagram shown below, create the class Elevator. An Elevator represents a moveable carriage that lifts passengers between floors. As an elevator operates, its sequence of operations are to open its doors, let off passengers, accept new passengers, handle a floor request, close its doors and move to another floor where this sequence repeats over and over while there are people onboard. A sample driver for this class is shown below. Each elevator request translates into just...
PLEASE DO THIS IN C#.Is there a Prius version? Did you know that the average Boeing...
PLEASE DO THIS IN C#.Is there a Prius version? Did you know that the average Boeing 747 airplane uses approximately 1 gallon of fuel per second? Given the speed of the airplane, that means it gets 5 gallons to the mile. No, not 5 miles to the gallon, 5 gallons to the mile. You may be questioning why such a horribly inefficient machine is allowed to exist, but you’ll be happy to find out that, because this airplane hold 568...
IN C++ LANGUAGE PLEASE:::: Is there a Prius version? Did you know that the average Boeing...
IN C++ LANGUAGE PLEASE:::: Is there a Prius version? Did you know that the average Boeing 747 airplane uses approximately 1 gallon of fuel per second?  Given the speed of the airplane, that means it gets 5 gallons to the mile. No, not 5 miles to the gallon, 5 gallons to the mile.   You may be questioning why such a horribly inefficient machine is allowed to exist, but you’ll be happy to find out that, because this airplane hold 568 people, it...
Write your own version of a class template that will create a static stack of any...
Write your own version of a class template that will create a static stack of any data type. Demonstrate the class with a driver program. please make a version to copy.
C Programming Question: Q) Write a C - program to implement an Uprooted version (Save to...
C Programming Question: Q) Write a C - program to implement an Uprooted version (Save to parent pointer instead of child pointer, ie. parent of top is null) of Kruskal's Minimum Spanning Tree with adjacency list and min-heap as the additional data structure. Note: Please implement it in C and also keep the above conditions in mind. You can take your time. Thank you.
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a...
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a member function named      int distance2origin() that calculates the distance from the (x, y, z) point to the origin (0, 0, 0) the ‘prototype’ in the class definition will be      int distance2origin() outside the class definition             int Coord :: distance2origin() {                         // your code } _______________________________________________________________________________________________________ /************************************************** * * program name: Coord02 * Author: * date due: 10/19/20 * remarks:...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so react that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will...
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads....
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads. The program will take in an array from 1 to n (n = 50) and will be passed to four different threads: If the current number is divisible by 2, then print HAP If the current number is divisible by 5, then print PY If the current number is divisible by both 2 and 5, then print HAPPY If the number is neither divisible...
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
Write a C program that calculates a student grade in the C Programming Class. Ask the...
Write a C program that calculates a student grade in the C Programming Class. Ask the user to enter the grades for each one of the assignments completed in class: Quiz #1 - 25 points Quiz #2 - 50 points Quiz #3 - 30 points Project #1 - 100 points Project #2 - 100 points Final Test - 100 points The total of the quizzes count for a 30% of the total grade, the total of the projects counts for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT