Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://pastebin.com/u/TheWhiteFang
- // Tutorial 7 Q2 [Member Function method]
- #include <iostream>
- using namespace std;
- class ComplexNum{
- float m_Real;
- float m_Img;
- public:
- ComplexNum(float inReal = 0.0, float inImg = 0.0){// To be completed
- m_Real = inReal;
- m_Img = inImg;
- }
- ComplexNum operator* (const ComplexNum &); //member function overload
- friend ostream &operator<<(ostream &output, const ComplexNum &source);
- };
- ComplexNum ComplexNum ::operator*(const ComplexNum &p) {
- return ComplexNum(((m_Real * p.m_Real) - (m_Img*p.m_Img)), ((m_Real * p.m_Img)+(m_Img * p.m_Real)));
- }
- //top level function
- //ComplexNum operator* (const ComplexNum &inVal1, const ComplexNum &inVal2)
- //{
- // ComplexNum temp;
- // temp.m_Real = (inVal1.m_Real * inVal2.m_Real) - (inVal1.m_Img * inVal2.m_Img);
- // temp.m_Img = (inVal1.m_Real * inVal2.m_Img) + (inVal1.m_Img * inVal2.m_Real);
- //
- //
- // return temp;
- //
- //}
- //top level global to overload stream operator
- ostream &operator<<(ostream &output, const ComplexNum &source){ //return ostream by reference //only thing that changes is classname
- output << source.m_Real << " + " << source.m_Img << "i " << endl;
- return output;
- }
- int main(){
- ComplexNum obj1(10.5, 20.8);
- ComplexNum obj2(31.8, 23.6);
- cout << "Complex Num 1: " << obj1 << endl;
- cout << "Complex Num 2: " << obj2 << endl;
- ComplexNum obj3;
- obj3 = obj1 * obj2;
- cout << endl << "Complex Num 1 × Complex Num 2 = ";
- cout << obj3 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement