Write a program in C++ to create a structure named complex having data member real and imag. Create member function add_complex which takes structure as an argument and return structure. Using function add two complex numbers.
Introduction
In this code example, we will explore how to perform addition operations on complex numbers using C++. Complex numbers consist of a real part and an imaginary part. We will define a structure to represent complex numbers and create functions to input, display, and add complex numbers.
Key Concepts
Before diving into the code, let's understand some key concepts related to complex numbers:
- Complex Numbers: Complex numbers are numbers that consist of a real part and an imaginary part. They are represented in the form a + bi, where a is the real part and b is the imaginary part.
- Structure: In C++, a structure is a user-defined data type that allows you to group different types of variables under a single name. In this code, we will define a structure named complex to represent complex numbers.
- Member Functions: Member functions are functions that are defined inside a structure or class. They can access the data members of the structure or class. In this code, we will define member functions to input and display complex numbers.
- Function Overloading: Function overloading allows us to define multiple functions with the same name but different parameters. In this code, we will define two functions with the same name add, but with different parameter types to perform addition operations on complex numbers.
Code Structure
The code consists of the following sections:
- Structure Definition: We define a structure named complex with two data members: real and imag. We also define two member functions: get_data() to input the values of real and imag, and display() to display the complex number.
- Function Declaration: We declare a function named add that takes two complex type arguments and returns a complex type value. This function will be used to add two complex numbers.
- Main Function: In the main() function, we create three complex type variables: comp1, comp2, and add_comp. We call the get_data() function to input the values of comp1 and comp2. We then call the add() function to add comp1 and comp2, and store the result in add_comp. Finally, we display the addition result using the display() function.
Code Examples
Let's take a closer look at the code examples to understand how complex number addition is implemented in C++:
C++
#include<iostream> using namespace std; struct complex //Structure defined { float real; float imag; //data members void get_data() //member function definition { cout<<"Enter the real value : "; cin>>real; cout<<"Enter the imag value : "; cin>>imag; } void display() //member function definition { cout<<real<<"+"<<imag<<"i"; } }; complex add(complex,complex); //function having comlpex type arguments complex add(complex c1,complex c2) { complex c; c.real = c1.real + c2.real; //adding real parts and storing in c.real c.imag = c1.imag + c2.imag; //adding imag parts and storing in c.imag return c; //returning a value of type complex } int main() { complex comp1,comp2,add_comp; comp1.get_data(); //data members initilization comp2.get_data(); add_comp = add(comp1,comp2); //calling the function add and passing comp1 and comp2 cout<<endl<<"Addition of : "; comp1.display(); cout<<" and "; comp2.display(); cout<<" is : "; add_comp.display(); return 0; }
Conclusion
In this code example, we learned how to perform addition operations on complex numbers using C++. We defined a structure to represent complex numbers and created member functions to input and display complex numbers. We also implemented a function to add two complex numbers. By understanding this code, you can now perform addition operations on complex numbers in your C++ programs.