The lab for this week will be on operator overloading. Design a class named Arr
ID: 3687111 • Letter: T
Question
The lab for this week will be on operator overloading. Design a class named Arr that has a private member named scores as an array of size 5 of integer and do the following requirements:
1- Overload the ++ operator to increment each item of the array by 1.
2- Overload the -- operator to decrement each item of the array by 1.
3- Overload the + operator to add a bounce of 5 points for each item of the array.
4- Overload the operator >> instream operator to inter the values of the array.
5- Overload the operator << outstream operator to print the values of the array.
Explanation / Answer
#include<iostream.h>
#include<stdlib.h>
class Arr
{
private:
int scores[5];
public:
Arr()
{
for(int i=0;i<5;i++)
{
scores[i]=i+1;
}
}
void operator++()
{
for(int i=0;i<5;i++)
{
scores[i]=scores[i]+1;
}
}
void operator--()
{
for(int i=0;i<5;i++)
{
scores[i]=scores[i]-1;
}
}
Arr operator+(Arr o2)
{
for(int i=0;i<5;i++)
{
o2.scores[i]=o2.scores[i]+5;
}
return o2;
}
void print()
{
for(int i=0;i<5;i++)
{
cout<<scores[i]<<endl;
}
}
};
void main()
{
Arr o,o1,o3;
o.print();
o++;
o.print();
o--;
o.print();
o3=o+o1;
o3.print();
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.