please just help with parts afterr //  3) Write friend function for the following class to overload the << and >> operators. The friend function, input operator (operator >>), should read 5 elements into the array attribute called “grade” for the objet and output (operator <<) should print the content of the array “grade” of the object on the screen. class Math   { // put the prototype of the friend functions here public: intgrade[5]:math( )   {for (int i=0, i<10, i++) grade[i] = 0;}; // implement the overloaded input operator function // implement the overloaded output operator function   void main (){ Math ObjA; //your friend function should be able to take care of the following code  cin >> ObjA; cout << ObjA;

Question
Asked Oct 13, 2019
18 views

please just help with parts afterr //

 

 

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

 

class Math

   { // put the prototype of the friend functions here

 

public:

 

intgrade[5]:

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

 

// implement the overloaded input operator function

 

// implement the overloaded output operator function 

 

 

void main ()

{

 

Math ObjA;

 

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

 

cin >> ObjA;

 

cout << ObjA;

check_circle

Expert Answer

Step 1

A friend function is defined outside the context of a class, but it has the rights to access the member and member function of that class. That is, it can access private, public and protected members of that class.

A keyword friend is used to defining a friend function.

Syntax:

class class_name

{

           friend function_declaration(arguments) {

           }

}  

Step 2

Algorithm:

  • Build a Math class.
  • Declare the prototype of the input friend function with the input stream(istream) and the input operator (> >).
  • Example:- friend istream & operator >> (istream &input, Math &a)
  • Declare the prototype of the output friend function with the output stream(ostream) and the input operator (< <).
  • Example:- friend ostream & operator << (ostream &output, const Math &a);
  • Declares an integer type array with size 5 called grade.
  • Insert default value in array class with the default constructor.
  • Implement the function of the overloaded input operator.
  • Implement the function of the overloaded output operator.
  • Build math class object.
  • Calling input output friend function.
Step 3

#include <iostream>

 

using namespace std;

//build a class Math

class Math {

  //declaration of the input friend function

  friend istream & operator >> (istream & input, Math & a);

  //declaration of the output friend function

  friend ostream & operator << (ostream & output,

    const Math & a);

  public:

    //an array declaration which takes 5 elements

    int grade[5];

  //default constructor

  Math()

 

  {

    for (int i = 0; i < 5; i++) {

      grade[i] = 0;

    }

  }

};

//definition of the input friend function

istream & operator >> (istream & input, Math & a)

 

{

  cout << "Please enter 5 Integer ...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Write a program which does the following (name=xxx_Chapter81):1) Asks the user for the number of row...

A: Description:The following program is to print the two dimensional array.

question_answer

Q: Python 3.7.4 Open the file hostdata.txt for reading.

A: To open the file hostdata.txt for reading, the statement is: f=open('hostdata.txt','r')The following...

question_answer

Q: What are the implications of crypto currency to the international banking system?

A: A cryptocurrency is a kind of digital asset which is designed  to work as medium of exchange that us...

question_answer

Q: Consider a program that declares global integer variables x, y[10]. Thesevariables are allocated sta...

A: Explanation of the code: lw $s1, 0($gp) #load value in base address to $s1addi $s1, $s1, 25 #increme...

question_answer

Q: program7_1.pyWrite a Python program that creates a list of your friends. Start with an empty list a...

A: Program:frd_list = []frdName = input('Enter the first name of a friend or ENTER to quit ')frdName = ...

question_answer

Q: Using Java Eclipse IDE write the following program:

A: Following program prompts the user to enter 10 numbers and invokes the method public static double m...

question_answer

Q: given 0011 0000 0000 0000 0000 0000 1111 0011 what is the answer when multipy by 2

A: Multiplying a binary number with 2 means performing left shift operation. The given binary number 00...

question_answer

Q: Think about your own university or college and choose an idea that could improve student satisfactio...

A: I passed from state university named ‘WBTU’.The university provide online facilities for students to...

question_answer

Q: Are the following two clauses the same: constraint X primary key (Y, Z) and constraint X primarykey ...

A: Primary key:Primary key is a key which is used to uniquely identify a row in a table.A Primary key c...