Programming

Define Class Named Complex with Two Data Members Real and Imaginary

Write a Program to defining a class named complex with two data members real and imaginary. Use necessary member functions for input/output and define a member function that adds the two complex objects and return object. Also display the result using the member function display ().

Source Code

#include<iostream.h>
#include<conio.h>
class complex
{
   int real;
   int imag;
   float r,i;
   public:
      void inp()
      {
                              cout<<"\nEnter the real component a : ";
                                             cin>>real;
                              cout<<"Enter the imaginary component b : ";
                                             cin>>imag;
      }
      void calc()
      {
               r=r+real;
                 i=i+imag;
      }
      void disp()
      {
               cout<<"\nThe sum of two inputs is of the form: "<<r<<"+"<<"i"<<i;
      }
};
void main()
{
   complex c;
cout<<"\nEnter the values for first input: \n";
               c.inp();
               c.calc();
   cout<<"\nEnter the value for second input: ";
                c.inp();
 c.calc();
   cout<<"\nThe summed output is: ";
               c.disp();
   getch();
}

 

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.