in more basic C++ terms what does the following code mean?

friend ostream& operator << (ostream& os, const item* itm);

it is a function in a header file

this is the program and im trying to figure out the highlighted parts:

#ifndef ASSIGN3_H_
#define ASSIGN3_H_
#include <string>

using namespace std;

class item 
{
      public:
              string denomination;
              int date;
              string condition;
              float cost;
              float value;

              item();
              //default constructor
              
              item(string& itemDenomination, int itemDate, string& itemCondition, float itemCost, float itemValue);
              //constructor
              //the object is initialized according to the parameters.
              //postcondition: itemDenomination = denomination; 
              //               itemDate = date; 
              //               itemCondition = condition; 
              //               itemCost = cost; 
              //               itemValue = value;
              
              friend ostream& operator << (ostream& os, const item* itm);
};

class coin : public item 
{

              string obverse;
              string converse;
              char metal;
              int percentage;

      public:
             coin();
             //default constructor
             
             coin(string& coinDenomination, int coinDate, string& coinCondition, float coinCost, float coinValue, string& coinObverse, string& coinConverse, char coinMetal, int coinPercentage);
             //constructor
             //function to set the coin information
             //the coin information is set according to the parameters
             //Postcondition: obverse = coinObverse;
             //               converse = coinConverse;
             //               metal = coinMetal;
             //               percentage = coinPercentage;
             
             friend ostream& operator << (ostream& os, const coin* cn);
             //
};


class paper : public item 
{

              bool newstyle;
              char type;
      
      public:
             paper();
             //default constructor
             
             paper(string& paperDenomination, int paperDate, string& paperCondition, float paperCost, float paperValue, bool paperNewstyle, char paperType);
             //constructor
             //function to set the paper information
             //the paper information is set according to the parameters
             //postcondition: newstyle = paperNewstyle;
             //               type = paperType; 
             
             friend ostream& operator << (ostream& os, const paper* ppr);
             //
};
#endif

#include <iostream>
//#include "assign3new.h"

item::item() 
{
	denomination = "none";
	date = 1900;
	condition = "unknown";
	cost = 0.0;
	value = 0.0;
}

item::item(string& itemDenomination, int itemDate, string& itemCondition, const float itemCost, float itemValue) 
{
           denomination = itemDenomination;
           date = itemDate;
           condition = itemCondition;
           cost = itemCost;
           value = itemValue;
}

ostream& operator <<(ostream& os, item* itm) 
{
	os << itm->denomination << endl;
	os << itm->date << endl;
	os << itm->condition << endl;
	os << itm->cost << ' ' << itm->value << endl;
	
	return os;
}



coin::coin() 
{
	string obverse = "none";
	string converse = "none";
	metal = 'U';
	percentage = 0;
}

coin::coin(string& coinDenomination, int coinDate, string& coinCondition, float coinCost, float coinValue, string& coinObverse, string& coinConverse, char coinMetal, int coinPercentage) 
                   :item(coinDenomination, coinDate, coinCondition, coinCost, coinValue)
{
           obverse = coinObverse;
           converse = coinConverse;
           metal = coinMetal;
           percentage = coinPercentage;
}

ostream& operator <<(ostream& os, const coin* cn) 
{
	os << "coin: " << endl;
	os << (item*)cn;
	os << cn->obverse << endl;
	os << cn->converse << endl;
	os << cn->metal << endl;
	os << cn->percentage << endl;
	
	return os;
}

paper::paper() 
{
	newstyle = false;
	type = 'U';
}

paper::paper(string& paperDenomination, int paperDate, string& paperCondition, float paperCost, float paperValue, bool paperNewstyle, char paperType) 
                     :item(paperDenomination, paperDate, paperCondition, paperCost,paperValue)
{
            newstyle = paperNewstyle;
            type = paperType;      
}

ostream& operator <<(ostream& os, const paper* ppr) 
{
	os << "paper:" << endl;
	os << (item*)ppr;
	os << ppr->type << endl;
	os << ppr->newstyle << endl;
	
	return os;
}

#include <iostream>
#include <fstream>
#include <string>
//#include "assign3new.h"

using namespace std;

int menu();

void add_item(char type);

int main() 
{
	int argc;
	char argv;
    
    for (;;) 
    {
		switch (menu()) 
        {
		       case 0:
			        return 0;
               case 1:
			        add_item('c');
			        break;
               case 2:
			        add_item('p');
			        break;
		            default:
	           cout << "error" << endl;
		}
	}
    return 0;
}

int menu() 
{
	for (;;) 
    {
		cout << "0   Exit" << endl << "1   Create an instance of coin" << endl << "2   Create an instance of paper money" << endl;
		
		int choice;
		
		cin >> choice;
		cin.clear();
		cin.ignore(250, '\n');
		
		if ((choice < 0) || (choice > 2)) 
        {
			cout << "try again" << endl;
			continue;
		}
		return choice;
	}
}

void add_item(char type) 
{
	item * new_item;
	string denomination;
	int date;
	string condition;
	float cost;
	float value;
	
    cout << "Enter denomination: ";
	cin >> denomination;
	cout << "Enter date: ";
	cin >> date;
	cout << "Enter condition: ";
	cin >> condition;
	cout << "Enter cost: ";
	cin >> cost;
	cout << "Enter value: ";
	cin >> value;
	
	if (type == 'c') 
    {
		string obverse;
		string converse;
		char metal;
		int percentage;
		
		cout << "Enter obverse: ";
		cin >> obverse;
		cout << "Enter converse: ";
		cin >> converse;
		cout << "Enter metal: ";
		cin >> metal;
		cout << "Enter percentage: ";
		cin >> percentage;
		
		new_item = new coin(denomination, date, condition, cost, value, obverse, converse, metal, percentage);
	} 
    
    else if (type == 'p') 
    {
		bool newstyle;
		char _type;
		
        cout << "Enter newstyle: ";
		cin >> newstyle;
		cout << "Enter type: ";
		cin >> _type;
		
		new_item = new paper(denomination, date, condition, cost, value, newstyle, _type);
	}
	
    ofstream report("report.txt");
	
    if (report.is_open()) 
    {
		if (type == 'c') 
        {
			report << (coin*)new_item;
		} 
        else if (type == 'p') 
        {
			report << (paper*)new_item;
		}
		
        report.close();
	} 
    else
    {
		cout << "Unable to open file report.txt";
    }
	
    delete new_item;
	cin.clear();
	cin.ignore(250, '\n');
}

That's called an operator overload.

The << and >> operators are bit-shift operators, but in C++ the STL uses them with iostreams as insertion and extraction operators.

In other words: cout << "Hello world!" << endl; cout is the left object
<< is the operator
"Hello world!" is the right object

The operator writes the right object to the left object stream, and returns the left object (cout), so what remains to be done is: cout << endl; Again, cout is the left object
<< is the operator
endl is the right object

The operator writes a newline to the left object stream and flushes it, and returns the left object (cout).

This is a convenient idiom, so you can use it to do things like:

struct point_t
  {
  int x, y;
  point_t( int x = 0, int y = 0 ): x( x ), y( y ) { }
  };

point_t p( 10, -7 );

cout << "The point is " << p << endl;

Unfortunately, C++ doesn't know anything about how to write a point to cout, so you have to tell it how by overloading the << operator to work on a point_t.

ostream& operator << ( ostream& outs, const point_t& pt )
  {
  // write the right object (pt) to the left object stream
  outs << '(' << pt.x << ',' << pt.y << ')';
  // return the left object stream
  return outs;
  }

When you are dealing with classes with private data, and you need functions that are not part of the class to be able to access that private data, you must declare that function as a friend.

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.