0% found this document useful (0 votes)
18 views5 pages

Assignmentx

C programs

Uploaded by

namishkhotre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Assignmentx

C programs

Uploaded by

namishkhotre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Write a C++ program to perform addition of two complex numbers using the concept of

constructor overloading and friend function.

Program:

#include<iostream>

using namespace std;

class Complex {
private:
float real;
float imag;

public:
Complex() {
real = 0.0;
imag = 0.0;
}

Complex(float r, float i) {
real = r;
imag = i;
}

// Copy Constructor (highlighted)


Complex(const Complex& other) {
real = other.real;
imag = other.imag;
}

friend Complex addComplex(const Complex& num1,


const Complex& num2);
void display() {
if (imag < 0)
cout << real << " - " << -imag << "i" <<
endl;
else
cout << real << " + " << imag << "i" <<
endl;
}
};

Complex addComplex(const Complex& num1, const Complex&


num2) {
float realSum = num1.real + num2.real;
float imagSum = num1.imag + num2.imag;
return Complex(realSum, imagSum);
}

int main() {
float real1, imag1, real2, imag2;

cout << "Enter the real and imaginary parts of the


first complex number: ";
cin >> real1 >> imag1;

cout << "Enter the real and imaginary parts of the


second complex number: ";
cin >> real2 >> imag2;

Complex num1(real1, imag1);


Complex num2(real2, imag2);

Complex sum = addComplex(num1, num2);


cout << "Sum of the two complex numbers: ";
sum.display();

return 0;
}

Output:

Enter the real and imaginary parts of the first complex number: 2

Enter the real and imaginary parts of the second complex number: 2

Sum of the two complex numbers: 4 + 4i

Write a program in to find the Sum of the series using Constructor

Overloading in C++.

Program code:

#include <iostream>
#include <cmath>

class SeriesSum {
private:
double sum;

public:
SeriesSum() {
sum = 0;
}
SeriesSum(int firstTerm, int commonDifference, int
numberOfTerms) {
sum = 0;
for (int i = 0; i < numberOfTerms; i++) {
sum += firstTerm + i * commonDifference;
}
}

SeriesSum(int firstTerm, double commonRatio, int


numberOfTerms, bool isGeometric) {
sum = 0;
if (isGeometric) {
for (int i = 0; i < numberOfTerms; i++) {
sum += firstTerm * pow(commonRatio,
i);
}
} else {
for (int i = 0; i < numberOfTerms; i++) {
sum += firstTerm + i * commonRatio;
}
}
}

void displaySum() {
std::cout << "Sum: " << sum << std::endl;
}

~SeriesSum() {
}
};

int main() {
SeriesSum arithSeries1(1, 2, 10);
arithSeries1.displaySum();
SeriesSum geoSeries1(2, 2.0, 5, true);
geoSeries1.displaySum();

return 0;
}

Output:

Sum: 100

Sum: 62

You might also like