0% found this document useful (0 votes)
23 views8 pages

(CSE-225) Lecture-4 (C++ Part-2)

This document discusses complex numbers in C++ using classes. It shows how to define a Complex class with real and imaginary members. Methods like the constructor, addition, and printing are demonstrated. Later, it covers friend functions and operator overloading to add complex numbers using the + operator instead of a separate Add method. The main function shows creating Complex objects, adding them, and printing the result.

Uploaded by

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

(CSE-225) Lecture-4 (C++ Part-2)

This document discusses complex numbers in C++ using classes. It shows how to define a Complex class with real and imaginary members. Methods like the constructor, addition, and printing are demonstrated. Later, it covers friend functions and operator overloading to add complex numbers using the + operator instead of a separate Add method. The main function shows creating Complex objects, adding them, and printing the result.

Uploaded by

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

Lecture 04

C++ Primer
CSE225: Data Structures and Algorithms
A Complex Number Example
class Complex
{ Complex Complex::Add(Complex c)
public: {
Complex(); Complex t;
Complex(double, double); t.Real = Real + c.Real;
Complex Add(Complex); t.Imaginary = Imaginary + c.Imaginary;
void Print(); return t;
private: }
double Real, Imaginary; void Complex::Print()
}; {
cout << Real << endl;
Complex::Complex() cout << Imaginary << endl;
{ }
Real = 0;
Imaginary = 0;
} int main(void)
{
Complex::Complex(double r, double i) Complex A(1,1), B(2,3);
{ Complex C;
Real = r; C = A.Add(B);
Imaginary = i; C.Print();
} return 0;
}
Friend Function
▪ The function has access to the private members in the
class declaration
▪ The function is not in the scope of the class
Friend Function
class Complex
{
public:
Complex();
Complex(double, double);
void Print();
friend Complex AddComplex(Complex, Complex);
private:
double Real, Imaginary;
};

Complex::Complex()
{
Real = 0;
Imaginary = 0;
}

Complex::Complex(double r, double i)
{
Real = r;
Imaginary = i;
}
Friend Function
void Complex::Print()
{
cout << Real << endl;
cout << Imaginary << endl;
}

Complex AddComplex(Complex a, Complex b)


{
Complex t;
t.Real = a.Real + b.Real;
t.Imaginary = a.Imaginary + b.Imaginary;
return t;
}

int main(void)
{
Complex A(1,1), B(2,3);
Complex C;
C = AddComplex(A, B);
C.Print();
return 0;
}
Operator Overloading
▪ We can use some operator symbols to define special member
functions of a class

▪ Provides convenient notations for object behaviors


Operator Overloading
class Complex
{
public:
Complex();
Complex(double, double);
Complex operator+(Complex);
void Print();
private:
double Real, Imaginary;
};

Complex::Complex()
{
Real = 0;
Imaginary = 0;
}

Complex::Complex(double r, double i)
{
Real = r;
Imaginary = i;
}
Operator Overloading
Complex Complex::operator+(Complex a)
{
Complex t;
t.Real = Real + a.Real;
t.Imaginary = Imaginary + a.Imaginary;
return t;
}

void Complex::Print()
{
cout << Real << endl;
cout << Imaginary << endl;
}

int main(void)
{
Complex A(1,1), B(2,3);
Complex C;
C = A + B;
C.Print();
return 0;
}

You might also like