Unary operator ovrerloading,prefix,postfix,nameless objects, operator return in oop

Unary operator ovrerloading, prefix,postfix,nameless objects, operator return

Task 1:

Write a program to create class “Counter” having integer data member, overload the post increment, post decrement, pre increment and pre decrement for the class “Counter”.

#include<iostream>

using namespace std;

class Counter{

            int a;

public:

            void in()

            {

                        cout << "Enter the value:";

                        cin >> a;

            }

            void operator ++()

            {

                        a++;

            }

            void operator +()

            {

                        ++a;

            }

            void operator--()

            {

                        a--;

            }

            void operator -()

            {

                        --a;

            }

            void show()

            {

                        cout << "Value of a after load the operator:" << a << endl;

            }

};

int main()

{

            Counter t;

            t.in();

            t.show();

            +t;

            t.show();

            ++t;

            t.show();

            -t;

            t.show();

            --t;

            t.show();

            getchar();

            getchar();

            return 0;

}       

          

       
 

Task 2:

Use the class of TASK 1 extend it to overload the count variable but use Nameless Object to return the incremented value of count.

#include<iostream>

using namespace std;

class Counter{

            int count;

public:

            Counter()

            {

                        count = 0;

            }

            Counter(int a)

            {

                        count = a;

            }

            Counter operator +()

            {

                        count++;

                        return


Counter(count);

            }

            void show()

            {

                        cout << "Value


of a is:" << count << endl;

            }

};

int main()

{

            Counter();

            Counter t(1), t2;

            t2 = +t;

            t.show();

            getchar();

            getchar();

            return 0;

}      

          

       
 

Task 3:

(Prefix Operator) Consider the class dateTypewith data members day, month, and year. In this class write a functions to overload the pre-increment (++) and pre-decrement operators (--) to increase the date by a day and decrease the date by a day, respectively.

#include<iostream>

using namespace std;

class datetypewith{

            int day, month, year;

public:

            datetypewith()

            {

                        day = month = year = 0;

            }

            datetypewith(int x)

            {

                        if (x > 0 && x <= 31)

                        {

                                    day = x;

                        }

                        else

                        {

                                    day = 0;

                        }

            }

            datetypewith(int a, int b, int c)

            {

                        if (b > 0 && b <= 12)

                        {

                                    month = b;

                        }

                        else

                        {

                                    cout << "Invalid month entered therefor it is set to 0" << endl;

                                    month = 0;

                        }

                        year = c;

                        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)

                        {

                                    if (a > 0 && a <= 31)

                                    {

                                                day = a;

                                    }

                                    else

                                    {

                                                cout << "Please enter the valid value.Value is set to 0" << endl;

                                                cin.get();

                                                day = 0;

                                    }

                        }

                        else if (month == 2)

                        {

                                    if (a > 0 && a <= 29)

                                    {

                                                if (a == 29)

                                                {

                                                            day = a;

                                                            cout << "It is leap year" << endl;

                                                }

                                                else

                                                {

                                                            cout << "Invalid entry therefor day is set to 0" << endl;

                                                            day = 0;

                                                }

                                    }

                                    else

                                                cout << "This is normal febuary" << endl;

                        }

                        else if (month == 4 || month == 6 || month == 9 || month == 11)

                        {

                                    if (a > 0 && a <= 30)

                                    {

                                                day = a;

                                    }

                                    else

                                    {

                                                cout << "Invalid entry therefor day is set to 0" << endl;

                                                day = 0;

                                    }

                        }

                        else

                        {

                                    cout << "Day is invalid therefor it is set to 0" << endl;

                                    day = 0;

                        }

            }

            datetypewith operator +()

            {

                        day++;

                       

                        return datetypewith(day);

            }

            datetypewith operator-()

            {

                        day--;

                        datetypewith d(day);

                        return d;

            }

            void show()

            {

                        cout << day << ":" << month << ":" << year << endl;

            }

};

int main()

{

            datetypewith();

            int a, b, c;

            cout << "Enter the day:";

            cin >> a;

            cout << "Enter the month(1 to 12):";

            cin >> b;

            cout << "Enter the year:";

            cin >> c;

            datetypewith t(a, b, c);

            +t;

            t.show();

            -t;

            t.show();

            getchar();

            getchar();

            return 0;

}      

          

       
 

Comments

Popular posts from this blog

Multiple inheritance,friend function and multiple file in oop(object oriented programming)

Concepts of OOP (object oriented programming)

Concepts in c++........