Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3.) Write friend functions for the fallowing class to overload the <<and>>operat

ID: 3594072 • Letter: 3

Question

3.) Write friend functions for the fallowing class to overload the <<and>>operators. The friend function, input operator(operator>>), should have 5 elements into the array attribute called “grade” for the object and output operator(operator<<) should print the content of array “grade” of the object on the screen.

class Math

               //put the prototype of the friend function in here

public:

               int grade [5];

               Math() {for(int i=0; i<10, i++) grade[i]=0;};

                                            

//implement the overloaded input operator functions      // implement the overloaded output function

void main()

{

               Math ObjA;

               //your friend function should be able to take care of the fallowing code

               Cin>>ObjA;

               Cout<<ObjA;

Explanation / Answer

#include <iostream>

using namespace std;

class Math
{
//put the prototype of the friend function in here
public:
int grade [5];
Math() {
for(int i=0; i<5; i++)
grade[i]=0;
  
};
friend ostream & operator << (ostream &out, const Math &c);
friend istream & operator >> (istream &in, Math &c);

};   

//implement the overloaded input operator functions // implement the overloaded output function
ostream & operator << (ostream &out, const Math &c)
{
for (int i = 0; i < 5; i++){
out << c.grade[i] << " ";
}
out << endl;
return out;
}

istream & operator >> (istream &in, Math &c)
{
for (int i = 0; i < 5; i++) {
in >> c.grade[i];
}
return in;
}


int main()
{
Math ObjA;

//your friend function should be able to take care of the fallowing code
cin>>ObjA;
cout<<ObjA;

return 0;
}

Sample run

1
2
3
4
5

1 2 3 4 5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote