In: Computer Science
Q1(a) A class named “Employee” holds information like employee code, name,gender, year of joining. Write a program in C++ to create three objects of employee and enter some data into it through setters. Make getters and setters for all employee information. Then ask the user to enter current year. Display the names of those employees whose tenure is 2 or more than 2 years according to the given current year only using getters
(b)A class named “Employee” holds information like employee
code, name,gender, year of joining. Write a program to create five
hundred objects (Array of employee objects )of employee and enter
some data into it through setters. Make getters and setters for all
employee information. Then ask the user to enter current year.
Display the names of those employees whose tenure is 5 or more than
5 years according to the given current year only using
getters.
Note: employee code is automatically assigned to newly created
object by calling default constructor
(b) Define a class StudentReport with the following
specification:
Private members :
adno 4 digit admission number
name 20 characters
marks an array of 5 floating point values
average average marks obtained
GETAVG() a function to compute the average obtained in five
subject
Public members:
READINFO() function to accept values for adno, name, marks. Invoke
the function
GETAVG()
DISPLAYINFO() function to display all data members of StudentReport
on the screen.
You should give function definitions outside the class using scope
resolution operator.
Use C++ to answer
(a)
#include<iostream>
using namespace std;
class Employee{
int code,yearjoin;
string name,gender;
public:
int getCode()
{
return
code;
}
int getYearjoin()
{
return
yearjoin;
}
string getName()
{
return
name;
}
string getGender()
{
return
gender;
}
void setCode(int code)
{
this->code =
code;
}
void setYearjoin(int
yearjoin)
{
this->yearjoin = yearjoin;
}
void setName(string name)
{
this->name =
name;
}
void setGender(string gender)
{
this->gender
= gender;
}
};
int main()
{
Employee obj[3];
obj[0].setCode(1001);
obj[0].setYearjoin(1990);
obj[0].setName("Mary");
obj[0].setGender("F");
obj[1].setCode(1002);
obj[1].setYearjoin(2019);
obj[1].setName("John");
obj[1].setGender("M");
obj[2].setCode(1003);
obj[2].setYearjoin(2017);
obj[2].setName("Mathew");
obj[2].setGender("M");
int cyear;
cout<<"Enter current year: ";
cin>>cyear;
for(int i=0;i<3;i++)
{
if(cyear-obj[i].getYearjoin()>=2)
{
cout<<obj[i].getCode()<<"\t"<<obj[i].getName()<<"\t"<<obj[i].getGender()<<"\t"<<obj[i].getYearjoin()<<"\n";
}
}
}
================================================
(b)
#include<iostream>
using namespace std;
class Employee{
int code,yearjoin;
string name,gender;
public:
int getCode()
{
return
code;
}
int getYearjoin()
{
return
yearjoin;
}
string getName()
{
return
name;
}
string getGender()
{
return
gender;
}
void setCode(int code)
{
this->code =
code;
}
void setYearjoin(int
yearjoin)
{
this->yearjoin = yearjoin;
}
void setName(string name)
{
this->name =
name;
}
void setGender(string gender)
{
this->gender
= gender;
}
};
int main()
{
Employee obj[500];
obj[0].setCode(1001);
obj[0].setYearjoin(1990);
obj[0].setName("Mary");
obj[0].setGender("F");
obj[1].setCode(1002);
obj[1].setYearjoin(2019);
obj[1].setName("John");
obj[1].setGender("M");
obj[2].setCode(1003);
obj[2].setYearjoin(2017);
obj[2].setName("Mathew");
obj[2].setGender("M");
/*
if wanted enter more details
*/
int cyear;
cout<<"Enter current year: ";
cin>>cyear;
for(int i=0;i<500;i++)
{
if(cyear-obj[i].getYearjoin()>=5)
{
cout<<obj[i].getCode()<<"\t"<<obj[i].getName()<<"\t"<<obj[i].getGender()<<"\t"<<obj[i].getYearjoin()<<"\n";
}
}
}
//Output
==========================================
(c)
#include<iostream>
#include<cstring>
using namespace std;
class StudentReport{
private:
int adno;
char name[20];
float marks[5];
float avg;
float GETAVG();
public:
void READINFO(int adno, char
name[], float marks[]);
void DISPLAYINFO();
};
void StudentReport::READINFO(int adno, char n[], float
marks[])
{
this->adno = adno;
strcpy(this->name,name);
for(int i=0;i<5;i++)
{
this->marks[i] = marks[i];
}
avg = GETAVG();
}
float StudentReport::GETAVG()
{
float sum = 0;
for(int i=0;i<5;i++)
{
sum += marks[i];
}
sum/=5;
return sum;
}
void StudentReport::DISPLAYINFO()
{
printf("%d\t%s\t%f\n",adno,name,avg);
}