Operator Overloading Question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Operator Overloading Question

  1. #1
    Join Date
    Dec 2008
    Posts
    1

    Operator Overloading Question

    Im new to this site but it seemed like a good place for programming questions.

    This is the assignment I am working on:

    Design, implement, and test a class “Complex” which uses the followings overloaded operators for complex number: >>, <<, +, –, *, and /.

    The complex number is described as having a real component or real part a and an imaginary component or imaginary part b where i2 = -1: A = a + bi

    Given the two complex numbers A and B then the following definition will help to overload the operators. For any pair of complex numbers apply all overloaded operators.

    A = a + bi
    B = c + di

    >> It extracts two part of complex number.
    << It inserts the complex number with (a) + (b) i format.
    + A+B = (a + c) + (b + d)i
    - A - B = (a - c) + (b - d)i
    * A* B = (ac - bd) + (bc + ad)i
    / A / B = (ac + bd)/(c2 + d2)+ (bc -ad)/(c2 + d2)i

    Plan 3P-I/O & Note (only for files) and test data for client program.

    Input Sample:

    2.5 -2.2
    1.0 1.0

    Output Sample:

    A = (2.5) + (-2.2) i
    B = (1.0) + (1.0) i

    A + B = (3.5) + ( -1.2) i
    A - B = .. . .
    A * B = .. . .
    A / B = .. . .


    I just dont really understand the whole operator overloading thing and was wondering if anyone had any pointers to completing this assignment.

  2. #2
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Operator Overloading Question

    All the operators you have to write should be written as nonmember friend functions.

    Operator overloading is just syntactic sugar for function calls.

    Assuming correct class design and a class complex_num....

    friend complex_num operator + ( const complex_num& lhs, const complex_num& rhs )
    { /* code here for adding two complex_nums together */ }

    should be in your class for each operator you have to overload. The << and >> are slightly different.....

    friend std::ostream& operator << ( std::ostream& lhs, const complex_num& rhs )
    { /* code here to print out your complex_num then return lhs */}

    friend std::istream& operator >> ( std::istream& lhs, complex_num& rhs )
    { /* code here to input your complex_num then return lhs */}

    now for instance when you write...
    Code:
    complex_num a( 4,3 ) , b ( 5,2 ) , result;
    result = a + b; // this line calls operator +(complex_num, complex_num)
    see now?
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,276

    Re: Operator Overloading Question

    Quote Originally Posted by smcheval
    All the operators you have to write should be written as nonmember friend functions.
    That is true, unless you have member functions to get the real and imaginary portions separately, in which case all the operators can be non-member non-friend functions. Or, if you are allowed to add your own operators, then implement operator+=, operator-=, operator*= and operator/= as member functions, then implement operator+, operator-, operator* and operator/ as non-member non-friend functions in terms of the respective assignment operators.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center