C++ Define an Investor class that has the following member variables and functio
ID: 673469 • Letter: C
Question
C++
Define an Investor class that has the following member variables and functions:
Two (2) private member variables:
A double array of size 10 that stores investment dollar amounts for 10 companies;
An int variable that stores the number of gains.
Three (3) overloading operators as member functions:
The unary operator ++ increments the number of gains
The unary operator += n increments all 10 investment amount by n dollars
The binary operator [ ] returns an element of the investment array (return type: double)
Four (4) overloading operators as friend functions:
The binary operator + adds the investment arrays, element by element, and the number of gains for two investors
The binary operator > compares the total investment amounts, and the number of gains of two investor objects (return type: bool)
The binary operator >> inputs the investment array and the number of gains (return type istream&)
The binary operator << outputs the investment array and the number of gains (return type ostrean&)
In the main, create at least three investor objects. Test all operators you created, namely,
Input two investors (>>)
Compare the two investors (>) to determine which investor has a better performance
Merge two investors by adding the two investors (+)
Increment number of gains for investors (++)
Append investment amount for investors (+=)
Access investment amount for a particular company ([ ])
Output investors (<<)
Explanation / Answer
CODE :
#include<stdio.h>
#include<iostream>
using namespace std;
class Investor
{
public:
Investor();
private:
double amount[10];
int gain;
public:
operator ++()
{
gain++;
}
operator +=(int n)
{
for(int i=0;i<10;i++)
{
amount[i]=amount[i]+n;
}
}
double operator [](int i)
{
return amount[i];
}
operator +(Investor& inv1)
{
double total[10];
for(int i=0;i<10;i++)
{
total[i]=inv1.amount[i]+this->amount[i];
}
int totalgain=inv1.gain+this->gain;
}
bool operator >(Investor& inv1)
{
double t1=0,t2=0;
for(int i=0;i<10;i++)
{
t1=t1+inv1.amount[i];
t2=t2+this->amount[i];
}
if(t2>t1)
{
if(this->gain>inv1.gain)
{
return true;
}
}
else
return false;
}
ostream &operator<<(ostream &out) //output
{
out<<"Investment Array : ";
for(int i=0;i<10;i++)
{
out<<this->amount[i]<<" ";
}
out<<"Gain: "<<this->gain<<" ";
return out;
}
istream &operator>>(istream &in) //input
{
cout<<"Input Investment Array : ";
for(int i=0;i<10;i++)
{
in>>this->amount[i];
}
cout<<"Enter Gain: ";
in>>this->gain;
return in;
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.