Develop a C++ program implementing hierarchical inheritance upon a base class "Employee", and derived classes "Full-time" containing basic salary and allowances for calculating the salary of the employee and
#include <iostream>
class Employee
{
private:
int age = 0;
std::string surname = "";
std::string name = "";
public:
Employee();
~Employee();
void set_age(const int& age)
{
this->age = age;
}
void set_name(const std::string & name)
{
this->name = name;
}
void set_surname(const std::string& surname)
{
this->surname = surname;
}
const std::string& get_name()const
{
return name;
}
const std::string& get_surname()const
{
return surname;
}
const int& get_age()
{
return age;
}
virtual void input_info()
{
std::cout << "Enter name : ";
std::cin >> name;
std::cout << "Enter surname : ";
std::cin >> name;
std::cout << "Enter age : ";
std::cin >> age;
}
virtual void output_info()
{
std::cout << "Name : " << name << " , Surname : " << surname << " , Age : " << age << std::endl;
}
};
Employee::Employee()
{
}
Employee::~Employee()
{
}
class PartTimeEmployee : public Employee
{
private:
int rate_per_day = 0;
public:
PartTimeEmployee();
~PartTimeEmployee();
void set_rate_per_day(const int& rate_per_day)
{
this->rate_per_day = rate_per_day;
}
const int& get_rate_per_day()const
{
return rate_per_day;
}
virtual void input_info()
{
std::string tmp = "";
std::cout << "Enter name : ";
std::cin >> tmp;
this->set_name(tmp);
std::cout << "Enter surname : ";
std::cin >> tmp;
this->set_surname(tmp);
int age = 0;
std::cout << "Enter age : ";
std::cin >> age;
this->set_age(age);
std::cout << "Enter rate per day : ";
std::cin >> rate_per_day;
}
virtual void output_info()
{
std::cout << "Name : " << this->get_name() << " , Surname : " << this->get_surname() << " , Age : " << this->get_age()<<" , Rate per day : "<<rate_per_day << std::endl;
}
};
PartTimeEmployee::PartTimeEmployee()
{
}
PartTimeEmployee::~PartTimeEmployee()
{
}
class FullTimeEmployee : public Employee
{
private:
int rate_per_month = 0;
public:
FullTimeEmployee();
~FullTimeEmployee();
void set_rate_per_month(const int& rate_per_month)
{
this->rate_per_month = rate_per_month;
}
const int& get_rate_per_month()const
{
return rate_per_month;
}
virtual void input_info()
{
std::string tmp = "";
std::cout << "Enter name : ";
std::cin >> tmp;
this->set_name(tmp);
std::cout << "Enter surname : ";
std::cin >> tmp;
this->set_surname(tmp);
int age = 0;
std::cout << "Enter age : ";
std::cin >> age;
this->set_age(age);
std::cout << "Enter rate per month : ";
std::cin >> rate_per_month;
}
virtual void output_info()
{
std::cout << "Name : " << this->get_name() << " , Surname : " << this->get_surname() << " , Age : " << this->get_age() << " , Rate per month : " << rate_per_month << std::endl;
}
};
FullTimeEmployee::FullTimeEmployee()
{
}
FullTimeEmployee::~FullTimeEmployee()
{
}
int main()
{
Employee* person;
std::cout << "Enter 1 to create Full Time Employee , 2 to create Part Time Employee\nEnter value : ";
int answer = 0;
std::cin >> answer;
if (answer == 1)
{
person = new FullTimeEmployee();
}
else if (answer == 2)
{
person = new PartTimeEmployee();
}
else
{
std::cout << "Wrong value\n";
system("pause");
return 0;
}
person->input_info();
person->output_info();
delete person;
system("pause");
return 0;
}
Comments
Leave a comment