add the following class methods(definition, implementation and function calls):
ID: 2879022 • Letter: A
Question
add the following class methods(definition, implementation and function calls):
operator +()
operator >=()
operator >>()
operator <<()
operator =()
copy constructor
operator ++()
operator ++(int dummy)
The dropbox is Extra Credit OpOverload. Submit the code in a text file.
#include <iostream>
#include <string>
#include <cstdlib>
#include <cctype>
using namespace std;
class Nabil{
private:
int theater;
string engineer;
double bridge;
public:
void setTheater(int = 8);
void setEngineer(string = "What a combination - Art and Science");
void setBridge(double = 1.2);
int getTheater();
string getEngineer();
double getBridge();
void input();
void output();
bool compareGreaterThan(const Nabil &n) const;
double addBridges(const Nabil&) const;
Nabil();
};
int main(){
Nabil n1;
Nabil n2;
n1.setBridge(9.8);
n1.setTheater(18);
n2.input();
if(n1.compareGreaterThan(n2))
cout << "N1 is greater than life itself. ";
else
cout << "N1 as a long road to take. ";
int sum = n2.addBridges(n1);
cout << "The convergence of these two Nabils is " << sum * 3 << " exponential units ";
cout << " ";
n1.output();
cout << " ";
n2.output();
cin.get();
}
void Nabil::setTheater(int t){
theater = t;
}
void Nabil::setEngineer(string e){
engineer = e;
}
void Nabil::setBridge(double b){
bridge = b;
}
int Nabil::getTheater(){
return theater;
}
string Nabil::getEngineer(){
return engineer;
}
double Nabil::getBridge(){
return bridge;
}
void Nabil::input(){
char sInput[200];
cout << "Enter an integer: ";
cin >> sInput;
theater = atoi(sInput);
cout << "Enter a decimal number: ";
cin >> sInput;
bridge = atof(sInput);
cin.ignore(100, ' ');
cout << "Enter a famous quote: ";
getline(cin, engineer);
}
void Nabil::output(){
cout << theater << ", " << bridge << ", "" << engineer << """ << endl;
}
bool Nabil::compareGreaterThan(const Nabil &n) const{
return (theater * bridge) >= (n.theater*n.bridge);
}
double Nabil::addBridges(const Nabil& n) const{
return bridge + n.bridge;
}
Nabil::Nabil(){
theater = 1;
bridge = 3.14;
engineer = "Coding rocks!";
}
Explanation / Answer
#include <iostream>
#include <string>
#include <cstdlib>
#include <cctype>
using namespace std;
class Nabil{
private:
int theater;
string engineer;
double bridge;
public:
void setTheater(int = 8);
void setEngineer(string = "What a combination - Art and Science");
void setBridge(double = 1.2);
int getTheater();
string getEngineer();
double getBridge();
void input();
void output();
bool compareGreaterThan(const Nabil &n) const;
double addBridges(const Nabil&) const;
Nabil();
Nabil(Nabil &n);
Nabil operator +(Nabil &n);
bool operator >=(Nabil &n);
friend istream &operator >>(istream &in, Nabil &n);
friend ostream &operator <<(ostream &out, Nabil &n);
void operator =(Nabil n);
Nabil operator ++();
Nabil operator ++(int dummy);
};
int main(){
Nabil n1;
Nabil n2;
n1.setBridge(9.8);
n1.setTheater(18);
n2.input();
if(n1.compareGreaterThan(n2))
cout << "N1 is greater than life itself. ";
else
cout << "N1 as a long road to take. ";
int sum = n2.addBridges(n1);
cout << "The convergence of these two Nabils is " << sum * 3 << " exponential units ";
cout << " ";
n1.output();
cout << " ";
n2.output();
cin.get();
}
void Nabil::setTheater(int t){
theater = t;
}
void Nabil::setEngineer(string e){
engineer = e;
}
void Nabil::setBridge(double b){
bridge = b;
}
int Nabil::getTheater(){
return theater;
}
string Nabil::getEngineer(){
return engineer;
}
double Nabil::getBridge(){
return bridge;
}
void Nabil::input(){
char sInput[200];
cout << "Enter an integer: ";
cin >> sInput;
theater = atoi(sInput);
cout << "Enter a decimal number: ";
cin >> sInput;
bridge = atof(sInput);
cin.ignore(100, ' ');
cout << "Enter a famous quote: ";
getline(cin, engineer);
}
void Nabil::output(){
cout << theater << ", " << bridge << ", "" << engineer << """ << endl;
}
bool Nabil::compareGreaterThan(const Nabil &n) const{
return (theater * bridge) >= (n.theater*n.bridge);
}
double Nabil::addBridges(const Nabil& n) const{
return bridge + n.bridge;
}
Nabil::Nabil(){
theater = 1;
bridge = 3.14;
engineer = "Coding rocks!";
}
Nabil::Nabil(Nabil &n){
this->theater = n.theater;
this->bridge = n.bridge;
this->engineer = n.engineer;
}
Nabil Nabil::operator +(Nabil &n){
this->addBridges(n);
return *this;
}
bool Nabil::operator >=(Nabil &n){
return compareGreaterThan(n);
}
istream &operator >>(istream &in, Nabil &n){
char sInput[200];
cout << "Enter an integer: ";
in >> sInput;
n.theater = atoi(sInput);
cout << "Enter a decimal number: ";
in >> sInput;
n.bridge = atof(sInput);
in.ignore(100, ' ');
cout << "Enter a famous quote: ";
getline(in, n.engineer);
return in;
}
ostream &operator <<(ostream &out, Nabil &n){
out << n.theater << ", " << n.bridge << ", "" << n.engineer << """;
return out;
}
void Nabil::operator =(Nabil n){
this->theater = n.theater;
this->bridge = n.bridge;
this->engineer = n.engineer;
}
Nabil Nabil::operator ++(){
Nabil ret = *this;
this->bridge++;
return ret;
}
Nabil Nabil::operator ++(int dummy){
Nabil ret = *this;
this->bridge += dummy;
return ret;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.