26.8.15

Create a class called 'TIME'

Create a class called 'TIME' that has

-     three integer data members for hours, minutes and seconds

-     constructor to initialize the object to zero

-     constructor to initialize the object to some constant value

-     member function to add two TIME objects

-     member function to display time in HH:MM:SS format

Write a main function to create two TIME objects, add them and display the result in HH:MM:SS format.

#include<iostream.h>

#include<conio.h> class Time

{
int hrs,min,sec;

public: Time()

{
hrs=0;min=0;sec=0;
}

Time(int a,int b,int c)

{
hrs=a; min=b;sec=c;
}

void addTime(Time tt1,Time tt2)

{
sec=tt1.sec+tt2.sec;
min=sec/60;
sec=sec%60;
min=min+tt1.min+tt2.min;
hrs=min/60;
min=min%60;
hrs=hrs+tt1.hrs+tt2.hrs;
}

void display()

{
cout<<hrs<<":"<<min<<":"<<sec<<endl;
}

};

int main()

{

clrscr();
Time t1(10,48,30);

Time t2(2,22,35); Time t3; t3.addTime(t1,t2); cout<<"\nTime 1 is :"; t1.display(); 
cout<<"\nTime 2 is :"; t2.display();

           cout<<"\nTime 1 + Time 2 is :";
          
          
           t3.display();
           
           getch();
           
    
  
           return(0);
}


***********************OUTPUT******************************

Time 1 is :10:48:30

Time 2 is :2:22:35

Time 1 + Time 2 is :13:11:5

Create a class called 'EMPLOYEE'

Create a class called 'EMPLOYEE' that has

-     EMPCODE and EMPNAME as data members

-     member function getdata( ) to input data

-     member function display( ) to output data

Write a main function to create EMP, an array of EMPLOYEE objects. Accept and display the details of at least 6 employees.


#include<iostream.h>

#include<conio.h>
#include<iomanip.h> class Employee

{
private:  int empcode;
char empname[10]; public: void getdata();

void display();
};
void Employee::getdata()
{
cout<<"\nNAME :"; cin>>empname; cout<<"\nCODE :"; cin>>empcode;

}
void Employee::display()
{
cout<<endl<<setw(20)<<empname<<setw(10)<<empcode;
}

int main()

{
Employee Emp[6]; clrscr();
cout<< "Enter employee details:\n "; for(int i=0;i<6;i++)
{

cout<<"\nemployee "<<i+1<<endl; Emp[i].getdata();
}

cout<<"\nEmployee details are as follows :"; cout<<"\n\n"<<setw(20)<<"NAME"<<setw(10)<<setiosflags(ios::right)<<"CODE";

cout<<"\n------------------------------
";
for(i=0;i<6;i++)

Emp[i].display();

getch();

return(0);

}

*************************OUTPUT*********************************

Enter employee details:

employee 1


NAME :ashok


CODE :111


employee 2


NAME :annapurna


CODE :112


employee 3


NAME :anupama


CODE :113


employee 4


NAME :anuradha


CODE :114


employee 5


NAME :ashraya


CODE :115


employee 6


NAME :akash


CODE :116


Employee details are as follows :

NAME
CODE

----------------------------------------------

Ashok
111

annapurna
112

anupama
113

anuradha
114

ashraya
115

akash
116


Create a 'DISTANCE' class

Create a 'DISTANCE' class with :

-     feet and inches as data members

-     member function to input distance

-     member function to output distance

-     member function to add two distance objects

Write a main function to create objects of DISTANCE class. Input two distances and output the sum.

#include<iostream.h>

#include<conio.h>

class distance

{
int feet,inch;
public:
void getdistance(); void putdistance();

void adddistance(distance d1,distance d2);
};

void distance::getdistance()

{

cout<<"\nEnter the feet : "; cin>>feet;
cout<<"\nEnter the inches :"; cin>>inch;
}

void distance::putdistance()
{
cout<<"\n\nfeet = "<<feet; cout<<"\tinch = "<< inch;

}
void distance::adddistance(distance d1,distance d2)
{
inch=d1.inch+d2.inch;
feet=inch/12;
inch=inch%12;
feet=feet+d1.feet+d2.feet;
}

int main()

{

distance d1,d2,d3; clrscr();

cout<< "\nDistance 1 \n"; d1.getdistance();

cout<< "\nDistance 2 \n"; d2.getdistance(); d3.adddistance(d1,d2);
cout<<"\nThe Sum Of two distance is : "; d3.putdistance();
getch();

return(0);
}


*********************************OUTPUT****************************

Distance 1

Enter the feet : 13

Enter the inches :11

Distance 2

Enter the feet : 11

Enter the inches :11

The Sum Of two distance is :

feet = 25   inch = 10

Implement function overloading

Write a C++ program to implement function overloading in order to compute power(m,n) where i) m is double and n is int ii) m and n are int.

#include<iostream.h>

#include<conio.h>
#include<math.h>

int power(int a,int b);

double power(int i, double d);

int main()

{

int i1,i2,i3; double d1; clrscr();
cout<<"\nEnter Two Integer Numbers To Calculate Power : "; cin >>i1>>i2;
int p1=power(i1,i2);

cout<<"\nThe Power of "<<i1<<" and "<<i2<<" is : "<<p1; cout<<"\n\nEnter One Integer And One Double To calculate power :"; cin>>i3>>d1;

double p2=power(i3,d1);

cout<<"\nThe Power of "<<i3<<" and "<<d1<<" is : "<<p2; getch();
return(0);

}

int power(int a,int b)

{
return(pow(a,b));
}
double power(int i, double d)
{
return(pow(i,d));
}


********************************OUTPUT**********************************

Enter Two Integer Numbers To Calculate Power : 2  4

The Power of 2 and 4 is : 16

Enter One Integer And One Double To calculate power :2 2.5


The Power of 2 and 2.5 is : 5.656854

Sort an array of integer in ascending order

Write a C++ program to sort an array of integer in ascending order using a function called exchange( ) which accepts two integer arguments by reference


#include<iostream.h>

#include<conio.h>

void exchange(int (&a)[],int &n);

int main()

{

int a[10],size; clrscr();

cout<<"Enter the Array size : "; cin>>size;
cout<<"Enter the Array elements :\n"; for(int i=0;i<size;i++)
cin>>a[i];

exchange(a,size); cout<<"After sorting :\n"; for(i=0;i<size;i++) cout<<a[i]<<endl; getch();

return 0;
}

void exchange(int (&a)[],int &n)

{
for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(a[i]<a[j])
{

int temp=a[i]; a[i]=a[j]; a[j]=temp;
}


}



**********************************OUTPUT********************************

Enter the Array size : 10 Enter the Array elements : 15 46 89 62 -5 -78 0 5 45 9











After sorting : -78 -5 0 5 9 15 45 46 62 89

Write a C++ program to find the largest of three numbers using inline function

Write a C++ program to find the largest of three numbers using inline function

#include<iostream.h>

#include<conio.h>

inline int largest(int &a,int &b,int &c)

{
int big=0; if(a>b)

big=a;
else
big=b;
if(c>big)
big=c; return big;

}

int main()

{
int a,b,c; clrscr();

cout<<"Enter Three Numbers To Find The Largest "<<endl; cout<<"a = ";
cin>>a; cout<<"\nb = "; cin>>b; cout<<"\nc = "; cin>>c;
int large=largest(a,b,c);
cout<<"\n Largest of "<<a<<","<<b<<" and "<<c<<" is "<<large; getch();
return(0);
}

********************************OUTOUT**********************************

Enter Three Numbers To Find The Largest a = 24

b = 45 c = 23

Largest of 24,45 and 23 is 45