Thursday, 28 December 2017

CPP-Slip1



Q.1)Write a C++ program to create a class Worker with data members as Worker_Name, No_of_Hours_worked, Pay_Rate. Write necessary member functions to calculate and display the salary of worker. (Use default value for Pay_Rate)   

 

#include<stdio.h>

#include<iostream.h>

#include<conio.h>

class worker

{

                char name[10];

                int hr;

                public:

                void accept()

                {            

                                cout<<"enter name";

                                cin>>name;

                                cout<<"enter hours";

                                cin>>hr;

                }

                 void calculate(int rate=20)

                {

                      cout<<"salary of worker is Rs."<<(hr*10)*rate;

                }

};

 

void main()

{

                worker w;

                clrscr();

                w.accept();

                w.calculate();

                getch();

}         


Another way


#include<iostream.h>

#include<conio.h>

class worker

{

public:

char worker_name[20];

int salary,hrworked,pay_rate;

 

void calsalary(int pay_rate=25)

{

cout<<"Enter worker name\t";

cin>>worker_name;

 cout<<"Enter number of hours worked\t";

cin>>hrworked;

 salary=pay_rate*hrworked;

cout<<"\nWorker name :-\t"<<worker_name;

cout<<"\nSalary :-\tRs."<<salary;

}

};

 void main()

{

clrscr();

worker w;

w.calsalary();

getch();

}



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).                                          

 #include<iostream.h>

#include<conio.h>

#include<stdlib.h>

class Person

{

int Passport_id;

char Person_name[20];

char Nationality[20];

char Gender[20];

char Date_of_Birth[30];

char Date_of_Issue[30];

char Date_of_Expiry[30];

 

public:

void getdata();

void display(Person,int);

void display();

};

 

void Person::getdata()

{

cout<<"\n\nEnter the Passport id of the Person:";

cin>>Passport_id;

cout<<"\nEnter the name of the Person:";

cin>>Person_name;

cout<<"\nEnter the Nationality of the person:";

cin>>Nationality;

cout<<"\nEnter the Gender of the Person:";

cin>>Gender;

cout<<"\nEnter the Date of Birth:";

cin>>Date_of_Birth;

cout<<"\nEnter the Date of Issue:";

cin>>Date_of_Issue;

cout<<"\nEnter the Date of Expiry:";

cin>>Date_of_Expiry;

}


void Person::display(Person a,int num)

{

if((num==a.Passport_id)==1)

{

cout<<"\nPassport Details Are:";

cout<<"\n\nPassport ID:"<<a.Passport_id;

cout<<"\nPerson Name:"<<a.Person_name;

cout<<"\nNationality:"<<a.Nationality;

cout<<"\nGender:"<<a.Gender;

cout<<"\nDate of Birth:"<<a.Date_of_Birth;

cout<<"\nDate of Issue:"<<a.Date_of_Issue;

cout<<"\nDate of Expiry:"<<a.Date_of_Expiry;

}

}

void Person::display()

{

cout<<"\n*********** Passport Details Are ***********";

cout<<"\nPassport ID:"<<Passport_id;

cout<<"\nPerson Name:"<<Person_name;

cout<<"\nNationality:"<<Nationality;

cout<<"\nGender:"<<Gender;

cout<<"\nDate of Birth:"<<Date_of_Birth;

cout<<"\nDate of Issue:"<<Date_of_Issue;

cout<<"\nDate of Expiry:"<<Date_of_Expiry;

}

 int main()

{

clrscr();

int num,ch,k,l;

Person p[10];

cout<<"\n********** MENU ***********";

cout<<"\n1.Enter Details of all Person";

cout<<"\n2.Display Passport Details of one Person";

cout<<"\n3.Display Passport Details of all Person";

cout<<"\n4.Exit.";

do

{

cout<<"\nEnter your choice:";

cin>>ch;

 

switch(ch)

{

case 1:

cout<<"\nHow many records you wish to enter:";

cin>>k;

for(int i=0;i<k;i++)

{

p[i].getdata();

}

break;

 

case 2:

cout<<"\nEnter the Passport ID to check records:";

cin>>l;

for(i=0;i<k;i++)

{

p[i].display(p[i],l);

}

break;

 

case 3:

cout<<"\n************ Passport Details ****************";

for(i=0;i<k;i++)

{

 p[i].display();

}

break;

 

case 4:

exit(0);

 default:

cout<<"\nYou entered wrong choice:";

}

}while(ch!=4);

getch();

return 0;

   }