ALLInterview.com :: Home Page            
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Google
   
 
Categories >> Entrance Exams >> Engineering Entrance Exams >> GATE
 
 


 

 
 GATE interview questions  GATE Interview Questions (152)
 IIT JEE interview questions  IIT JEE Interview Questions (30)
Question
c++ program to add 2 complex number using operator
overloading technique
 Question Submitted By :: GATE
I also faced this Question!!     Answer Posted By  
 
Answer
# 1
manish soni tagore collage jaipur
ph:9785261817
e_mail:monupanhar@yahoo.comf;
hapy to help;

#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
struct complex
{
int real;
int img;
};

void getdata(complex &);
complex sum(complex,complex);
complex mult(complex,complex);
void display(complex);

int main()
{
complex c1,c2,c3,c4;
getdata(c1);
getdata(c2);
c3=sum(c1,c2);
cout<<"Sum= ";
display(c3);
c4=mult(c1,c2);
cout<<"MULT= ";
display(c4);
getch();
return 0;
}
complex sum(complex c1,complex c2)
{
complex c;
c.real=c1.real+c2.real;
c.img=c1.img+c2.img;
return c;
}
void display(complex c)
{
cout<<c.real<<"+"<<c.img<<"i"<<endl;


}
complex mult(complex c1,complex c2)
{
complex c;
c.real=c1.real*c2.real-c1.img*c2.img;
c.img=c1.img*c2.real+c1.real*c2.img;
return c;
}
void getdata(complex &c)
{
cout<<"Enter first complex number:"<<endl;
cout<<"Enter real ";
cin>>c.real;
cout<<"Enter img ";
cin>>c.img;
}
 
Is This Answer Correct ?    36 Yes 12 No
Manish Soni
 
Answer
# 2
A.BHA RAJA
THENNAGALAPURI

#include<iostream.h>
#include<conio.h>
class complex
{
int r,i;
public:
{
void getdata(int a,int b)
{
r=a; i=b;
}
complex operator+(complex,complex);
void display(void);
};
complex complex :: operator+(complex a,complex b)
{
complex c;
c.r=a.r+b.r;
c.i=a.i+b.i;
return c;
}
void complex :: display(void)
{
cout<<r<<"+i"<<i<<"\n";
}
void main()
{
complex p,q,r;
int n,m;
clrscr();
cout<<"Enter the real number:";
cin>>n;
cout<<"Enter the imag number:";
cin>>m;
p.getdata(n,m);
cout<<"Enter the real number:";
cin>>n;
cout<<"Enter the imag number:";
cin>>m;
q.getdata(n,m);
r=p+q;
p.display(p);
q.display(q);
r.display(r);
getch();
}
 
Is This Answer Correct ?    26 Yes 9 No
A.bharathi Raja Bsc.,hdca.,
 
 
 
Answer
# 3
#include<iostream.h>
#include<conio.h>
class complex
{
int r;
int i;
public:
complex()
{ }
complex(int a,int b)
{
r=a;i=b;
}
friend complex operator+(complex,complex);
friend show(complex);
complex operator+(complex c1,complex c2)
{
complex c3;
c3.r=c1.r+c2.r;
c3.i=c1.i+c2.i;
return(c3);
}
show(complex c)
{
cout<<c.r<<"i+"<<c.i<<endl;
}
void main()
{
complex a,b,c;
clrscr();
a.complex(3,6);
b.complex(4,7);
c=a+b;
show(a);
show(b);
show(c);
getch()
}
 
Is This Answer Correct ?    23 Yes 9 No
A.bharathi Raja
 
Answer
# 4
#include<iostream.h>
#include<conio.h>
class complex
{
float x;
float y;
public:
complex(){}
complex(float real,float imag)
{
x=real;
y=imag;
}
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp x=x+c.x;
temp y=x+c.y;
return
}
void complex::display(void)
{
cout<<x<<"+j"<<y<<"\n";
}
int main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1=",c1.display();
cout<<"c2=",c2.display();
cout<<"c3=",c3.display();
return 0;
}
 
Is This Answer Correct ?    17 Yes 6 No
Sundari.b.v
 
Answer
# 5
write a c++ program to create class student having
datamember name,Roll_no,age,and branch intilcization all the
member using constructor print the all the details on the
screen.
 
Is This Answer Correct ?    8 Yes 3 No
Arvind
 
Answer
# 6
// Program for complex operation by using operator overloading.
#include<iostream.h>
#include<conio.h>
class complex
{
int r,i;
public:
complex()
{}
void accept();
void display();
friend complex operator +(complex ,complex );
friend complex operator -(complex ,complex );
friend complex operator *(complex ,complex );
//friend complex operator /(complex ,complex );
}; // end of class complex.

void complex::accept()
{
cout<<"\n\n Enter Real & Imaginary part of complex";
cin>>r>>i;
} //end of accept().

void complex::display()
{
cout<<r<<"+"<<i<<"i";
} //end of display().


complex operator +(complex c1,complex c2)
{
complex ans;
ans.r=c1.r+c2.r;
ans.i=c1.i+c2.i;
return(ans);
} //end of complex +()

complex operator -(complex c1,complex c2)
{
complex ans;
ans.r=c1.r-c2.r;
ans.i=c1.i-c2.i;
return(ans);
} //end of complex -()

complex operator *(complex c1,complex c2)
{
complex ans;
ans.r=(c1.r*c2.r)-(c1.i*c2.i);
ans.i=(c1.r*c2.i)+(c1.i*c2.r);
return(ans);
} //end of complex *()

/*complex operator /(complex c1,complex c2)
{
complex ans,t1,t2;

t1.r=(c1.r*c2.r)+(c1.i*c2.i);
t1.i=(c1.i*c2.r)-(c1.r*c2.i);

t2.r=(c2.r*c2.r)+(c2.i*c2.i);
t2.i=0;

ans.r=t1.r/t2.r;
ans.i=t1.i/t2.i;
return(ans);
} //end of complex /() */


void main()
{
int ch;
complex c1,c2,c3;
clrscr();
cout<<"\n Enter first complex no";
c1.accept();
c1.display();
cout<<"\n Enter second complsx no";
c2.accept();
c2.display();

cout<<"\n\n~~~~~MENU~~~~~";
cout<<"\n 1. ADDATION ";
cout<<"\n 2. SUBTRACTION ";
cout<<"\n 3. MULTIPLACTION ";
cout<<"\n 4. DIVISION ";
cout<<"\n 5. EXIT ";
cout<<"\n\n Enter your choice = \t";
cin>>ch;

switch(ch)
{
case 1:
{
c3=c1+c2;
cout<<"\n ADDATION=";
c3.display();
} // end case 1.

case 2:
{
c3=c1-c2;
cout<<"\n SUBTRACTION=";
c3.display();
} // end case 2.

case 3:
{
c3=c1*c2;
cout<<"\n MULTIPLACTION=";
c3.display();
} // end case 3.

/* case 4:
{
c3=c1/c2;
cout<<"\n DIVISION=";
c3.display();
} // end case 4. */
} //end of switch case.

getch();
} //End of main ().
 
Is This Answer Correct ?    5 Yes 1 No
Aalok
 
Answer
# 7
Write a C++ program to create a class called COMPLEX and
implement the following overloading functions ADD that
return a COMPLEX number .
I. ADD(a,s2) - where a is an integer(real part) and s2 is a
complex number.
II. ADD(s1,s2) - where s1 and s2 are complex objects.


#include<iostream.h>
#include<conio.h>
class complex
{
int r,i;
public:
void read();
void print();
friend complex add(int a,complex c);
friend complex add(complex c1,complex c2);
};
void complex::read()
{
cout<<"Enter real and imaginary\n";
cin>>r>>i;
}
void complex::print()
{
cout<<r<<"+i"<<i<<endl;
}
complex add(int a,complex c)
{
complex t;
t.r=a+c.r;
t.i=c.i;
return t;
}
complex add(complex c1,complex c2)
{
complex t;
t.r=c1.r+c2.r;
t.i=c1.i+c2.i;
return t;

}
void main()
{
int a=2;
clrscr();
complex s1,s2,s3;
s1.read();
cout<<"\ns1 : ";
s1.print();
s2=add(a,s1);
cout<<"s2 : 2+s1\n";
cout<<" : ";
s2.print();
s3=add(s1,s2);
cout<<"s3=s1+s2\n";
cout<<"s1 : ";
s1.print();
cout<<"s2 : ";
s2.print();
cout<<"s3 : ";
s3.print();
getch();
}


MORE C++ PROGRAMS AND SOLUTIONS:
1. ADDING 'TIME' OBJECTS AND DISPLAYING THE RESULT IN
HH:MM:SS FORMAT USING MEMBER FUNCTION

2. OPERATOR OVERLOADING - ADDITION, SUBTRACTION OF MATRICES
USING OPERATOR OVERLOADING

3. STACK IMPLEMENTATION USING OPERATOR OVERLOADING IN C++

http://programscpp.blogspot.in/
 
Is This Answer Correct ?    3 Yes 1 No
Binoy
 
Answer
# 8
#include<iostream>
using namespace std;

class complex_number
{
int real_part;
int imaginary_part;
public:
void setData(int val,int flag)
{
real_part = 0;
imaginary_part = 0;
if(flag == 0)
{
real_part = val;
imaginary_part = 0;
}
else if(flag == 1)
{
real_part = 0;
imaginary_part = val;
}
}
void setData(int rp, int ip, int flag)
{ real_part = 0;
imaginary_part = 0;
if(flag == -1)
{
real_part = rp;
imaginary_part = ip;
}
}
void show()
{ if(imaginary_part>=0)
cout<<"Complex Number
is: "<<real_part<<"+"<<imaginary_part<<"i"<<endl;
else
cout<<"Complex Number
is: "<<real_part<<imaginary_part<<"i"<<endl;
}
complex_number operator+ (complex_number op2);
complex_number operator- (complex_number op2);
complex_number operator++();
complex_number operator++(int x);
};

complex_number complex_number:: operator+(complex_number
op2)
{
complex_number temp;
temp.real_part = op2.real_part + real_part;
temp.imaginary_part = op2.imaginary_part +
imaginary_part;
return temp;
}

complex_number complex_number:: operator-(complex_number
op2)
{
complex_number temp;
temp.real_part = real_part - op2.real_part;
temp.imaginary_part = imaginary_part -
op2.imaginary_part;
return temp;
}

complex_number complex_number:: operator++()
{
real_part++;
imaginary_part++;
return *this;
}
complex_number complex_number:: operator++(int x)
{
++real_part;
++imaginary_part;
return *this;
}
int main()
{
complex_number ob1;
ob1.setData(5,0);

complex_number ob2;
ob2.setData(10,1);
//cout<<"Before addition"<<ob2.show();
complex_number ob3;
ob3.setData(2,3,-1);
complex_number ob4;
ob4.setData(4,5,-1);
complex_number ob5;
cout<<"Before addition, complex numbers are :"<<endl;
ob4.show();
ob3.show();
ob5 = ob4+ob3;
cout<<"After Addition";
ob5.show();
cout<<"Before addition, complex numbers are :"<<endl;
ob1.show();
ob4.show();
ob5 = ob1 + ob4;
cout<<"After Addition";
ob5.show();
cout<<"Before addition, complex numbers are :"<<endl;
ob2.show();
ob4.show();
ob5 = ob2+ ob4;
cout<<"After Addition";
ob5.show();
cout<<"Before preincrement"<<endl;
ob5.show();
++ob5;
cout<<"After preincrement"<<endl;
ob5.show();
cout<<"Before postincrement"<<endl;
ob5.show();
ob5++;
cout<<"After postincrement"<<endl;
ob5.show();
cout<<"Before Subtraction, complex numbers are :"<<endl;
ob4.show();
ob3.show();
ob5 = ob4-ob3;
cout<<"After Subtraction";
ob5.show();
cout<<"Before Subtraction, complex numbers are :"<<endl;
ob1.show();
ob4.show();
ob5 = ob1 - ob4;
cout<<"After Subtraction";
ob5.show();
cout<<"Before Subtraction, complex numbers are :"<<endl;
ob2.show();
ob4.show();
ob5 = ob2- ob4;
cout<<"After subtraction";
ob5.show();


int i;
cin>>i;
return 0;
}
 
Is This Answer Correct ?    77 Yes 76 No
Varsha
 
Answer
# 9
It is a correct program,thanks a lot........... 
Is This Answer Correct ?    14 Yes 15 No
Vishnu Tiwari,cochin Universit
 
Answer
# 10
i got a correct answer this will help me alot thanks 
Is This Answer Correct ?    29 Yes 37 No
Waleed
 

 
 
 
Other GATE Interview Questions
 
  Question Asked @ Answers
 
What is the optimum frequency of an electronic choke?   1
What is the broadcast address of the subnet address 192.168.10.33 255.255.255.248 ?   3
A can have a piece of work done in 8 days,B can ork three times faster than A,C can work five times faster than A.how many days will they take to do the work together? TCS 1
ZINDAL STEEL EXAM PAPER FOR PRODUCTION ENGG. Jindal 1
1)what is the abbreviation for DGFS/OCGS for bark written test? 2)how can we know, we r in which category? BARC 1
hi i need to no when is tancet notification is going to apper and where .please send the information to my id satish_don334@yahoo.co.in   5
if the polynomial x^4-6x^3+16x^2-25x+10 is divided by another polynomial x^2-2x+k the reminder is x+a. Find k and a.   2
Graduate Aptitude Test in Engineering ( GATE ) Question Paper - 2003 COMPUTER SCIENCE & ENGINEERING   5
how many key in key board   5
EVOLUTION OF SBI   1
what reason to change compnay ?   3
Who was the first writer in english to be awarded the sahitya akademi Award in 1961? JNU 3
 
For more GATE Interview Questions Click Here 
 
 
 
 
 


   
Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
   
Copyright 2013  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  KalAajKal.com