Operater Overloading Using 'class' Keyword (OOP)-2

Posted on at


PROBLEM:

 Create a class that imitates part of the functionality of the basic data type int. Call theclass Int (note different capitalization). The only data in this class is an int variable.Include member functions to initialize an Int to 0, to initialize it to an int value, to displayit (it looks just like an int).

Overload four integer arithmeticoperators (+, -, *, and /) so that they operate on objects of type Int. If the result of anysuch arithmetic operation exceeds the normal range of ints (in a 32-bit environment)—from 2,147,483,648 to –2,147,483,647—have the operator print a warning and terminatethe program. Such a data type might be useful where mistakes caused by arithmetic overfloware unacceptable. Hint: To facilitate checking for overflow, perform the calculationsusing type long double. Write a program to test this class.

PROGRAM:

#include "stdafx.h"

#include "iostream"

using namespace std;

/////////////////////////////////////////////////////////////////////////////////////////

classInt

{

           private:

                        long i;

            public:

                        Int( ) :i(0)         { /*Empty Body*/ }

                        Int(int n) :i(n)    { /*Empty Body*/ }

                        void display( Int obj2, Int obj3, char ch ) const

                        {           cout<< obj2.i <<ch<< obj3.i << " = " << i <<endl;      }

                        Int operator + (Int) const;                    Int operator - (Int) const;

                        Int operator * (Int) const;                     Int operator / (Int) const;

};

//---------------------------------------------------------------------------------------

IntInt::operator + (Int temp) const

{           long r;              r = i+temp.i;                 return Int(r);                 }

IntInt::operator - (Int temp) const

{           long r;              r = i-temp.i;                  return Int(r);                 }

IntInt::operator * (Int temp) const

{           long r;              r = i*temp.i;                 return Int(r);                 }

IntInt::operator / (Int temp) const

{           long r;              r = i/temp.i;                  return Int(r);                 }

/////////////////////////////////////////////////////////////////////////////////////////

void main( )

{

            Int first, second(250), third(122);

            first = second+third;                first.display( second,third,'+' );

            first = second-third;                 first.display( second,third,'-' );

            first = second*third;                 first.display( second,third,'*' );

            first = second/third;                 first.display( second,third,'/' );

            system("pause");

}

OUTPUT:

                      

PROBLEM:

Augment the time class referred to in Exercise 3 to include overloaded increment (++) and decrement (--) operators that operate in both prefix and postfix notation and return values. Add statements to main() to test these operators.

PROGRAM:

#include "stdafx.h"

#include "iostream"

using namespace std;

/////////////////////////////////////////////////////////////////////////////////////////

class time

{           private:

                        inthrs, mints, secs;

            public:

                        time( ) :hrs(0), mints(0), secs(0)

                                    { /*Empty Body*/ }

                        time(int h, int m, int s):hrs(h), mints(m), secs(s)

{ /*Empty Body*/ }

                        void display( ) const

                                     {          cout<<hrs<< ":" << mints << ":" <<secs<<endl;                    }

                        time operator ++ ( );

                        time operator ++ (int);

                        time operator -- ( );

                        time operator -- (int);

};

//---------------------------------------------------------------------------------------

time time::operator ++ ( )

{

            ++secs;

            if(secs> 59)

                        {           secs -=60;       mints++;          }

            if(mints > 59)

                        {           mints -=60;      hrs++;              }

            return time(hrs, mints, secs); 

}

time time::operator ++ (int)

{

            secs++;

            if(secs> 59)

                        {           secs -=60;       mints++;          }

            if(mints > 59)

                        {           mints -=60;      hrs++;              }

            return time(hrs, mints, secs); 

}

time time::operator -- ( )

{

            --secs;

            if(secs< 0)

                        {           secs +=60;      mints--;            }

            if(mints <0)

                        {           mints+=60;      hrs--;                }

            return time(hrs, mints, secs); 

}

time time::operator -- (int)

{

            secs--;

            if(secs< 0)

                        {           secs +=60;      mints--;            }

            if(mints < 0)

                        {           mints +=60;     hrs--;                }

            return time(hrs, mints, secs); 

}

/////////////////////////////////////////////////////////////////////////////////////////

void main( )

{

            time time1(10,25,2),time2(5,59,58);

            cout<< "Time1 is: ";                time1.display( );

            cout<< "Time2 is: ";                time2.display( );

            ++time1;          cout<< "\nTime1 after prefix increment is: ";             time1.display( );

            ++time2;          cout<< "Time2 after prefix increment is: ";                time2.display( );

            time1++;          cout<< "\nTime1 after postfix increment is: ";            time1.display( );

            time2++;          cout<< "Time2 after postfix increment is: ";               time2.display( );

            --time1;            cout<< "\nTime1 after prefix decrement is: ";            time1.display( );

            --time2;            cout<< "Time2 after prefix decrement is: ";               time2.display( );

            time1--;            cout<< "\nTime1 after postfix decrement is: ";           time1.display( );

            time2--;            cout<< "Time2 after postfix decrement is: ";              time2.display( );

            system("pause");

}

OUTPUT:

                              



About the author

MuhammadTayyab

Me from Pakistan and im the student of undergraduate;BS Electronics.

Subscribe 0
160