Total Pageviews

PRASO's Solution for EMBETRONIX & ROBOTIX

SOLUTION PROVIDER OF ANY SORT OF PROBLEMS RELATED TO ELECTRONICS AND TELECOMMUNICATION

Monday, February 28, 2011

All the features of C++ are Demonstrated in One Program

#include"iostream"
using namespace std;

class Height
{
    private:
        int feet, inches;
    public:
        /* Default Constructor */

        Height()
        {
            feet=0;
            inches=0;
        }
       
        /* Parametrized Constructor */

        Height(int temp)
        {
            feet=temp;
            inches=temp;
        }

        /*Parametrized Constructor */

        Height(int a, int b)
        {
            feet=a;
            inches=b;
        }
       
        /* Copy Constructor */
       
        Height(const Height &ht)
        {
            feet=ht.feet;
            inches=ht.inches;
        }

        /* Member Function to set Values */

        void setValues()
        {
            cout<<"Enter dimension in feet";
            cin>>feet;
            cout<<"Enter dimension in inches";
            cin>>inches;
           
        }

        /* Overloaded Member Function to set values e:g: if the dimension is 2 feet 5 inches*/

        void setValues(int a, int b)
        {
            feet=a;
            inches=b;
        }

        /* Overloaded Member Function to set values e:g: if the dimension is 5 feet 5 inches*/

        void setValues(int a)
        {
            feet=a;
            inches=a;
        }

        /* Member Function to print the Dimension */
       
        void print()
        {
            cout<<<" feet "<<<" inches "<
        }

        /* '+' Operator Overloading */

        Height operator + (Height x)
        {
            Height sum;
            inches = (feet*12) + inches;
            x.inches = (x.feet*12) + x.inches;
            sum.feet = (inches + x.inches)/12;
            sum.inches = (inches + x.inches)%12;
            return sum;
        }
       
        /* '-' Operator Overloading */

        Height operator - (Height x)
        {
            Height gap;
            inches = (feet*12) + inches;
            x.inches = (x.feet*12) + x.inches;
            gap.feet = absolute((inches - x.inches))/12;
            gap.inches = absolute((inches - x.inches))%12;
            return gap;
        }

        /* function Which will operate as Modulo Operator */
       
        int absolute (int num)
        {
            if(num<0)
                return (-num);
            else
                return (num);
        }       
};


int main()
{

Height Ant;
cout<<"Height of Ant is:"<
Ant.print();

Height table(2,8);
cout<<"Height of Table is:"<
table.print();

Height ku;
ku = table + Ant;
cout<<"Height of ku is:"<
ku.print();

Height stick(2);
cout<<"Height of Stick is:"<
stick.print();

Height dog;
dog = table-stick;
cout<<"Height of dog is:"<
dog.print();

Height RosePlant;
RosePlant.setValues(1,4);
cout<<"Height of RosePlant is:"<
RosePlant.print();

Height HebiscusPlant;
HebiscusPlant = RosePlant + stick - Ant + dog;
cout<<"Height of HebiscusPlant is:"<
HebiscusPlant.print();

Height cosmos=RosePlant;
cout<<"Height of cosmos is:"<
cosmos.print();

return 0;
}




OUTPUT:

somanath@ubuntu:~/programs/CPP/28feb$ g++ Height.cpp
somanath@ubuntu:~/programs/CPP/28feb$ ./a.out
Height of Ant is:
0 feet 0 inches
Height of Table is:
2 feet 8 inches
Height of ku is:
2 feet 8 inches
Height of Stick is:
2 feet 2 inches
Height of dog is:
2 feet 6 inches
Height of RosePlant is:
1 feet 4 inches
Height of HebiscusPlant is:
6 feet 0 inches
Height of cosmos is:
1 feet 16 inches

No comments: