complex calculator

This is a discussion on complex calculator within the C++ Programming forums, part of the General Programming Boards category; hi i just have made add function yet for complex calculator but its not working in this function i just ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    41

    Arrow complex calculator

    hi i just have made add function yet for complex calculator but its not working
    in this function i just want to add real in real part and complex in complex part but compilor says
    error C2065: 'o1' : undeclared identifier
    error C2228: left of '.real' must have class/struct/union
    error C2228: left of '.complx' must have class/struct/union
    i think i am missing something kindly guide me here is my code
    Code:
    # include <iostream>
    # include <conio.h>
    # include <windows.h>
    using namespace std;
    class complex
    {
    private:
    	double real;
    	double complx;
    public:
    	complex ()
    	{
    	
    	}// defualt constructor
    	complex (double r1,double r2) 
    	{
    		real=r1;
    		complx=r2;
    	
    	}//end constructor 2
    
    	void add (complex o)
    	{
    		
    		real=o.real+o1.real;
    		complx=o.complx+o1.complx;
    		
    	
    	}//end function
    	
    
    
    };//end class
    int main ()
    {
    	const complex o(2,4),o1(4,6);
    	complex o3;
    	o3.add (o1);
    	getch ();
    	
    
    }//end main
    thanks

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    complex is a standard type you know. I wouldn't redefine it as "complex" if I were you, even though it's technically OK in this instance.

    OK, aside from that, if you call add like this

    o3.add (o1);

    Then that means you can add o1, which becomes o in the function, and the member data within the class. You need another parameter if you wanted to do something differently.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    i have also used
    Code:
    public:
             void add (complex o,complex o1)
    	{
    		complex temp;
    		real=o.real+o1.real;
    		complx=o.complx+o1.complx;
    
    		
    	
    	}//end function
    int main ()
    {
       const complex o(2,4),o1(4,6);
    	complex o3;
    	o3.add (o,o1);
    }
    it works but my requirement is to pass only one object as an argument that is i have failed to do so

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    If you know how return values work you could always add together the object and the argument.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    now i have modified this code like this
    Code:
    class complex
    {
    private:
    	double real;
    	double complx;
    public:
    	complex ()
    	{
    	real=0;
    	complx=0;
    	}// defualt constructor
    	complex (double r1,double r2) 
    	{
    		real=r1;
    		complx=r2;
    	
    	}//end constructor 2
    
    complex add (complex o1)
    	{
    		complex temp;
    		temp.real=o1.real+real;
    		temp.complx=o1.complx+complx;
    		temp.disp ();
    		return (temp);
    
    	}//end function
    void disp ()
    {
    	cout<<real<<"+"<<complx<<"i";
    
    }//end function
    	
    
    
    };//end class
    int main ()
    {
    	 complex o(2,4),o1(4,6);
    	complex o3;
    	o3=o.add (o1);
    	getch ();
    	
    
    }//end main
    now can i have better than that if i want to extend my code for subtraction, multiplication and division
    kindly guide me if i want
    when any operator function is called it displays the result like i have done in above code but i want to use one function to display all results of addition multiplication subtraction and division
    can i do this without using separate display functions for addition subtraction ,division and multiplication?
    Last edited by rafay_07; 11-06-2010 at 05:48 AM.

  6. #6
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Isn't the problem with your original add function that you are attempting to access private class members from outside the class (in this case, from another instance of the same class definition).
    Code:
        void add (complex o)
        {
            
            real=o.real+o1.real;
            complx=o.complx+o1.complx;
            
        
        }//end function
    If you made them public members or alternatively used Getter/Setter methods to access them solve your problems?

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    actually i am also not of this opinion but my requirement is this
    kindly give some example may b its closer to my requirement

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by rafay_07 View Post
    it works but my requirement is to pass only one object as an argument that is i have failed to do so
    Sure. What two things are you adding then?

    IMHO, whiteflags had it right that you'll be wanting to add to the current object. I.e as per +=
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    @ iMalc
    the thing u r talking about is it same as in my last posted code?

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    @Finchie:
    Isn't the problem with your original add function that you are attempting to access private class members from outside the class (in this case, from another instance of the same class definition).
    That's not the problem, and that's not an answer. The problem was that if you called this -- the older version of rafay's code --
    Code:
    	void add (complex o)
    	{
    		
    		real=o.real+o1.real;
    		complx=o.complx+o1.complx;
    		
    	
    	}//end function
    The problem is that o1 is undefined in the function, and the only ways to define it would be to make a new object called o1 (probably not what was wanted since you'd be adding the same thing all the time) or passing in an object called o1. The latter possibility was eschewed by the requirements.

    @rafay:
    the thing u r talking about is it same as in my last posted code?
    No, he was responding to an earlier post of yours where you talked about requirements on add's argument list. He thinks that I was correct when I suggested adding the argument to the object, and the object would be the answer; or you could define it -- as you did later -- so that the object and argument are added and the result is returned as a new object. It's not clear which is correct and both could work, it (possibly) depends on the assignment. I have no way of knowing.

    can i do this without using separate display functions for addition subtraction ,division and multiplication?
    Is that really a requirement? It's hard.

    The real problem with doing that is unless one of the operands contains information pertaining to the operation, you will need to add an argument. Don't do it if you can avoid it.

    If you eschewed functions and overloaded operators for complex, you could then write a function with one argument to do that, but the problem itself is not solved by one function.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    @ whiteflags
    no this is not the requirement i was just trying to shorten my code
    i m clear about u said but for me another problem exists if i want to define a multiplication function
    in similar way like passing an object as arguments and manipulate multiplication i m trying "this" pointer to do this but compiler says "illegal use of this expression" may b i m defining it in a silly way
    yet i have read about "this" pointer is
    for input "cin>>(*this).real"
    for output "cout<<(*this).real"
    but this does not work for me in the multiplication function

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    if i define a multiplication function in this way
    Code:
    {
    public:
              complex multiply (complex o)
    {
               ?
    
    }
    }
    int main ()
    {
              o3=o.multiply (o1);
    }
    in place of question mark what should be the definition of this function i m not posting my code for this function defintion because i think it looks silly

  13. #13
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    i m using this rule for multiplication
    if one complex number is (x+iy) and other complex number is (a+ib) then
    (x+iy)(a+ib)=(ax-by)+(bx+ay)i

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    >> i m clear about u said but for me another problem exists if i want to define a multiplication function
    OK, there is probably a way that you could write this that always works no matter what complex numbers you're doing arithmetic on.

    >> in similar way like passing an object as arguments and manipulate multiplication i m trying "this" pointer to do this but compiler says "illegal use of this expression" may b i m defining it in a silly way
    I'd say you're doing it in a silly way, yeah. Just read into the appropriate variable types for the real and imaginary parts. Then you would take those variables and make a new complex number out of them. Or you could write an input function for complex numbers, and then add. If you go with the latter then the next part will be important.

    >> yet i have read about "this" pointer is
    for input "cin>>(*this).real"
    for output "cout<<(*this).real"
    but this does not work for me in the multiplication function

    OK well, why? Your compiler gives you a specific error doesn't it?

    In any case, the expression (*this).real and real could be the same. You would only write *this if you were resolving an ambiguity. (*this).real refers to the class member real not the local variable, real.

  15. #15
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    did u mean this
    Code:
    void multiply (complex o1)
    {
    		double x,y;
    		x=real* o1.real-complx * o1.real;
    		y=real * o1.complx+complx * o1.real;
    		cout <<x<<"+"<<y<<endl;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Complex Number Class
    By Sephiroth1109 in forum C++ Programming
    Replies: 15
    Last Post: 12-12-2007, 03:46 PM
  2. Why am I getting 'undelcared identifier' ???
    By Bill83 in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2006, 12:00 PM
  3. arithmetic operator friend functions
    By linucksrox in forum C++ Programming
    Replies: 7
    Last Post: 02-06-2006, 10:39 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM
  5. Problem from texbook
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2002, 04:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21