C++ Programming
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) [Marks 15]
Solution
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class emp
{
private:
int eid;
char ename[20];
float bs;
float da,hra,gsal;
public:
void accept();
void cal();
void display();
};
void emp:: accept()
{
cout<<"Enter the Employee Id:";
cin>>eid;
cout<<"Enter the Employee Name:";
cin>>ename;
cout<<"Enter the Employee basic Salary:";
cin>>bs;
}
void emp:: cal()
{
da=(12*bs)/100;
hra=(30*bs)/100;
gsal=bs+da+hra;
}
void emp:: display()
{
cout<<"\n";
cout.width(15);
cout<<setiosflags(ios::left);
cout<<"Id";
cout.width(15);
cout<<setiosflags(ios::left);
cout<<"Name";
cout.width(15);
cout<<setiosflags(ios::right);
cout<<"DA";
cout.width(15);
cout<<setiosflags(ios::right);
cout<<"HRA";
cout.width(15);
cout<<setiosflags(ios::right);
cout<<"Gross Salary";
cout<<endl<<"\n";
cout.width(15);
cout<<setiosflags(ios::left);
cout<<eid;
// cout<<"Employee Name:";
cout.width(15);
cout<<setiosflags(ios::left);
cout<<ename;
// cout<<"Employee DA:";
cout.width(15);
cout.precision(2);
cout<<setiosflags(ios::right);
cout<<da;
// cout<<"Employee HRA:";
cout.width(15);
cout.precision(2);
cout<<setiosflags(ios :: right);
cout<<hra;
// cout<<"Employee Gross Salary:";
cout.width(15);
cout.precision(2);
cout<<setiosflags(ios :: right);
cout<<gsal;
}
void main()
{
emp e1;
clrscr();
e1.accept();
e1.cal();
e1.display();
getch();
}
Output
Enter the Employee Id:321
Enter the Employee Name:AAA
Enter the Employee basic Salary:
25000
Id Name DA HRA Gross Salary
321 AAA 3e+03 7.5e+03 3.55e+04
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. [Marks 25]
Solution
#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;
return 0;
}
operator<<(ostream &dout,date &d)
{
dout<<d.day<<"/"<<d.month<<"/"<<d.year<<"\n";
return 0;
}
int main()
{
date d;
clrscr();
cin>>d;
cout<<"\nDate:";
cout<<d;
getch();
return(0);
}
Output
Enter day:20
Enter month:02
Enter year:2010
Date:20/2/2010
Tags:
c++