Write the implementation of a Complex class in C++ that has the following member
ID: 3585892 • Letter: W
Question
Write the implementation of a Complex class in C++ that has the following member functions:
1-constructor that initializes the complex number by default arguments
2-set function that sets the real part of the complex number
3-set function that sets the imaginary part of the complex number
4-get function that returns the real part of the complex number
5-get function that returns the imaginary part of the complex number
6-a function that displays the complex number
7-A member function to get the sum of two or more complex numbers
8-A non-member function to get the sum of two or more complex numbers
Explanation / Answer
#include<iostream>
#include<stdarg.h>
using namespace std;
class Complex{
private:
int real;
int imaginery;
public:
Complex(){
real = 0;
imaginery = 0;
}
void setReal(int a){
real = a;
}
void setImaginery(int a){
imaginery = a;
}
int getReal(){
return real;
}
int getImaginery(){
return imaginery;
}
Complex add(int count,...){
va_list ap;
va_start(ap,count);
Complex c ;
Complex a;
int rl = 0;
int im = 0;
for (int i = 0; i<count; i++){
a = va_arg(ap,Complex);
rl = rl + a.getReal();
im = im +a.getImaginery();
}
c.setReal(rl);
c.setImaginery(im);
return c;
}
void disp(){
cout << real << " " << imaginery << endl;
}
};
Complex add(int count,...){
va_list ap;
va_start(ap,count);
Complex c ;
Complex a;
int rl = 0;
int im = 0;
for (int i = 0; i<count; i++){
a = va_arg(ap,Complex);
rl = rl + a.getReal();
im = im +a.getImaginery();
}
c.setReal(rl);
c.setImaginery(im);
return c;
}
int main(){
Complex c1,c2,c3,c4;
c1.setReal(2);
c1.setImaginery(3);
cout << "C1 is : ";
c1.disp();
c2.setReal(4);
c2.setImaginery(5);
cout << "C2 is : ";
c2.disp();
c3 = c1.add(2,c2,c1);
cout << "C3 after adding c1 and c2 to it: ";
c3.disp();
c4 = add(2,c1,c2);
cout << "C4 is : ";
c4.disp();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.