Skip to main content

Operator Overloading

    Create a class called Rational for performing arithmetic with rational nos. Use integer variables to represent numerator and denominator. Write a constructer method that enables an object of this class to be initialized when it is declared. Provide a no argument constructor with default values in case no initializes are provided. Provide a copy constructor also. Write methods for
a.       Addition, Overload '+' operator
b.      Subtraction, Overload '-' operator
c.       Multiply, Overload ‘*’ operator
d.      Division, Overload '/' operator
e.       Increment, Overload ++ operator (both prefix & postfix)
f.       Overload operator = =  (to check equality of two rational nos.) as a friend function
g.      Overload Assignment operator
h.      Printing in the form of a/b


#include<iostream>

using namespace std;

class rational
{
int x,y;
public:
rational(int i=1, int j=1)
{
x=i;
y=j;
}

rational operator + (rational ob)
{
rational temp;
temp.x= (x*ob.y + y*ob.x);
temp.y= y*ob.y;
return temp;
}

rational operator - (rational ob)
{
rational temp;
temp.x= (x*ob.y - y*ob.x);
temp.y= y*ob.y;
return temp;
}

rational operator * (rational ob)
{
rational temp;
temp.x= x*ob.x;
temp.y= y*ob.y;
return temp;
}

rational operator / (rational ob)
{
rational temp;
temp.x= x*ob.y;
temp.y= y*ob.x;
return temp;
}

rational operator = (rational ob)
{
x=ob.x;
y=ob.y;
return *this;
}

rational operator ++ ()  // prefix increment
{
x++;
y++;
return *this;
}

rational operator ++ ( int x) //postfix
{
rational temp = *this;
x++;
y++;
return temp;
}

friend int operator == (rational ob1, rational ob2);


void display()
{
cout<<" x/y ="<<x<<"/"<<y;
}
};

int operator == (rational ob1, rational ob2)
{
if((ob1.x * ob2.y) == (ob2.x * ob1.y)) /* x1   y2 = = x2   y1
                                                        -- * --          -- * --
                                                       y1   y2         y2   y1 */
return 1;
else
return 0;
}

void main()
{
rational r3;
int x,y;

cout<<" Enter 1st rational number\n x(numerator) = ";
cin>>x;
cout<<" y(denominator) = ";
cin>>y;

rational r1(x,y);
r1.display();

cout<<"\n Enter 2nd rational number\n x(numerator) = ";
cin>>x;
cout<<" y(denominator) = ";
cin>>y;

rational r2(x,y);
r2.display();

r3=r1 + r2;
cout<<"\n\n r3=r1 + r2 \n";
r3.display();

r3=r1 - r2;
cout<<"\n\n r3=r1 - r2 \n";
r3.display();

r3=r1 * r2;
cout<<"\n\n r3=r1 * r2 \n";
r3.display();

r3=r1 / r2;
cout<<"\n\n r3=r1 / r2 \n";
r3.display();

if(r1==r2)
cout<<"\n\n r1 is equal to r2\n\n";
else
cout<<"\n\n r1 is not equal to r2\n\n\n";

r1.display();
cout<<"\n Postfix Increment\n r1++ = ";
r1++; //postfix
r1.display();


cout<<"\n\n\n r2 = ";
r2.display();
++r2; //prefix
cout<<"\n Prefix Increment\n ++r2 = ";
r2.display();
cout<<"\n\n";

system("pause");
}




Comments

Popular posts from this blog

Main menu dropdown parent item clickable in Drupal 8 using Bootstrap 4

 Edit menu--main.html.twig template file {# /**  * @file  * Bootstrap Barrio's override to display a menu.  *  * Available variables:  * - menu_name: The machine name of the menu.  * - items: A nested list of menu items. Each menu item contains:  *   - attributes: HTML attributes for the menu item.  *   - below: The menu item child items.  *   - title: The menu link title.  *   - url: The menu link url, instance of \Drupal\Core\Url  *   - localized_options: Menu link localized options.  *   - is_expanded: TRUE if the link has visible children within the current  *     menu tree.  *   - is_collapsed: TRUE if the link has children within the current menu tree  *     that are not currently visible.  *   - in_active_trail: TRUE if the link is in the active trail.  */ #} {% import _self as menus...

Add JS to the bottom of the page in Drupal 7

How to add JS at bottom of a particular page in Drupal 7 Step 1: Get the page id. In case of front page "is_front"  Step 2: In template.php file of your theme add the below code : function illume_preprocess_page(&$variables) { if ($variables['is_front']) { drupal_add_js(path_to_theme().'/js/util.js', array('type' => 'file', 'scope' => 'footer')); drupal_add_js(path_to_theme().'/js/main.js', array('type' => 'file', 'scope' => 'footer')); drupal_add_js(path_to_theme().'/js/slideimage.js', array('type' => 'file', 'scope' => 'footer')); $variables['bottom_scripts'] = drupal_get_js(); } } This way you can add JS file using drupal_add_js() and define scope as footer. Step 3: Now you can use bottom_script variable to the page tpl file where you want to add JS print $bottom_scripts;

Use Case Diagram for Online Book Store