Search This Blog

Showing posts with label operatoroverloading. Show all posts
Showing posts with label operatoroverloading. Show all posts

Sunday, 28 February 2016

Operator Overloading ( == )

Practical-16

Define a circle class with radius as data member, Add necessary constructors and member function to compute area of circle. Class should overload the = = operator to compare two circle objects whether they are equal in radius. Demonstrate its use in main().

#include<iostream>
#include<math.h>
using namespace std;
class circle
{
  int radius;
  float area;
  public:
  circle(int);
  void display();
 int operator==(circle c1);
 };
int circle::operator==(circle c1)
{
  if(radius==c1.radius)
  return 1;
  else
  return 0;
}
circle::circle(int radius)
{
cout<<"radius="<<radius<<endl;
      area=M_PI*(radius*radius);
}
void circle :: display()
{

  cout<<"area="<<area<<endl;
}
int main()
{
  circle c1(2);
  c1.display();
  circle c2(2);
  c2.display();
     int x;
     x=(c1==c2);
      if(x==1)
          cout<<"they are equal in radius";
      else
          cout<<"not equal";
  return 0;
}

Saturday, 23 January 2016

Function Overloading In C++

#include<iostream>

using namespace std;

void power(double m,int n=2);                //default value of n is set to 2
void power(int m,int n=2);                      
//default value of n is set to 2
 
int main()
{
    int m,n;
    double dm;
    cout << "Enter the integer value of m"<<endl;
    cin >> m;
    cout << "Enter the double value of m"<<endl;
    cin >> dm;
    cout << "Enter the integer value of n"<<endl;
    cin >> n;
    power(dm,n);
    power(m,n);
    power(dm);                                            //default value from prototype is used
    power(m);   
                                           //default value from prototype is used
 }

void power(double m,int n)
{
    double r=m;
    for(int i=1;i<n;i++)
    r=r*m;
    cout << "Value of "<< m <<" raise to "<< n <<" is "<<r<<endl;
}

void power(int m,int n)
{
    int r=m;
    for(int i=1;i<n;i++)
    r=r*m;
    cout << "Value of "<< m <<" raise to "<< n <<" is "<<r<<endl;
}

Monday, 18 January 2016

Operator Overloading in C++

#include<iostream.h>
class KARAN
{
private:
int k;

public:
KARAN():k(5)
{
}

void operator ++()
{
k=k+1;
}

void Display()
{
cout<<"Value is: "<<k;
}
};
int main()
{
KARAN g;
++g; /* operator function void operator ++() is called */
g.Display();
return 0;
}