User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,609 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,318 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 151 | Replies: 3
Reply
Join Date: Jul 2008
Posts: 2
Reputation: supersoup is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
supersoup supersoup is offline Offline
Newbie Poster

what does this mean?

  #1  
Jul 21st, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Location: WA, USA
Posts: 676
Reputation: Alex Edwards has a spectacular aura about Alex Edwards has a spectacular aura about Alex Edwards has a spectacular aura about 
Rep Power: 4
Solved Threads: 56
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Practically a Master Poster

Re: what does this mean?

  #2  
Jul 21st, 2008
Which header file yielded this function?
Reply With Quote  
Join Date: Jul 2008
Posts: 2
Reputation: supersoup is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
supersoup supersoup is offline Offline
Newbie Poster

Re: what does this mean?

  #3  
Jul 21st, 2008
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');
}
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,839
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 189
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: what does this mean?

  #4  
Jul 21st, 2008
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:
  1. struct point_t
  2. {
  3. int x, y;
  4. point_t( int x = 0, int y = 0 ): x( x ), y( y ) { }
  5. };
  6.  
  7. point_t p( 10, -7 );
  8.  
  9. 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.
  1. ostream& operator << ( ostream& outs, const point_t& pt )
  2. {
  3. // write the right object (pt) to the left object stream
  4. outs << '(' << pt.x << ',' << pt.y << ')';
  5. // return the left object stream
  6. return outs;
  7. }
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:34 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC