In: Computer Science
(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:
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.)
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.
An overloaded assignment operator, so objects can be added to STL vectors and queues.
Write an H and a CPP. Don't write the constructor inline (because we'll add to it in a later version).
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.
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 ##################