Saturday, September 27, 2014

Create a class time which stores time in hours, minutes and seconds. Use necessary member functions to get and display the information. Finally define a member function sum that takes the two input time as hrs:min:sec and add the two time and assign to third time i.e. t3=t1+t2. Note that 60sec=1 min and 60mins= 1 hr.

ALGORITHM:
1.      Declare class time
2.      get(int,int,int)
i.                    set hrs =h, min=m,secs=s
3.      put()
ii.                  display hrs:min:secs
4.      sum(time,time)
sec=t1.sec+t2.sec;
                        min=t1.min+t2.min;
                        if (sec>=60)
                        {
                                    min++;
                        }

                        hr=t1.hr+t2.hr;
                        if (min>=60)
                        {
                                    hr++;

                        }
                        sec=sec%60;
                        min=min%60;
                        hr=hr%12;
5.      main()
                                i.            t1.get(h,m,s)
                              ii.            t2.get(h,m,s)
                            iii.            t3.sum(t1,t2)
                            iv.            t1.put()
                              v.            t2.put()
                            vi.            t3.put()
6.      end

CODE:
#include
#include
class time
{
            int hr,min,sec;
            public:
            void gettime()
            {
            cout<
            cin>>hr>>min>>sec;
            }
            void puttime()
            {
                        cout<
            }
            void timesum(time t1,time t2)
            {
                        sec=t1.sec+t2.sec;
                        min=t1.min+t2.min;
                        if (sec>=60)
                        {
                                    min++;
                        }

                        hr=t1.hr+t2.hr;
                        if (min>=60)
                        {
                                    hr++;

                        }
                        sec=sec%60;
                        min=min%60;
                        hr=hr%12;
            }
};
void main()
{
            clrscr();
            time t1,t2,t3;
            t1.gettime();
            t2.gettime();
            t3.timesum(t1,t2);
            cout<
            t1.puttime();
            cout<<"+ T2~";
            t2.puttime();
            cout<
            t3.puttime();
            getch();

}

Create a class containing name and age of the employee as data member and getdata and putdata member function for getting and displaying the output. Using array of objects declare the number of employee object and list out their name and age.

ALGORITHM:
1.      Declare class record
2.      getdata()
i.      enter name
ii.     enter age
3.      showdata()
i.                    display name and age
4.      main()
for(i=0;i<3 i="" o:p="">
            s[i].setdata()
             for(i=0;i<3 i="" o:p="">
            s[i].showdata()
5.      end

CODE:

#include
#include
class employee
{
            private:
            char name[25];
            int age;
            int n;
            public:

                        void getdata(void)
                        {
                                    cout<
                                    cin>>name;
                                    cout<
                                    cin>>age;
                        }
                        void showdata()
                        {
                                    cout<<"Name:"<
                                    cout<<"Age:"<
                        }

};
void main()
{
            clrscr();
            employee e[100];
            int n=0;
            cout <<"How many Employees???"<
            cin>>n;
            for (int i=0 ;i
            e[i].getdata();
            for (i=0;i
            e[i].showdata();
            cout<<"Total no. of employees:"<
            getch();
}


Create a class containing name, age, weight as data members and setdata(),showdata() as member function to get and display the details. Write a main program to test the program.

ALGORITHM:
1.      Delcare class info with data members name,age and weight and member functions setdata() and showdata()
2.      setdata()
i.                    enter name
ii.                  enter age
iii.                enter weight
3.      showdata()
i.                    display name, age and weight
CODE
#include
#include
class grp
{
            char name[20];
            int age;
            int weight;
            public:
            void setdata(void)
            {
                        cout<
                        cin>>name;
                        cout<
                        cin>>age;
                        cout<
                        cin>>weight;
            }
            void showdata()
            {
                        cout<

                        cout<

                        cout<

            }
};
            void main()
            {
                        clrscr();
                        grp s1;
                        s1.setdata();
                        s1.showdata();

                        getch();


            }

WAP To Implement The Concept Of Dynamic Object And The Invocation Of Member Functions By Dynamic Objects.

#include
#include

class name
{

public:
int sample(int x)
{

    return x;
}

};
void main()

{
   clrscr();
   int x;
   name s1;
   cout<
   cin>>x;
    int  t = s1.sample(x); //dynamic initialization
    int p = s1.sample(x);       //dynamic initialization
    cout<
    cout <
    cout <
    getch();

}

Create a function template to compare two values and apply this function to data of various type also include string data type with its appropriate function.

#include
#include
#include
template
Tcomp(T a,T b)
{
            return((a>b)?a:b);
}
void comp(char s[],char t[])
{
            int c=strcmp(s,t);
            if(c==0)
                        cout<<"\nThe characters are equal";
            else if(c>0)
                        cout<
            else
                        cout<
}
void main()
{
            clrscr();
            double a,b;
            char c[20],d[20];
            cout<<"\nEnter two numbers: ";
            cin>>a>>b;
            cout<<"\nEnter two strings: ";
            cin>>c>>d;
            int ret =Tcomp(a,b);
            cout<
            comp(c,d);
            getch();

}

Create a function called sum () that returns the sum of the elements of an array. Make this function into a template so it will work with any numerical type. Write a main program that applies this function to data of various types.

#include
#include
template
T sum(T *a,int n)
{
            T s=0;
            for(int i=0;i
            {
                        s=s+a[i];
            }
            return s;
}
void main()
{
            clrscr();
            int n;
            double a[20];
            cout<<"Enter no. of elements:";
            cin>>n;
            cout<<"\nEnter the elements:";
            for(int i=0;i
            {
                   cin>>a[i];
            }
            cout<<"\nSum: "<
            getch();

}

Create a polymorphic class vehicle and create other derived classes Bus, Car and Bike from vehicle. Write a suitable program to illustrate virtual function and virtual destructor.

#include
#include
class vehicle
{
protected:
char brand[20];
long int pr;
public:
virtual void get()=0;
virtual void show()=0;
virtual~vehicle()
{
cout<
}
};
class bus:public vehicle
{
public:
void get()
{
cout<
cin>>brand;
cout<
cin>>pr;
}
void show()
{
cout<
}
virtual ~bus()
{
cout<
}
};
class car:public vehicle
{
public:
void get()
{
cout<
cin>>brand;
cout<
cin>>pr;
}
void show()
{
cout<
}
virtual ~car()
{
cout<
}
};

class bike:public vehicle
{
public:
void get()
{
cout<
cin>>brand;
cout<
cin>>pr;
}
void show()
{
cout<
}
virtual~bike()
{
cout<
}
};
void main()
{
clrscr();
vehicle *v[3];
v[0]=new bus;
v[1]=new car;
v[2]=new bike;
for(int i=0;i<3 i="" o:p="">
{
v[i]->get();
}
for(i=0;i<3 i="" o:p="">
{
cout<
v[i]->show();
delete v[i];
}
getch();

}

Create a class person with its necessary data members and member function. Create a member function that compares the age of person and returns the person having greater age by the use of ‘this’ pointer.

#include
#include
class person
{
            int age1,age2,age;
            public:
            void input()
            {
                        cout<<"\nEnter two ages:";
                        cin>>age1>>age2;
            }
            void show()
            {
                        cout<age<<"is greater.";
            }
            int check()
            {
                        if(age1
                        return this->age=age2;
                        else
                        return this->age=age1;
            }
};
int main()
{
            clrscr();
            person p;
            p.input();
            p.check();
            p.show();
            getch();
            return 0;
}