Tuesday, October 20, 2009

Operator Overloading, Constructor, Destructor, Mutator, Facilitator methods in C++

/*This code snippet is an example of operator overloading, constructor, destructor,mutator, faclitator methods  Also it is divided in two parts, one a defintion part and another a declaration part */


#include 
using namespace std;

#include "complex1.h"

//Constructor
Complex1 :: Complex1()
{
 this->m_real = 0;
 this->m_imaginary = 0;
}
Complex1 :: Complex1(int m_real, int m_imaginary)
{
 this->m_real = m_real;
 this->m_imaginary = m_imaginary;
}

//inspector Methods

int Complex1 :: get_real() const
{
 return this->m_real;
}
int Complex1 :: get_imaginary() const
{
 return this->m_imaginary;
}

 //Mutator Methods

void Complex1 :: set_real(int m_real)
{
 this->m_real = m_real;
}
void Complex1 :: set_imaginary(int m_imaginary) 
{
 this->m_imaginary = m_imaginary;
}

 //Facilitator Methods

void Complex1 :: accept()
{
 cout << "Enter m_real and m_imaginary parts of a complex number ::";
 cin >> this->m_real >> this->m_imaginary;
}
void Complex1 :: display() const
{
 if(this->m_imaginary >= 0)
  cout << this->m_real << "+" << this->m_imaginary << "i" << endl;
 else if(this->m_imaginary < 0)
  cout << this->m_real << this->m_imaginary << "i" << endl;
}

Complex1 Complex1 :: complex_add(const Complex1& c1) const
{
 Complex1 temp;
 temp.m_real = this->m_real + c1.m_real;
 temp.m_imaginary = this->m_imaginary + c1.m_imaginary;
 return temp;
}


Complex1 Complex1 :: complex_subtract(const Complex1& c1) const
{
 Complex1 temp;
 temp.m_real = this->m_real - c1.m_real;
 temp.m_imaginary = this->m_imaginary - c1.m_imaginary;
 return temp;

}

Complex1 Complex1 :: operator+(const Complex1& c) const
{
 return complex_add(c);
}

Complex1 Complex1 :: operator-(const Complex1& c) const
{
 return complex_subtract(c);
}

Complex1 Complex1 :: operator++()
{
 this->m_real++;
 return *this;
}

Complex1 Complex1 :: operator++(int)
{
 Complex1 temp = *this;
 this->m_real++;
 return temp;
}

Complex1 Complex1 :: operator--()
{
 this->m_real--;
 return *this;
}

Complex1 Complex1 :: operator--(int)
{
 Complex1 temp = *this;
 this->m_real--;
 return temp;
 
}

bool Complex1 :: operator==(const Complex1& c)const
{
 if(this->m_real == c.m_real && this->m_imaginary == c.m_imaginary)
  return true;
 else
  return false;
}

bool Complex1 :: operator!=(const Complex1& c) const
{
 if(this->m_real != c.m_real || this->m_imaginary != c.m_imaginary)
  return true;
 else
  return false;
 
}




Complex1 :: ~Complex1()
{
 cout << "Destroying... " << endl;
}


ostream&  operator<<(ostream& out,const Complex1& c) 
{
 if(c.m_imaginary >= 0)
  out << c.m_real << "+" << c.m_imaginary << "i" << endl;
 else if(c.m_imaginary < 0)
  out << c.m_real << c.m_imaginary << "i" << endl;
 return out;
}

istream& operator>>(istream& in,Complex1& c) 
{
 in >> c.m_real >> c.m_imaginary;
 return in;
}



// Class Declaration


class Complex1
{
private:
 int m_real,m_imaginary;
public:
 //constructors
 Complex1();
 Complex1(int, int);

 //inspector Methods

 int get_real() const;
 int get_imaginary() const;

 //Mutator Methods

 void set_real(int );
 void set_imaginary(int );

 //Facilitator Methods

 void accept();
 void display() const;
 Complex1 complex_add(const Complex1& c) const;
 Complex1 complex_subtract(const Complex1& c) const;
 Complex1 operator+(const Complex1& c) const;
 Complex1 operator-(const Complex1& c) const;

 Complex1 operator++();
 Complex1 operator++(int);

 Complex1 operator--();
 Complex1 operator--(int);

 bool operator==(const Complex1&) const;
 bool operator!=(const Complex1&) const;

 friend ostream& operator<<(ostream& ,const Complex1&);
 friend istream& operator>>(istream& ,Complex1&);
 //destructor
 ~Complex1();


};


No comments:

Post a Comment