Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reserved word questions 1

Status
Not open for further replies.

teser

Technical User
Mar 6, 2001
194
US
Please explain what strcpy is doing in this program. Also what is friend and the ostream& represent?? And I am not familiar with what the operator function is doing?


[tt]
#include <iostream.h>
#include <string.h>

class Building
{
public:
Building(char *room, int rmNum, char *cubicle) //CONSTRUCTOR
{
strcpy(Building::room,room);
Building::rmNum = rmNum;
strcpy(Building::cubicle,cubicle);
};

friend ostream& operator << (ostream& myoutput, Building s1);

private:
char room[200];
int rmNum;
char cubicle[200];
};

ostream& operator << (ostream& myoutput, Building s1)
{
cout << &quot;Room nam = &quot; << s1.room << &quot;\nRoom num = &quot; << s1.rmNum
<< &quot;\nCub = &quot; << s1.cubicle << endl;
return myoutput;
}

void main()
{
Building object1(&quot;Room1&quot;, 10, &quot;234-4T&quot;);
cout << &quot;First object info\n&quot;
<< &quot;-----------------\n&quot; << object1 << endl;[/tt]
 
ostream is an &quot;Output Stream&quot;

the friend allows ostream to have access to the class and to output it. The technical definition of friend is:
i
The friend keyword allows a function or class to gain access to the private and protected members of a class[/i]

What the strcpy is doing is copying specificly into the Building::room, which is equivalent to saying this->room. This is the same with cubicle. What would have been better and easier to read was to have the argument name be &quot;copy_room&quot; so there would be no confusion and strcpy would look like

strcpy(room,copy_room);


Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top