C++ Programming
Slip1
No_of_Hours_worked, Pay_Rate. Write necessary member functions to calculate and
display the salary of worker. (Use default value for Pay_Rate) [Marks 15]
Solution
#include<iostream.h>
#include<conio.h>
#include<string.h>
class worker
{
private:
char w_name[30];
int no_of_hours_work;
int pay_rate;
public:
void worker::display()
{
int sal;
sal=no_of_hours_work * pay_rate;
cout<<"\n Name : "<<w_name<<"\n salary : "<<sal;
cout<<"\n no of hours worked : "<<no_of_hours_work;
cout<<"\n pay rate : "<<pay_rate;
}
void worker::accept(char nm[30],int no,int rate=200)
{
strcpy(w_name,nm);
no_of_hours_work=no;
pay_rate=rate;
}
};
int main()
{
worker w1,w2;
clrscr();
w1.accept("Sonu",3,500);
w2.accept("Rahul",5);
w1.display();
w2.display();
getch();
return 0;
}
#include<conio.h>
#include<string.h>
class worker
{
private:
char w_name[30];
int no_of_hours_work;
int pay_rate;
public:
void worker::display()
{
int sal;
sal=no_of_hours_work * pay_rate;
cout<<"\n Name : "<<w_name<<"\n salary : "<<sal;
cout<<"\n no of hours worked : "<<no_of_hours_work;
cout<<"\n pay rate : "<<pay_rate;
}
void worker::accept(char nm[30],int no,int rate=200)
{
strcpy(w_name,nm);
no_of_hours_work=no;
pay_rate=rate;
}
};
int main()
{
worker w1,w2;
clrscr();
w1.accept("Sonu",3,500);
w2.accept("Rahul",5);
w1.display();
w2.display();
getch();
return 0;
}
Output
Name : Sonu
salary : 1500
no of hours worked : 3
pay rate : 500
Name : Rahul
salary : 1000
no of hours worked : 5
pay rate : 200
salary : 1500
no of hours worked : 3
pay rate : 500
Name : Rahul
salary : 1000
no of hours worked : 5
pay rate : 200
Q.2) Create class Person which contains data member as Passport_Id, Person_name,
Nationality, Gender, Date_of_Birth, Date_of_Issue, Date_of_expiry .Write a c++ program to perform following member functions:
i. Enter details of all persons
ii. Display passport details of one person
iii. Display passport details of all persons
(Use Function overloading and Array of object). [Marks 25]
Solution
#include<iostream.h>
#include<conio.h>
class person
{
private:
int p_id;
char p_name[30];
char nationality[20];
char gender [10];
char date_of_birth[10];
char date_of_issue[10];
char date_of_expiry[10];
public:
void accept();
void display(int);
void display();
};
void person::accept()
{
cout<<"Enter passport ID : ";
cin>>p_id;
cout<<"Enter person name : ";
cin>>p_name;
cout<<"Enter nationality : ";
cin>>nationality;
cout<<"Date of birth : ";
cin>>date_of_birth;
cout<<"Enter date of issue : ";
cin>>date_of_issue;
cout<<"Enter expiry date :";
cin>>date_of_expiry;
cout<<"Enter gender :";
cin>>gender;
}
void person::display(int k)
{
if(p_id==k)
{
cout<<"\npassport id : "<<p_id<<endl<<"person name : "<<p_name<<endl;
cout<<"nationality :"<<nationality<<endl<<"Gender : "<<gender<<endl;
cout<<"Date of birth : "<<date_of_birth<<endl<<"Date of issue: "<<date_of_issue<<endl;
cout<<"Date of Expiry : "<<date_of_expiry<<endl<<"------------------------"<<endl;
}
}
void person::display()
{
cout<<"\npassport id : "<<p_id<<endl<<"person name : "<<p_name<<endl;
cout<<"nationality :"<<nationality<<endl<<"Gender : "<<gender<<endl;
cout<<"Date of birth : "<<date_of_birth<<endl<<"Date of issue: "<<date_of_issue<<endl;
cout<<"Date of Expiry : "<<date_of_expiry<<endl<<"-------------------"<<endl;
}
int main()
{
person p[10];
clrscr();
int n,i;
int choice;
do
{
cout<<"\n 1: Enter person details.. \n 2: Passport details of one person..";
cout<<"\n 3: passport details of all person..\n 4: Exit \n";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: cout<<"\n Enter how many peron details wants to create ";
cin>>n;
for(i=0;i<n;i++)
{
p[i].accept();
}
break;
case 2:int k;
cout<<"\n enter passport id : ";
cin>>k;
for(i=0;i<n;i++)
{
p[i].display(k);
}
break;
case 3: for(i=0;i<n;i++)
{
p[i].display();
}
break;
}
}while(choice!=4);
getch();
return 0;
}
Output
1: Enter person details..
2: Passport details of one person..
3: passport details of all person..
4: Exit
Enter your choice: 1
Enter how many peron details wants to create 2
Enter passport ID : 41140
Enter person name : Rahul
Enter nationality : Indian
Date of birth : 10/11/1995
Enter date of issue : Wrong Date
Enter expiry date :Enter gender :Male
Enter passport ID : 41128
Enter person name : Sonu
Enter nationality : Indian
Date of birth : 20/6/1992
Enter date of issue : Wrong Date
Enter expiry date :Enetr gender :Male
1: Enter person details..
2: Passport details of one person..
3: passport details of all person..
4: Exit
Enter your choice: 4
passport id : 41140
person name : Rahul
nationality :Indian
Gender : Male
Date of birth : 10/11/1995Wrong
Date of issue: Wrong
Date of Expiry : date
-------------------
passport id : 41128
person name : Sonu
nationality :Indian
Gender : Male
Date of birth : 20/6/1992
Date of issue: Wrong
Date of Expiry : date
-------------------
1: Enter person details..
2: Passport details of one person..
3: passport details of all person..
4: Exit
Enter your choice: 4
Tags:
c++