Online C Compiler | Online C++ Compiler

bOtskOOl Online C++ Compiler

Online C Compiler | Online C++ Compiler - Login Required


You need to login or register in order to access our Free Online C Compiler | Online C++ Compiler.

Online Compiler - Register for free

 

 

Some recent program(s) compiled by bOtskOOl user(s) :-

Comments

Member since:
18 February 2009
Last activity:
20 weeks 3 days

Complex Numbers class in C++

 

#include <cmath>

#include <iostream>

#include <iomanip.h>


using namespace std;



class complex

{

     private:

                  float real;               // Real Part

            float imag;      //  Imaginary Part



   public:

          complex(float,float);

          complex(const complex&);

          complex operator +(complex);

          complex operator -(complex);

          complex operator *(complex);

          complex operator /(complex);

          complex getconjugate();

          complex getreciprocal();

          float getmodulus();

          void setdata(float,float);

          void getdata();

          float getreal();

          float getimaginary();

          bool operator ==(complex);

          void operator =(complex);

          friend ostream& operator <<(ostream &s,complex &c);

};

//                                        CONSTRUCTOR

                  complex::complex(float r=0.0f,float im=0.0f)

            {

                 real=r;

               imag=im;

            }


//                                 COPY CONSTRUCTOR

            complex::complex(const complex &c)

            {

                 this->real=c.real;

               this->imag=c.imag;

            }



            void complex::operator =(complex c)

            {

               real=c.real;

               imag=c.imag;

            }



            complex complex::operator +(complex c)

            {

                 complex tmp;

               tmp.real=this->real+c.real;

               tmp.imag=this->imag+c.imag;

               return tmp;

            }


            complex complex::operator -(complex c)

            {

                 complex tmp;

               tmp.real=this->real - c.real;

               tmp.imag=this->imag - c.imag;

               return tmp;

            }


                    complex complex::operator *(complex c)

            {

                 complex tmp;

               tmp.real=(real*c.real)-(imag*c.imag);

               tmp.imag=(real*c.imag)+(imag*c.real);

               return tmp;

            }


            complex complex::operator /(complex c)

            {

                 float div=(c.real*c.real) + (c.imag*c.imag);

               complex tmp;

               tmp.real=(real*c.real)+(imag*c.imag);

               tmp.real/=div;

               tmp.imag=(imag*c.real)-(real*c.imag);

               tmp.imag/=div;

               return tmp;

            }


            complex complex::getconjugate()

            {

                 complex tmp;

               tmp.real=this->real;

               tmp.imag=this->imag * -1;

               return tmp;

            }


            complex complex::getreciprocal()

            {

                 complex t;

               t.real=real;

               t.imag=imag * -1;

               float div;

               div=(real*real)+(imag*imag);

               t.real/=div;

               t.imag/=div;

               return t;

            }


            float complex::getmodulus()

            {

                 float z;

               z=(real*real)+(imag*imag);

               z=sqrt(z);

               return z;

            }


            void complex::setdata(float r,float i)

            {

                 real=r;

               imag=i;

            }


            void complex::getdata()

            {

                 cout<<"Enter Real:";

               cin>>this->real;

               cout<<"Enter Imaginary:";

               cin>>this->imag;


            }


            float complex::getreal()

            {

                 return real;

            }


            float complex::getimaginary()

            {

                 return imag;

            }


            bool complex::operator ==(complex c)

            {

             return (real==c.real)&&(imag==c.imag) ? 1 : 0;

             }


              ostream& operator <<(ostream &s,complex &c)

            {

                 s<<"Real Part = "<<c.real<<endl<<"Imaginary Part = "<<c.imag<<endl;

               s<<"z = "<<c.real<<setiosflags(ios::showpos)<<c.imag<<"i"<<endl<<resetiosflags(ios::showpos);

                return s;

            }




int main()

{

     complex a(10.0f,-2.f); // Calls Constructor

   cout<<a<<endl;               // Calls the overloaded << operator

   complex b(-2);         // Calls Constructor

   complex c=b;                    // Calls Copy Constructor

   c=a;                                   // calls overloaded = operator

   b.getdata();                    // Calls Getdata()

   c.getdata();

   if(b==c)            // calls overloaded == operator

      cout<<"\nb == c";

      else

      cout<<"\nb != c";



   cout<<endl<<c.getmodulus()<<endl; // calls getmodulus function()

   complex d;

   d=a+b;   

   cout<<d<<endl;

   

   d=a-b;     

   cout<<d<<endl;

   d=a*b;     

   cout<<d<<endl;

   d=a/b;     

   cout<<d<<endl;



   return 0;

}

Input - 

Output -

Complex Numbers class in C++

Founder and Admin

Member since:
30 November 2009
Last activity:
4 years 24 weeks

how to create a class to represent a complex nos overload +and *operator to addand multiple complex nos?

 

Member since:
30 November 2009
Last activity:
4 years 24 weeks

thanks it was correct

Member since:
18 February 2009
Last activity:
20 weeks 3 days

Hello ramya,

Here is your corrected code -

#include<iostream.h>

#include<string.h>

template<class t>

int sort(t*a,int n)

{

int flag=1,i,j;

t temp;

for(i=0;i<n-1;i++)

{

for(j=0;j<(n-1-i);j++)

{

if(a[j]>a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

flag=0;

}

}

if(flag)

break;

}

}

int main()

{

int a[5],n,i;

cout<<"\nenter the size of the array";

cin>>n;

cout<<"\nenter the elements of integer array ";

for(i=0;i<n;i++)

cin>>a[i];

cout<<"\nelements of the unsorted array are:"<<endl;

for(i=0;i<n;i++)

cout<<a[i]<<"\n";

sort(a,n);

cout<<"elements of the sorted array array:"<<endl;

for(i=0;i<n;i++)

cout<<a[i]<<"\n";

} 

Input - 

Output - 

Founder and Admin

Member since:
28 November 2009
Last activity:
4 years 42 weeks

an error in this to

#include<iostream.h>
#include<string.h>
template<class t>
int sort(t*a,int n)
{
int flag=1,i,j;
t temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<(n-1-i);j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=0;
}
}
if(flag)
break;
}
}
int main()
{
int a[10],n,i;
cout<<"enter the size of the array";
cin>>n;
cout<<"enter the elements of integer array ";
for(i=0;i>n;i++)
cout<<a[i];
cout<<"elements of the unsorted array are:"<<endl;
for(i=0;i>n;i++)
cout<<a[i]<<"";
sort(a,n);
cout<<"elements of the sorted array array:"<<endl;
}

 

Note:- This post has been edited by ramya at Mon, 2009-11-30 16:57.

Member since:
18 February 2009
Last activity:
20 weeks 3 days

yes sure  just keep posting your doubts.. Happy Coding!  

Founder and Admin

Member since:
28 November 2009
Last activity:
4 years 42 weeks

thanks  and pls help me with many more programs too

Member since:
18 February 2009
Last activity:
20 weeks 3 days

Hello ramya,

I have corrected your program. I have highlighted the necessary changes. You had declared area (and perimeter also) both as a variable and as a function. Check this out -

#include<iostream.h>

#include<string.h>

class shape

{

protected:

float inp_perimeter;

float inp_area;

public:

 void area();

 void perimeter();

 void show();

 };

 class rectangle:public shape

 {

 float length;

 float breadth;

 public:

 rectangle()

 {

 cout<<"\nenter the length";

 cin>>length;

 cout<<"\nenter the breadth";

 cin>>breadth;

 }

void area()

{

inp_area=length*breadth;

}

void perimeter()

{

inp_perimeter=2*(length+breadth);

}

void show()

{

cout<<"\narea of rectangle = "<<inp_area;

cout<<"\nperimeter of rectangle = "<<inp_perimeter;

}

 };

int main()

 {

 rectangle r;

 rectangle *ptr[3]={&r,&r,&r};

 int i;

 for(i=0;i<3;i++)

 ptr[i]->area();

 for(i=0;i<3;i++)

 ptr[i]->perimeter();

 for(i=0;i<3;i++)

 ptr[i]->show();

 } 

 

I have given inputs as 5 and 6. See the screenshot below -

Output - 

Output of C program

Founder and Admin

Member since:
28 November 2009
Last activity:
4 years 42 weeks

what is the error in this program?

#include<iostream.h>
#include<string.h>
class shape
{
protected:
float perimeter;
float area;
public:
 void area();
 void perimeter();
 void show();
 };
 class rectangle:public shape
 {
 float length;
 float breadth;
 public:
 rectangle()
 {
 cout<<"enter the length";
 cin>>length;
 cout<<"enter the breadth";
 cin>>breadth;
 }
void area()
{
area=length*breadth;
}
void perimeter()
{
perimeter=2*(length+breadth);
}
void show()
{
cout<<"area of rectangle"<<area;
cout<<"perimeter of rectangle"<<perimeter;
}
 };
int main()
 {
 rectangle r;
 shape*ptr[]=&r;
 int i;
 for(i=0;i<3;i++)
 ptr[i]->area();
 for(i=0;i<3;i++)
 ptr[i]->perimeter();
 for(i=0;i<3;i++)
 ptr[i]->show();
 }
 
 this is the erroe statement

 declaration of `void shape::area()'
 conflicts with previous declaration `float shape::area'
 declaration of `void shape::perimeter()'
 conflicts with previous declaration `float shape::perimeter'
 invalid use of member (did you forget the `&' ?)
 invalid use of member (did you forget the `&' ?)
 no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits](((std::basic_ostream
 no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits](((std::basic_ostream
 initializer fails to determine size of `ptr'
 invalid initializer
Member since:
18 February 2009
Last activity:
20 weeks 3 days

Hello Harikrishnabodanki,

Your program is correct. I have successfully compiled it. Check out the output below -

Output of C program 

Try re-running the program. If you are still experiencing problem try running online compiler in a different browser. 

Founder and Admin