Click here to Skip to main content
15,923,197 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
why friend complex sum(complex s1, complex s2); is written?
why not void sum(complex s1, complex s2) ?
why object s3 is declared in sum function and why is it written complex s3?

What I have tried:

C++
#include<iostream>
using namespace std;

class complex
{
	int a,b;
	public:
		void take(int x, int y)
		{
			a=x;
			b=y;
		}
		
		friend complex sum(complex s1, complex s2);
		void display(void)
		{
			cout<<"A is:"<<a<<endl;
			cout<<"B is:"<<b<<endl;
			cout<<"complex number is:"<<a<<"+"<<b<<"i"<<endl;
			cout<<endl;
		}
};

complex sum(complex s1, complex s2)
{
	complex s3;

	s3.a=s1.a+ s2.a;
	s3.b=s1.b + s2.b;
	return s3;
}

int main()
{
	complex c1,c2,c3;
	
	c1.take(4,5);
	c1.display();
	
	c2.take(2,6);
	c2.display();
	
	c3=sum(c1,c2);
	c3.display();
	return 0;
}
Posted
Updated 6-Sep-21 4:53am
v2

Quote:
why
C++
friend complex sum(complex s1, complex s2);
is written?

Start here: friend declaration - cppreference.com[^]
You will probably need to understand classes fairly well before it makes any sense to you though.

Quote:
why not
C++
void sum(complex s1, complex s2)
?

Because if you don't provide a return type you would have to overwrite the values in one of the two parameters passed to the function in order to return the combined value - a function called sum should logically add the parameter values together, and return the combined value. Without a return value, it can't do that.

Quote:
why object s3 is declared in sum function and why is it written complex s3?
Haven't got a clue, can't see that code at all!
 
Share this answer
 
Comments
Member 14897676 6-Sep-21 9:46am    
I have updated the code ..pls check it
OriginalGriff 6-Sep-21 10:07am    
It's declared to give you somewhere to store the result, and something to return!
Seriously, you need to take a couple of steps back and brush up on classes (and C++ basics as well) as this is pretty basic stuff!
Member 14897676 6-Sep-21 9:48am    
but why the return value is complex ? bcs the two parameters are of complex data type?
what if I write return type as void?
OriginalGriff 6-Sep-21 10:05am    
If the return type is void, you can't return anything ...

And the sum of two complex numbers is a complex number. Even if the imaginary parts have equal magnitude and opposite sign (in which case the result is technically a real number) since real numbers are a subset of complex numbers. Since most complex sums won't produce a real value, you need to be able to return a complex value, and you can't "swap types" at run time.
Hence the fucnction is declared as returning a complex value.
CPallini 6-Sep-21 10:53am    
5.
In addition of what our OriginalGriff already said, the arguments are usually passed (for preformance reasones) as const references, namely:

C++
#include<iostream>
using namespace std;

class complex
{
  int a,b;
  public:
    void take(int x, int y)
    {
      a=x;
      b=y;
    }

    friend complex sum(const complex & s1, const complex & s2);

    void display(void)
    {
      cout<<"A is:"<<a<<endl;
      cout<<"B is:"<<b<<endl;
      cout<<"complex number is:"<<a<<"+"<<b<<"i"<<endl;
      cout<<endl;
    }
};

complex sum(const complex & s1, const complex & s2)
{
  complex s3;

  s3.a = s1.a + s2.a;
  s3.b = s1.b + s2.b;

  return s3;
}

int main()
{
  complex c1, c2, c3;

  c1.take(4,5);
  c1.display();

  c2.take(2,6);
  c2.display();

  c3=sum(c1,c2);
  c3.display();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900