Tuesday, October 21, 2014

Age calculation of a person implemented with c++


#include<iostream.h>
 class age
 {
            private: int y,m,d;
            public :

                                    age()
                                    {
           
                                    }
                                    age(int y1,int m1,int d1)
                                    {
                                                y=y1;m=m1;d=d1;
                                    }
                                    age operator - (age c)
                                    {
                                                age temp;
                                                if(d<31)
                                                {
                                                            d=d+30;
                                                            temp.d=d-c.d;
                                                            c.m=c.m+1;
                                                }
                                                if(m<13)
                                                {
                                                            m=m+12;
                                                            temp.m=m-c.m;
                                                            c.y=c.y+1;
                                                }
                                                temp.y=y-c.y;
            return temp;
                                    }
                                    void show()
                                    {
            cout<<"Age of the person=";
                                                cout<<endl<<"Year="<<y<<" month="<<m<<" day="<<d;
                                    }
 };
 void main()
 {
            age a1(2014,3,5),a2(1993,4,12),a3;
            a3=a1-a2;
            a3.show();
 }


Output:

No comments:

Post a Comment