TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JAMIE KOWINSKY
from: KURT KUZBA
date: 1997-09-24 01:19:00
subject: cout`ing an object

JK>   normally to print this thing i have to call "thing.print"
JK>   I would rather do a "cout << thing".  I am fimular with
JK>   overloading operators, and I would like to know if its
JK>   possible to overload the << operator somehow to do this
JK>   Or perhaps there is another way.
   You could try something like this.
   I just posted this sample a while ago.
   If you read the ECHO faithfully, you will often see your
   answers before you even ask the questions. :)
/*_|_|   MATHCLAS.CPP   PUBLIC DOMAIN   by Kurt Kuzba  9/11/1997
_|_|_|   Example of dynamic object creation and ostream friends.
_|_|*/
#include 
#include 
class MathCalc
{
private:
   int iVar1, iVar2, iResult;
   char cOperation;
public:
   MathCalc(int, int, char);
   friend ostream &operator<<(ostream&, const MathCalc&);
   // This function is an ostream friend and has access to
   // ostream functions. It returns an ostream object which
   // is created within the friend function
};
MathCalc::MathCalc(int iA, int iB, char cOp = '+')
{
   switch(cOperation = cOp)
   {
      case '-':iResult = (iVar1 = iA) - (iVar2 = iB); break;
      case '/':iResult = (iVar1 = iA) / (iVar2 = iB); break;
      case '*':iResult = (iVar1 = iA) * (iVar2 = iB); break;
      default :iResult = (iVar1 = iA) + (iVar2 = iB);
         cOperation = '+'; break;
   }
}
ostream &operator<<(ostream &O, const MathCalc &MC)
{
   // This function creates an ostream object. It accepts an
   // ostream object and a data object. It then formats the
   // data in the data object to an ostream format and returns
   // the ostream to the calling function.
   O << MC.iVar1 << " " << MC.cOperation << " " << MC.iVar2
     << " = " << MC.iResult << "\n\r" << flush;
   return O;
}
int main(void)
{
   char cOp;
   int iV1, iV2;
   MathCalc
      mcA(12, 18),
      mcB(30, 18, '-'),
      mcC(12, 12, '*'),
      mcD(144, 12, '/');
   MathCalc
      *pmcA = new MathCalc (130, 20),
      *pmcB = new MathCalc (150, 20, '-'),
      *pmcC = new MathCalc (256, 16, '/'),
      *pmcD = new MathCalc (16, 16, '*');
   cout << mcA << mcB << mcC << mcD
       << *pmcA << *pmcB << *pmcC << *pmcD
       " << endl;
   delete pmcA, pmcB, pmcC, pmcD;
   getch();
   do   {
      cout  " << flush;
      cin >> iV1;
      cout  " << flush;
      cin >> cOp;
      cout  " << flush;
      cin >> iV2;
      pmcA = new MathCalc (iV1, iV2, cOp);
      cout " << endl;
      delete pmcA;
   }   while('\x1b' != getch());
   return 0;
}
> ] Sorry.  I left my taglines in my other offline reader.......
---
---------------
* Origin: *YOPS ]I[* 3.1 GIG * RA/FD/FE RADist * Milwaukee, WI (1:154/750)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.