Thread: Overloading in C++

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Overloading in C++

    So I have to do a program overloading the operators >>, <<, +, –, *, and /. I am quite an amatuer at doing this and I was wondering whether someone could help me...I looked at previous threads on this and tried using it but I use putty and it doesn't really work in there. I need to use outFile and get the numbers from inFile if someone could please help me out it would be greatly appreciated
    It needs to do this

    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

    Input Sample:

    2.5 -2.2
    1.0 1.0

    Output Sample:

    < *~ Complex numbers operations ~*>

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

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

    < end >

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The general rule here is that you do the coding, and we try to give you good advice when you get stuck.

    What have you done so far?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    well.. i really don't know where to start
    besides
    #include <iostream>
    using namespace std;
    int main ()




    i know that I am supposed to make a class
    so I got something like this

    #include <iostream>
    #include <iomanip>
    #include <fstream>


    using namespace std;

    class complex
    public:
    void readnums(ifstream& inFile); //i don't know if this counts for the << operator
    void divide
    void multiply
    void subtract
    void addition
    void print (ofstream& outFile)//again i don't know if this counts for the >> operator
    private:
    float a;
    float b;
    float c;
    float d;
    but like i said i am a real beginnier and am so lost on overloading

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    i was going to use this code but i don't know how to get it to print to the outFile and read from the inFile ... I tried but i keep getting so many errors
    Code:
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    
    using namespace std;
    class complex
    {
    float real;
    float imag;
    public:
    complex()
    {}
    complex(float x,float y)
    {
    real=x;
    imag=y;
    }
    complex operator + (complex);
    complex operator - (complex);
    complex operator * (complex);
    complex operator / (complex);
    void display(void)
    {
    cout << real<< " +i" << imag<< endl;
    }
    
    };
    
    complex complex :: operator +(complex c)
    {
    complex c2;
    c2.real=real+c.real;
    c2.imag=imag+c.imag;
    return (c2);
    }
    
    complex complex :: operator -(complex c)
    {
    complex c2;
    c2.real=real-c.real;
    c2.imag=imag-c.imag;
    return (c2);
    }
    complex complex :: operator *(complex c)
    {
    complex c2;
    c2.real = ((real * c.real) - (imag * c.imag));
    c2.imag = ((real * c.imag) + (imag * c.imag));
    return (c2);
    }
    complex complex :: operator /(complex c)
    {
    complex c2;
    c2.real=((real * c.real) + (imag * c.imag))/((real * c.real) + (imag * c.imag));
    c2.imag=((imag * c.real) - (real * c.imag))/((real * c.real) + (imag * c.imag));
    return (c2);
    }
    
    int main()
    {
    complex c1,c2,c3;
    int op;
    char ch,y,Y;
    
    c1 = complex(5.6,2.7);
    c2 = complex(3.5,5.6);
    cout<< "Two Complex numbers Are :"<< endl;
    c1.display();
    c2.display();
    do
    {
    cout<< endl<< "******** MENU *********"<< endl;
    cout<< "1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5.Exit"<< endl;
    cout<< "Enter Your Choice : ";
    cin>>op;
    switch(op)
    {
    case 1:
    c3 = c1 + c2;
    cout<< "Addition of Two complex Nos. :";
    c3.display();
    break;
    case 2:
    c3 = c1 - c2;
    cout<< "Subtraction of Two complex Nos. :";
    c3.display();
    break;
    
    case 3:
    c3 = c1 * c2;
    cout<<" Multiplication of Two complex Nos. :";
    c3.display();
    break;
    
    case 4:
    c3 = c1 / c2;
    cout<< "division of Two complex Nos. :";
    c3.display();
    break;
    
    case 5:exit(0);
    
    default: cout<< endl<< "Aborting!!!!!!!INVALID CHOICE"<< endl;
    }
    cout<< " Do you want to continue(Y/y)";
    cin>>ch;
    }
    while(ch=='y'||ch=='Y');
    return 0;
    }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Would you PLEASE indent the code?
    Also note that the operators should take a const reference to its arguments, if you know what that means.
    Would you also care to elaborate on what is wrong and what you expect to happen?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 04:10 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 08:49 PM