Below is a c++ program that represents throttle implementation. Add to the exist
ID: 667373 • Letter: B
Question
Below is a c++ program that represents throttle implementation. Add to the existing program with the following procedures:
1. Add a member function that will "compare" two throttles using the '==' symbol.
ex if ( car = truck ) ... Implement the '==' member function inside the class throttle.
i. To test the above, write code to call the '==' function explicitly ie operator==.
ii. Also code to call the above function using the nice syntax '=='.
2. Add the member function '!=' to throttle and implement this function outside the class.
i. To test the above, write code to call the '!=' function explicitly ie operator!=.
ii. Also code to call the above function using the nice syntax '!='.
3. Write an overloaded output function (<<) to print out an object throttle showing the Top_position and the current position.
Print out all steps that show these additional implementations.
:---Throttle Program---:
#include "StdAfx.h"
#include <iostream>
using namespace std;
class throttle
{
public:
int position, Top_position;
void shut_off();
void shift(int amount);
int max(int m);
double flow() const;
bool is_on() const;
throttle()
{
Top_position = 6;
position = 0;
}
throttle(int t)
{
Top_position = t;
position = 0;
}
throttle(int t, int p)
{
Top_position = t;
position = p;
}
};
int main()
{
throttle sample;
int user_input;
cout << "This throttle class contains 6 positions." << endl;
cout << "Enter a number from 0 to 6: ";
cin >> user_input;
if (user_input < 0)
{
cout << "Error! Throttle input cannot be less than 0!" << endl;
system("pause");
return EXIT_FAILURE;
}
sample.shut_off();
sample.shift (user_input);
while (sample.is_on())
{
cout << "The flow is now " << sample.flow() << endl;
sample.shift(-1);
}
cout << "The flow is now off." << endl;
cout << "" << endl;
cout << "Now inputting sample data. " << endl;
throttle car;
cout << "Car's current position: " << car.position << " Car's top position: " << car.Top_position << endl;
throttle truck(30);
cout << "Trucks's current position: " << truck.position << " Truck's top position: " << truck.Top_position << endl;
throttle shuttle(20, 6);
cout << "Shuttle's current position: " << shuttle.position << " Shuttle's top position: " << shuttle.Top_position << endl;
system("pause");
return EXIT_SUCCESS;
}
void throttle::shut_off()
{
position = 0;
}
void throttle::shift (int amount)
{
position += amount;
if (position < 0)
position = 0;
else if (position > 6)
position = 6;
}
double throttle::flow() const
{
return position / 6.0;
}
bool throttle::is_on() const
{
return (flow() > 0);
}
Explanation / Answer
#include <iostream>
using namespace std;
class throttle{
public:
int Top_Position;
int current_Position;
throttle(){
Top_Position = 6;
current_Position = 0;
}
throttle(int n){
Top_Position = n;
current_Position = 0;
}
throttle(int n,int c){
Top_Position = n;
current_Position = c;
}
throttle mythrottle(throttle truck){
throttle temp = truck;
return temp;
}
bool compare(throttle t1, throttle t2){
if (t1.Top_Position == t2.Top_Position && t1.current_Position == t2.current_Position) return true;
return false;
}
bool Not_Equal(throttle t1, throttle t2);
void output(){
cout << Top_Position << " " << current_Position << endl;
}
};
bool throttle::Not_Equal(throttle t1, throttle t2){
if (t1.Top_Position != t2.Top_Position || t1.current_Position != t2.current_Position) return true;
return false;
}
int main() {
throttle car();
throttle truck(30);
throttle shuttle(20,6);
throttle t = truck.mythrottle(truck);
t.output();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.