- This topic has 0 replies, 1 voice, and was last updated 17 years, 7 months ago by .
Viewing 0 reply threads
Viewing 0 reply threads
- The forum ‘C Programming’ is closed to new topics and replies.
Home › Forums › C Programming › Operator Overloading
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#pragma once<br /> #include <iostream><br /> #include <string><br /> using namespace std;<br /> class Exponentor<br /> {<br /> public:<br /> Exponentor(void);<br /> ~Exponentor(void);<br /> //Exponentor operator ^ (int num1);<br /> //Exponentor operator ^ (Exponentor&);<br /> Exponentor operator^(int num1);<br /> int iLeftNum;<br /> int iRightNum;<br /> int iResult; // no private vars required...<br /> };<br /> This is the code:<br /> #include "exponentor.h"<br /> <br /> Exponentor::Exponentor(void)<br /> {<br /> iResult=0;<br /> iLeftNum=0;<br /> iRightNum=0;<br /> }<br /> <br /> Exponentor::~Exponentor(void)<br /> {<br /> }<br /> <br /> void main() {<br /> Exponentor Exponent;<br /> cout << "Welcome to Exponentor the exponent test programme!n";<br /> cout << "**************************************************n";<br /> int iRightNumber;<br /> cout << "Hello, please give me the left operand:";<br /> cin >> iRightNumber;<br /> cout << "nand now please give me the right operand:";<br /> int iAnyNumber;<br /> cin >> iAnyNumber;<br /> Exponent.iResult=iRightNumber ^ iAnyNumber;<br /> cout << "nThe result of " <<<br /> Exponent.iLeftNum << " raised to " << iAnyNumber << "<br /> is:" << Exponent.iResult << endl;<br /> <br /> }<br /> //Exponentor operator^(int num1);<br /> Exponentor Exponentor::operator^(int num1)<br /> {<br /> cout << "In the function";<br /> <br /> return *this;<br /> }<br /> </string></iostream> |
Thanks in advance