Operater Overloading Using 'class' Keyword (OOP)

Posted on at


PROBLEM:

Create a classdistance to add an overloaded- operator that subtracts two distances. It should allow statements like dist3=dist1-dist2;.Assume that the operator will never be used to subtract a larger numberfrom a smaller one (that is, negative distances are not allowed).

PROGRAM:

#include "stdafx.h"

#include "iostream"

using namespace std;

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

class Distance

{

            private:

                        int feet;            float inches;

            public:

                        Distance( ): feet(0), inches(0.0)

                                    {/*Empty Body*/}

                        Distance(intft, float in): feet(ft), inches(in)

                                    {/*Empty Body*/}

                        voidgetdist( )

                        {

                                    cout<< "Enter Feet: ";             cin>> feet;

                                    cout<< "Enter Inches: ";          cin>> inches;

                        }

                        voidshowdist( ) const

                        {

                                    cout<< feet << "'" << inches << "\"";

                        }

                        Distance operator - (Distance) const;

};

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

Distance Distance::operator - (Distance d2) const

{

            int f=0;float i=0.0;

            if(feet > d2.feet)

            {

                        f = feet - d2.feet;

                        if(inches > d2.inches)

                        {

                                    i = inches - d2.inches;

                                    if(i >= 12.0)

                                    {

                                                i-=12.0;

                                                f++;

                                    }

                        }

                        else if(inches < d2.inches)

                        {

                                    i = d2.inches - inches;

 

 

                                    if(i >= 12.0)

                                    {

                                                i-=12.0;

                                                f++;

                                    }

                        }

            }

            else if(feet < d2.feet)

            {

                        f = d2.feet - feet;

                        if(inches > d2.inches)

                        {

                                    i = inches - d2.inches;

                                    if(i >= 12.0)

                                    {

                                                i-=12.0;

                                                f++;

                                    }

                        }

                        else if(inches < d2.inches)

                        {

                                    i = d2.inches - inches;

                                    if(i >= 12.0)

                                    {

                                                i-=12.0;

                                                f++;

                                    }

                        }

            }

            return Distance(f,i);

}

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

void main( )

{

            Distance dist1, dist2(10,6.8), dist3, dist4;

            dist1.getdist( );

            dist3 = dist1 - dist2;

            dist4 = dist1 - (dist2 - dist3);

            cout<< "dist1 = ";                    dist1.showdist();

            cout<< "\ndist2 = ";                 dist2.showdist();

            cout<< "\ndist3 = ";                 dist3.showdist();

            cout<< "\ndist4 = ";                 dist4.showdist();

            cout<<endl;

            system("pause");

}

OUTPUT:

                                  

PROBLEM:

Create a classString that substitutes an overloaded += operator for the overloaded + operatorin the STRPLUS program in this chapter. This operator should allow statements likes1 += s2;where s2 is added (concatenated) to s1 and the result is left in s1. The operator shouldalso permit the results of the operation to be used in other calculations, as ins3 = s1 += s2.

PROGRAM:

#include "stdafx.h"

#include "iostream"

#include "string.h"

#include "stdlib.h"

using namespace std;

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

class String

{

            private:

                        enum { SZ=80 };

                        charstr[SZ];

            public:

                        String()

                                    {           strcpy_s(str, "");          }

                        String( char s[] )

                                    {           strcpy_s(str, s);           }

                        void display() const

                                    {           cout<<str<<endl;        }

                        String operator += (String ss) const;

};

//---------------------------------------------------------------------------------------String String::operator += (String ss) const

                        {

                                    String temp;

                                    if(strlen(str) + strlen(ss.str) < SZ )

                                    {

                                                strcpy_s(temp.str, str);

                                                strcat_s(temp.str, ss.str);

                                    }

                                    else

                                    {           cout<< "\nString overflow";     exit(1);             }

                                    return temp;

                        }

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

void main()

{

            String s1 = "Merry Christmas! ", s2 = "Happy new year!", s3;

            s1.display();                 s2.display();

            s3 = s1 += s2;             s3.display();

            cout<<endl;

}

OUTPUT:

                               

PROBLEM:

Create a class called time that has separate int member data for hours, minutes, and seconds. One constructor should initialize this data to 0, and another should initialize it to fixed values. Another member function should display it, in 11:59:59 format. Instead of a function add_time() it uses the overloaded + operator to add two times.

A main() program should create two initialized time objects (should they be const ?) and one that isn’t initialized. Then it should add the two initialized values together, returning the result to the third time variable. Finally it should display the value of this third variable.Make appropriate member functions const.

PROGRAME:

#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) const;

};

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

time time::operator + (time t2) const

{

            int sec, min, hr;

            hr = hrs + t2.hrs;

            min = mints + t2.mints;

            if(min > 59)

                        {           min -=60;         hr++;               }

            sec=secs + t2.secs;

            if(sec > 59)

                        {           sec -=60;         min++;             }

            return time(hr, min, sec);       

}

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

void main( )

{

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

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

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

            time3 = time1 + time2;

            cout<< "Time3 is: ";                time3.display( );

            system("pause");

}

OUTPUT:

                              



About the author

MuhammadTayyab

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

Subscribe 0
160