Slip8
Q.1) Write a C++ program to create a class Employee which contains data members as Emp_Id, Emp_Name, Basic_Salary, HRA, DA, Gross_Salary. Write member functions to accept Employee information. Calculate and display Gross salary of an employee. (DA=12% of Basic salary and HRA = 30% of Basic salary) (Use appropriate manipulators to display employee information in given format :- Emp_Id and Emp_Name should be left justified and Basic_Salary, HRA, DA, Gross salary Right justified with a precision of two digits)
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class emp
{
int eid;
float bs,hra,da,gs;
char name[10];
public:
void accept()
{
cout<<"enter id";
cin>>eid;
cout<<"enter name";
cin>>name;
cout<<"enter bs";
cin>>bs;
}
void display()
{
cout<<"\n eid="<<eid<<"\n name="<<name<<"\n bssic salary = "<<bs;
cout<<"\n HRA = "<<(bs*0.30);
cout<<"\n DA = "<<(bs*0.12);
cout<<"\n Gross Salary = "<<(bs-((bs*0.12)+(bs*0.30)));
}
};
void main()
{
emp ob;
clrscr();
ob.accept();
ob.display();
getch();
}
Another way
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class emp
{
int eid;
float bs,hra,da,gs;
char name[10];
public:
void accept()
{
cout<<"enter id";
cin>>eid;
cout<<"enter name";
cin>>name;
cout<<"enter bs";
cin>>bs;
}
void display()
{
cout<<"\n eid="<<eid<<"\n name="<<name<<"\n bssic salary = "<<bs;
cout<<"\n HRA = "<<(bs*0.30);
cout<<"\n DA = "<<(bs*0.12);
cout<<"\n Gross Salary = "<<(bs-((bs*0.12)+(bs*0.30)));
}
};
void main()
{
emp ob;
clrscr();
ob.accept();
ob.display();
getch();
}
Another way
#include<iostream.h>
#include<conio.h>
class
empl
{
int
id;
char
name[30];
float
basic,hra,da,gross_salary;
public:
void
getdata()
{
cout<<"\nenter
employee ID :\n";
cin>>id;
cout<<"\nenter
name:\n";
cin>>name;
cout<<"\nenter
basic salary:\n";
cin>>basic;
}
void
putdata()
{
cout.setf(ios::left,ios::adjustfield);
cout<<"\n
employee ID is :"<<id;
cout<<"\name
is :"<<name;
cout.setf(ios::right,ios::adjustfield);
cout<<"\n
basic salary is :\n"<<basic;
hra=0.30*basic;
cout<<"\n
HRA is\n"<<hra;
da=0.12*basic;
cout<<"\n
DA is "<<da;
gross_salary=basic+hra+da;
cout<<"\n
gross salary is :\n"<<gross_salary;
}
};
void
main()
{
int
a,n;
empl
b[10];
clrscr();
cout<<"\n
enter how many detail you want to enter";
cin>>n;
for(int
i=0;i<n;i++)
{
b[i].getdata();
cout.precision(2);
}
for(i=0;i<n;i++)
{
b[i].putdata();
}
getch();
}
Q.2) Create a class Date containing members as:
- dd
- mm
- yyyy
Write a C++ program for overloading operators >> and << to accept and display a Date also write a member function to validate a date.
#include<iostream.h>
#include<conio.h>
class date
{
int day;
int month;
int year;
public:
friend operator
>>(istream &,date &);
friend operator
<<(ostream &,date &);
};
operator
>>(istream &din,date &d)
{
cout<<"Enter
day:";
din>>d.day;
cout<<"Enter
month:";
din>>d.month;
cout<<"Enter
year:";
din>>d.year;
}
operator<<(ostream
&dout,date &d)
{
dout<<d.day<<"/"<<d.month<<"/"<<d.year<<"\n";
}
int main()
{
date d;
clrscr();
cin>>d;
cout<<"\nDate:";
cout<<d;
getch();
return(0);
}