TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: FRANK MASINGILL
from: KURT KUZBA
date: 1997-09-11 12:19:00
subject: Worked this out

FM>   Criticize away if you have time and inclination.
   You forgot to add a smiley! :)
/*_|_|   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 (16, 16, '*'),
      *pmcD = new MathCalc (256, 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;
}
> ] Remain calm and think. I will help you. - Kwai Chang CPUfan.
---
---------------
* 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™.