Returning Object

Discussion in 'Computer Science & Culture' started by S.Tarafdar, Aug 11, 2003.

Thread Status:
Not open for further replies.
  1. S.Tarafdar Registered Senior Member

    Messages:
    35
    I have this c++ code:
    Code:
    #include&ltiostream&gt
    using namespace std;
    
    class Complex{
    	float real;
    	float imaginary;
    public:
    	void getvalue(float,float);
    	void putvalue();
    	Complex sum(Complex,Complex);
    };
    
    void Complex::getvalue(float re,float im){
    	real=re;
    	imaginary=im;
    }
    
    void Complex::putvalue(){
    	cout&lt&ltreal&lt&lt"+j"&lt&ltimaginary&lt&ltendl;
    }
    
    Complex Complex:: sum(Complex x,Complex y){ 
    	Complex p;
    	p.real=x.real+y.real;
    	p.imaginary=x.imaginary+y.imaginary;
    
    	return p;
    }
    
    int main(){
    	Complex x,y,z;
    	x.getvalue(2,8);
    	y.getvalue(4,9);
    	z=sum(x,y);
    	z.putvalue();
    
    	return 0;
    }
    
    I want to return p from 'sum' method.But the compiler shows error.Please suggest.
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. okinrus Registered Senior Member

    Messages:
    2,669
    I think you want to do
    Code:
    #include<iostream>
    using namespace std;
    
    class Complex{
      float real;
      float imaginary;
     public:
      void getvalue(float,float);
      void putvalue();
      static Complex sum(Complex,Complex);
    };
    
    
    Complex Complex::sum(Complex x,Complex y){
      Complex p;
      p.real=x.real+y.real;
      p.imaginary=x.imaginary+y.imaginary;
    
      return p;
    }
    
    //....
    
    int main(){
      Complex x,y,z;
      x.getvalue(2,8);
      y.getvalue(4,9);
      z = Complex::sum(x,y);
      z.putvalue();
    
      return 0;
    }
    
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. bigjnorman Registered Senior Member

    Messages:
    158
  6. Google AdSense Guest Advertisement



    to hide all adverts.
Thread Status:
Not open for further replies.

Share This Page