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...
PLEASE ANSWER USING C# Is there a Prius version? Did you know that the average Boeing...
PLEASE ANSWER USING 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...
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:...
PROGRAM SIMULATION. Understand the given JAVA program and write the output. 1.     public class Places        {...
PROGRAM SIMULATION. Understand the given JAVA program and write the output. 1.     public class Places        {            public static void main(String args[])            {                   String place[]=new String[4];           place[0]="Salmaniya"; place[1]="Salmabad"; place[2]="Isa Town"; place[3] = “Manama”         System.out.println(place[3]);         System.out.println(place[1].toLowerCase());         System.out.println(place[2].substring(4,6);         System.out.println(place[3].charAt(4));         System.out.println(place[1].equals(place[2]));            } }    b. public class ChangeIt { public void doIt( int[] z ) { z[0] = 0; } } public class TestIt { public static void main ( String[] args ) {...
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...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following two methods(headers provided) as described below: 1. A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT