C++ Programming
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. [Marks 15]
Solution
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class clock
{
private:
int hr,min,sec;
public:
void setclock(int p,int q,int r)
{
hr=p;
min=q;
sec=r;
}
void showclock()
{
cout<<"\n"<<hr<<" : "<<min<<" : "<<sec<<"(HH:MM:SS)";
}
void tick(int i=1)
{
sec=sec+i;
if(sec>=60)
{
sec=sec-60;
min=min+1;
}
}
};
int main()
{
clrscr();
clock c;
int h,m,s;
cout<<"\n Enter hour mins and seconds:";
cin>>h>>m>>s;
c.setclock(h,m,s);
c.showclock();
cout<<"\n After increment:";
c.tick();
c.showclock();
getch();
return 0;
}
Output
Enter hour mins and seconds: 5 45 55.
5 : 45 : 55(HH:MM:SS)
After increment:
5 : 45 : 56(HH:MM:SS)
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) [Marks 25]
Solution
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class person
{
private:
char pname[20];
char city[30];
long mno;
public:
void accept();
void display();
void search(char a[]);
void search(long m);
};
void person:: accept()
{
cout<<"Enter the name:";
gets(pname);
cout<<"Enter the City:";
gets(city);
cout<<"Enter the mobile no:";
cin>>mno;
}
void person:: display()
{
cout<<"\nName :"<<pname;
cout<<"\nCity:"<<city;
cout<<"\nMobile no:"<<mno;
}
void person:: search(char a[])
{
if((strcmp(pname,a))==0)
{
cout<<"Mobile no of person:"<<mno;
}
}
void person:: search(long m)
{
if(mno==m)
{
display();
}
}
void main()
{
person p[10];
int i,n;
long m;
char a[20];
clrscr();
cout<<"Enter how many details u want:";
cin>>n;
for(i=0;i<n;i++)
{
p[i].accept();
}
for(i=0;i<n;i++)
{
p[i].display();
}
cout<<"\nEnter the name to search:";
gets(a);
for(i=0;i<n;i++)
{
p[i].search(a);
}
cout<<"\nEnter the number to saerch:";
cin>>m;
for(i=0;i<n;i++)
{
p[i].search(m);
}
getch();
}
Output
Enter the City:pune
Enter the mobile no:9860449276
Enter the name:Girish
Enter the City:Pune
Enter the mobile no:9527280773
Enter the name:Ravi
Enter the City:Latur
Enter the mobile no:8087482248
Name :Akshay
City:pune
Mobile no:1270514684
Name :Girish
City:Pune
Mobile no:937346181
Name :Ravi
City:Latur
Mobile no:-8087482248
Enter the name to search:akshay
Enter the number to saerch:9860449276
Name :Akshay
City:pune
Mobile no:9860449276
Tags:
c++