Home Forums C Programming Operator Overloading

Viewing 0 reply threads
  • Author
    Posts
    • #1900
      sameena
      Participant

      Hi

      Doing a course in C++.Net which upto now has been ok and interesting. However Operator Overloading is confusing me to a great deal. There is plenty written on the subject but for Computer Scientists who just love mathematics and complicated text! To find something simple – it does not exist. I can use operator overloading like a parrot but I want to know what exactly is going on…how does the compiler treat an overloaded operator…etc.

      We were asked to make an Exponent class that will allow us to overload the ^ operator so that we can perform exponential notation so that you can have iResult=iNumber ^10 where iNumber equals 10 so that iResult equals 1000. This is my zillionth attempt but the overloaded function is not called..please help.

      This is the class:

      #pragma once
      #include
      #include
      using namespace std;
      class Exponentor
      {
      public:
          Exponentor(void);
          ~Exponentor(void);
          //Exponentor operator ^ (int num1);
          //Exponentor operator ^ (Exponentor&);
          Exponentor operator^(int num1);
          int iLeftNum;
          int iRightNum;
          int iResult; // no private vars required...
      };
      This is the code:
      #include "exponentor.h"

      Exponentor::Exponentor(void)
      {
      iResult=0;
      iLeftNum=0;
      iRightNum=0;
      }

      Exponentor::~Exponentor(void)
      {
      }

      void main() {
          Exponentor Exponent;
          cout << "Welcome to Exponentor the exponent test programme!n";
          cout << "**************************************************n";
          int iRightNumber;
          cout << "Hello, please give me the left operand:";
          cin >> iRightNumber;
          cout << "nand now please give me the right operand:";
          int iAnyNumber;
          cin >> iAnyNumber;
          Exponent.iResult=iRightNumber ^ iAnyNumber;
          cout << "nThe result of  " <<
      Exponent.iLeftNum << " raised to " << iAnyNumber << "
      is:" << Exponent.iResult << endl;

          }
      //Exponentor operator^(int num1);
      Exponentor Exponentor::operator^(int num1)
      {
          cout << "In the function";

          return *this;
      }

      Thanks in advance

Viewing 0 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.