Slip25
Q.1) Create a
class Clock that contains integer data members as hours, minutes and seconds. Write
a C++ program to perform following member functions:
void setclock(int, int, int ) to set
the initial time of clock object.
void
showclock() to display the time in hh:min:sec format.
Write a
function tick( ) which by default increment the value of second by 1 or
according to user specified second. The clock uses 24 hours format.
#include<iostream.h>
#include<conio.h>
#include<dos.h>
class time1
{
int h,m,s,ms;
public:
void setclock()
{
h=m=s=0;
}
void showclock()
{
if(s>59)
{
s=0;
m++;
}
else if(m>59)
{
m=0;
h++;
}
cout<<endl<<h<<":"<<m<<":"<<s;
}
void tick()
{
s++;
}
};
void main()
{
clrscr();
time1 t1;
if(1)
{
t1.setclock();
while(!kbhit())
{
t1.showclock();
delay(500);
t1.tick();
}
}
getch();
}
Q.2) Create a class Person that contains
data members as Person_Name, City, Mob_No. Write a C++ program to perform
following functions:
i.
To accept
and display Person information
ii.
To search
the mobile number of a given person
iii.
To search
the Person details of a given mobile number
(Use Function Overloading)
#include<iostream.h>
#include<conio.h>
#include<string.h>
class person
{
char name[10],city[10];
long int mobile;
public:
void get(void)
{
cout<<"\n\nenter name of person:\t";
cin>>name;
cout<<"\nenter city:\t";
cin>>city;
cout<<"\nenter mobile number:\t";
cin>>mobile;
}
void display(void)
{
cout<<"\n\n1.Name\t"<<name;
cout<<"\n2.City\t"<<city;
cout<<"\n3.Mobile\t"<<mobile;
}
void search(person a, char *n)
{
if((strcmp(n,a.name))==0)
cout<<"\nMobile number
is:\t"<<a.mobile;
}
void search(person a, long num)
{
if(num==a.mobile)
{
cout<<"\n\nPerson name:\t"<<a.name;
cout<<"\nCity:\t"<<a.city;
}
}
};
void main()
{
long int k,i,m;
char a[10];
person p[10];
clrscr();
cout<<"How many records you want to
enter:\t";
cin>>k;
for(i=0;i<k;i++)
{
p[i].get();
}
for(i=0;i<k;i++)
{
p[i].display();
}
cout<<"\n\n\nEnter name to search:\t";
cin>>a;
for(i=0;i<k;i++)
{
p[i].search(p[i],a);
}
cout<<"\n\n\nEnter mobile to search:\t";
cin>>m;
for(i=0;i<k;i++)
{
p[i].search(p[i],m);
}
getch();
}
Another way
Another way
#include<iostream.h>
#include<conio.h>
#include<string.h>
class
Person
{
char
Person_name[20];
char
city[20];
long
Mob_no;
public:
void
getdata();
void
displaydata();
void
search(Person,char *);
void
search(Person,long);
};
void
Person::getdata()
{
cout<<"\nEnter
the name of the Person:";
cin>>Person_name;
cout<<"\nEnter
the city of the Person:";
cin>>city;
cout<<"\nEnter
the Mobile Number:";
cin>>Mob_no;
}
void
Person::displaydata()
{
cout<<"\n1.Person
Name:"<<Person_name;
cout<<"\n2.Person
City:"<<city;
cout<<"\n3.Mobile
Number:"<<Mob_no;
}
void
Person::search(Person a,char *n)
{
if((strcmp(n,a.Person_name))==0)
{
cout<<"\n\nMobile
Number is:"<<a.Mob_no;
}
}
void
Person::search(Person a,long num)
{
if(num==a.Mob_no)
{
cout<<"\n\nPerson
Name is:"<<a.Person_name;
}
}
int
main()
{
clrscr();
int
k;
cout<<"\n\nEnter
how many records you want to enter:";
cin>>k;
char
a1[20];
long
l;
Person
p[10];
cout<<"\n\n*****
Enter the details of the Person. *****";
for(int
i=0;i<k;i++)
p[i].getdata();
cout<<"\n***********
Details of the Person ***********";
for(i=0;i<k;i++)
p[i].displaydata();
cout<<"\nEnter
the name of the person to search phone number:";
cin>>a1;
for(i=0;i<k;i++)
p[i].search(p[i],a1);
cout<<"\nEnter
the Phone number of the person to search Person name:";
cin>>l;
for(i=0;i<k;i++)
p[i].search(p[i],l);
getch();
return
0;
}