In: Computer Science
Write in C++ language.
(Employee Record): Create a class named 'Staff' having the following members: Data members - Id – Name - Phone number – Address - AgeIt also has a function named 'printSalary' which prints the salary of the staff.Two classes 'Employee' and 'Officer' inherits the 'Staff' class. The 'Employee' and 'Officer' classes have data members 'Top Skill' and 'department' respectively. Now, assign name, age, phone number, address and salary to an employee and a officer by making an object of both of these classes and print the same.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
** NOTE: I WAS NOT
CLEAR WITH THE PART OF SALARY SO I PROVIDED BOTH
CODES
IF YOU STILL HAVE ANY PROBLEM PLEASE COMMENT BELOW I WILL DEFINELTY HELP YOU REGARDING THE SOLUTION**
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1. THIS CODE HAS AN ATTRIBUTE SALARY , AND THE FUNCTION PRINTS THE SALARY ASSIGNED
CODE TO COPY
#include<iostream>
#include<string>
using namespace std;
class Staff
{
public:
int Id;
string Name;
string Phonenumber;
string Address;
int Age;
double salary;
void printSalary()
{
cout<<"Salary:"<<salary<<endl;
}
};
class Employee:public Staff
{
public:
string Top_Skill;
};
class Officer:public Staff
{
public:
string department;
};
int main()
{
Employee emp1;
emp1.Id = 10;
emp1.Name = "empName";
emp1.Address = "London";
emp1.Age= 25;
emp1.Phonenumber = "04012310";
emp1.salary =1000;
cout<<"Employee Name:
"<<emp1.Name<<endl;
cout<<"Employee id:
"<<emp1.Id<<endl;
cout<<"Employee Age:
"<<emp1.Age<<endl;
cout<<"Employee Address:
"<<emp1.Address<<endl;
cout<<"Employee PhoneNum:
"<<emp1.Phonenumber<<endl;
cout<<"Employee Salary using printSalary method:
"<<endl;
emp1.printSalary();
cout<<endl<<"**********************"<<endl<<endl;
Officer off1;
off1.Id = 12;
off1.Name= "offName";
off1.Address = "Bankok";
off1.Age = 27;
off1.Phonenumber = "0400000";
off1.salary = 5000;
cout<<"Officer Name:
"<<off1.Name<<endl;
cout<<"Officer id:
"<<off1.Id<<endl;
cout<<"Officer Age:
"<<off1.Age<<endl;
cout<<"Officer Address:
"<<off1.Address<<endl;
cout<<"Officer PhoneNum:
"<<off1.Phonenumber<<endl;
cout<<"Officer Salary using printSalary method:
"<<endl;
off1.printSalary();
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
EXPLANATION
#include<iostream> // for taking input output
#include<string> // for string operation
using namespace std; // for cout cin
class Staff //creating a class Staff
{
public: // to access all these fields they must be
public
int Id; // creating a id
member
string Name; //creating a Name
of type string
string Phonenumber; //
creating a phonenumber of type string
string Address; // creating a
address field
int Age; //creating age
fiels
double salary; //creating
salary of type double
void printSalary() // creating
a function printSalary
{
cout<<"Salary:"<<salary<<endl; // printing
salary
}
};
class Employee:public Staff //inheriting Staff class
{
public:
string Top_Skill; // creating
a Top_Skill field of type string
};
class Officer:public Staff //inheriting Staff class
{
public:
string department; // creating
a department field of type string
};
int main()
{
Employee emp1; //creating a object of employee
class
emp1.Id = 10; //assigning id as 10
emp1.Name = "empName"; //assigning Name as
empName
emp1.Address = "London"; //assigning address as
London
emp1.Age= 25; //assigning age as 25
emp1.Phonenumber = 0401231230; //assigning
PhoneNumber
emp1.salary =1000; //assigning salary
//printing all details of emp
cout<<"Employee Name:
"<<emp1.Name<<endl;
cout<<"Employee id:
"<<emp1.Id<<endl;
cout<<"Employee Age:
"<<emp1.Age<<endl;
cout<<"Employee Address:
"<<emp1.Address<<endl;
cout<<"Employee PhoneNum:
"<<emp1.Phonenumber<<endl;
cout<<"Employee Salary using printSalary method:
"<<endl;
emp1.printSalary(); //printing salary using the
function
cout<<endl<<"**********************"<<endl<<endl;
//printing line
Officer off1; //creating a object of officer
class
off1.Id = 12; //assigning id as 12
off1.Name= "offName"; //assigning Name offName
off1.Address = "Bankok"; //assigning address as
Bankok
off1.Age = 27; //assigning age as 27
off1.Phonenumber = 0400000000; // assigning
PhoneNumber
off1.salary = 5000; //assigning salary
//printing all details of officer
cout<<"Officer Name:
"<<off1.Name<<endl;
cout<<"Officer id:
"<<off1.Id<<endl;
cout<<"Officer Age:
"<<off1.Age<<endl;
cout<<"Officer Address:
"<<off1.Address<<endl;
cout<<"Officer PhoneNum:
"<<off1.Phonenumber<<endl;
cout<<"Officer Salary using printSalary method:
"<<endl;
off1.printSalary(); //printing salary using the
function
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2.THIS CODE ASSIGNS SALARY BASED ON THE TOPSKILL AND DEPARTMENT OF STAFF AND OFFICER RESPECTIVELY GIVEN
#include<iostream>
#include<string>
using namespace std;
class Staff
{
public:
int Id;
string Name;
string Phonenumber;
string Address;
int Age;
void printSalary()
{
cout<<"Salary:"<<endl;
}
};
class Employee:public Staff
{
public:
string Top_Skill;
void printSalary()
{
if(Top_Skill=="coder"){
cout<<"Salary:"<<10000<<endl;
}
}
};
class Officer:public Staff
{
public:
string department;
void printSalary()
{
if(department=="headOffice"){
cout<<"Salary:"<<5000<<endl;
}
}
};
int main()
{
Employee emp1;
emp1.Id = 10;
emp1.Name = "empName";
emp1.Address = "London";
emp1.Age= 25;
emp1.Phonenumber = "0401231230";
emp1.Top_Skill = "coder";
cout<<"Employee Name:
"<<emp1.Name<<endl;
cout<<"Employee id:
"<<emp1.Id<<endl;
cout<<"Employee Age:
"<<emp1.Age<<endl;
cout<<"Employee Address:
"<<emp1.Address<<endl;
cout<<"Employee PhoneNum:
"<<emp1.Phonenumber<<endl;
cout<<"Employee Salary using printSalary method:
"<<endl;
emp1.printSalary();
cout<<endl<<"**********************"<<endl<<endl;
Officer off1;
off1.Id = 12;
off1.Name= "offName";
off1.Address = "Bankok";
off1.Age = 27;
off1.Phonenumber = "0400000000";
off1.department = "headOffice";
cout<<"Officer Name:
"<<off1.Name<<endl;
cout<<"Officer id:
"<<off1.Id<<endl;
cout<<"Officer Age:
"<<off1.Age<<endl;
cout<<"Officer Address:
"<<off1.Address<<endl;
cout<<"Officer PhoneNum:
"<<off1.Phonenumber<<endl;
cout<<"Officer Salary using printSalary method:
"<<endl;
off1.printSalary();
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<iostream> // for taking input output
#include<string> // for string operation
using namespace std; // for cout cin
class Staff //creating a class Staff
{
public: // to access all these fields they must be
public
int Id; // creating a id
member
string Name; //creating a Name
of type string
string Phonenumber; //
creating a phonenumber of type string
string Address; // creating a
address field
int Age; //creating age
fiels
double salary; //creating
salary of type double
void printSalary() // creating
a function printSalary
{
cout<<"Salary:"<<salary<<endl; // printing
salary
}
};
class Employee:public Staff //inheriting Staff class
{
public:
string Top_Skill; // creating
a Top_Skill field of type string
void printSalary()
{
if(Top_Skill=="coder"){
//IF
THE Top_Skill IS CODER THEN SALARY IS 5000
cout<<"Salary:"<<10000<<endl;
}
}
};
class Officer:public Staff //inheriting Staff class
{
public:
string department; // creating
a department field of type string
void printSalary()
{
if(department=="headOffice"){
//IF
THE DEPARTMENT IS HEADOFFICE THEN SALARY IS 5000
cout<<"Salary:"<<5000<<endl;
}
}
};
int main()
{
Employee emp1; //creating a object of employee
class
emp1.Id = 10; //assigning id as 10
emp1.Name = "empName"; //assigning Name as
empName
emp1.Address = "London"; //assigning address as
London
emp1.Age= 25; //assigning age as 25
emp1.Phonenumber = "0401231230"; //assigning
PhoneNumber
emp1.Top_Skill = "coder";
//printing all details of emp
cout<<"Employee Name:
"<<emp1.Name<<endl;
cout<<"Employee id:
"<<emp1.Id<<endl;
cout<<"Employee Age:
"<<emp1.Age<<endl;
cout<<"Employee Address:
"<<emp1.Address<<endl;
cout<<"Employee PhoneNum:
"<<emp1.Phonenumber<<endl;
cout<<"Employee Salary using printSalary method:
"<<endl;
emp1.printSalary();
cout<<endl<<"**********************"<<endl<<endl;
//printing line
Officer off1; //creating a object of officer
class
off1.Id = 12; //assigning id as 12
off1.Name= "offName"; //assigning Name offName
off1.Address = "Bankok"; //assigning address as
Bankok
off1.Age = 27; //assigning age as 27
off1.Phonenumber = "0400000000"; // assigning
PhoneNumber
off1.department = "headOffice";
//printing all details of officer
cout<<"Officer Name:
"<<off1.Name<<endl;
cout<<"Officer id:
"<<off1.Id<<endl;
cout<<"Officer Age:
"<<off1.Age<<endl;
cout<<"Officer Address:
"<<off1.Address<<endl;
cout<<"Officer PhoneNum:
"<<off1.Phonenumber<<endl;
cout<<"Officer Salary using printSalary method:
"<<endl;
off1.printSalary(); //printing salary using the
function
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IF YOU STILL HAVE ANY DOUBTS OR PROBLEM WITH THE SOLUTION PLEASE PLEASE COMMENT BELOW I WILL DEFINETLY HELP YOU
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////