User Tools

Site Tools


c_-_c_operator_overloading:cast_operator_overloading

C - C++ Operator Overloading - Cast Operator Overloading

#include <iostream>
using namespace std;
 
class forgetcode 
{
  long double d;
  int i;
  long double v;
 
public:
  forgetcode(double b1, int e1)
  {
    d = b1;
    i = e1;
    v = 1;
 
    if(e1==0)
      return;
 
    for( ; e1>0; e1--)
      v = v * d;
  }
 
  forgetcode operator+(forgetcode o) 
  {
    long double b1;
    int e1;
    b1 = d + o.d;
    e1 = i + o.i;
 
    forgetcode temp(b1, e1);
    return temp;
  }
 
  operator long double() { return v; } // convert to double
};
 
 
int main() 
{
  forgetcode x(5.0, 3);
  long double a;
 
  a = x;             
  cout << x + 1.2; 
  cout << "\n";
 
  forgetcode y(4.4, 4), z(1, 1);
 
  z = x + y;  
  a = z;      
  cout << a;
 
  return 0;
}
c_-_c_operator_overloading/cast_operator_overloading.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki