You are on page 1of 6

Data Structure and Algorithm (DSA)

Lab # 01

Submitted to:
Sir Tanveer Ali

Submitted by:
Warda-tu-Zahra
BSEE (17-21)
Section A
Submission Date:
10-03-2021

Pakistan Institute of Engineering and Applied


Sciences (PIEAS)
Develop a simple payroll application for a company; there are
three kinds of employees in the system: salaried employee,
hourly employee, and commissioned employee. The system
should take input as an array containing employee objects,
calculates salary.

Question 1:
Draw the OOP Model for the above-mentioned problem.

Employee
Variables:
Name
taxrate
Functions:
clacSalary
getname
setname

HourlyEmployee ComissionedEmployee
SalariedEmployee
Variables: Variables:
Variables:
Hours Sales
Salary
hourlyRate ComissionRate
Functions:
Functions: Functions:
clacSalary
clacSalary clacSalary

Question 2:
Write the C++ code using OOP concepts for the given problem.

Code:
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
// Base class class public: Employee { private:
string name; float taxRate;
void setName(string n) {
name = n;
}
string getName() {
return name;
}
virtual float calcSalary() {
return 10000;
}
Employee(string nam){
name = nam; taxRate = 0.5; calcSalary();
}
Employee() { string name = ""; float taxRate = 0;
calcSalary();
}

};

// Derived class
class SalariedEmployee : public Employee { private:
int salary; public:
SalariedEmployee(string n, int salarySal) {
salary = salarySal; setName(n);
}
SalariedEmployee() {
salary = 0;
setName("");
}
virtual float calcSalary() { float tax = salary*0.06;
return salary - tax;
}
};
// Derived class
class HourlyEmployee : public Employee { private:
float sal, tax; int hours,ret; int hourlyRate=600; public:
HourlyEmployee() {
hours = 1; setName("");
};
HourlyEmployee(string n, int hoursFromMain) { hours = hoursFromMain;
setName(n);
};
virtual float calcSalary() { sal = hours*hourlyRate; tax =
sal*0.05; return sal - tax;
}
};
// Derived class
class CommissionedEmployee : public Employee { private:
int sales;
float commissionRate=600; public:
CommissionedEmployee() {
sales = 1;
setName("");
};
CommissionedEmployee(string n, int salesFromMain) {
setName(n);
sales = salesFromMain;
};
virtual float calcSalary() {
float sal, tax; sal = sales*commissionRate; tax =
sal*0.04;
return sal - tax;
}
}; int main() {
int numOfEmp,EmpType,salary,hours,sales; string name;
cout << "Enter no. of Employees.\n";
cin >> numOfEmp; Employee* arrEmp[10];
for (int i = 0; i < numOfEmp; i++)
{
cout << "Enter name of Employee "<<i+1<<"\n";
cin >> name;
cout << "Enter 1 for Salaried, 2 for Hourly and 3 for Commissioned Employee.\n";
cin >> EmpType;
if (EmpType == 1)
{
cout << "Enter salary for Employee.\n";
cin >> salary;
arrEmp[i]= new SalariedEmployee(name, salary);
}
else if (EmpType == 2)
{
cout << "Enter total no. of hours employee worked.\n";
cin >> hours;
arrEmp[i] = new HourlyEmployee(name, hours);
}
else if (EmpType == 3)
{
cout << "Enter total no. of sold objects.\n"; cin >> sales;
arrEmp[i] = new CommissionedEmployee(name, sales);
} else {
cout << "Error. Enter only 1, 2, or 3 for type.\n"; }
}
cout << "Employee name\t Salary\n" << endl; for (int i = 0; i < numOfEmp; i++) {

cout << arrEmp[i]->getName()<<"\t\t"<< arrEmp[i]->calcSalary()<<endl;


}
_getch();
return 0;
}
Q3: Output:

Question 4:
What is the importance of using virtual functions?

Virtual functions are here to introduce the concept of polymorphism. If they are not used, in a pointer of
base class type, even if array contains derived class function, base class function is called and so we
cannot achieve correct result i.e., correct function riding is not achieved. Virtual functions avoid that
issue. They ensure that a correct function call is made regardless of the type of pointers used in function
call.

Question 5:
Write the time-complexity of your code.

 time complexity estimates how an algorithm performs regardless of the kind of machine it


runs on. You can get the time complexity by “counting” the number of operations performed by
your code. This time complexity is defined as a function of the input size n using Big-O
notation. n indicates the input size, while O is the worst-case scenario growth rate function.

Question 6:
Polymorphism:
 In programming, polymorphism is a feature that allows one interface to be used for a general
class of actions. The idea of polymorphism is the ability of the same object to take multiple
forms.

Abstraction:
abstraction means the art of representing the essential features without concerning about
the background details.

The reason why abstraction is considered as one of the important concepts is:
1. It reduces the complexity of viewing things.
2. Avoids code duplication and increases reusability.
3. Helps to increase security of an application or program as only important details are
provided to the user.

Encapsulation:
It can also be said data binding. Encapsulation is all about binding the data variables and
functions together in class.
Inheritance
Inheritance is a way to reuse once written code again and again. The class which is inherited is
called the Base class & the class which inherits is called the Derived class. They are also called
parent and child class.
So when, a derived class inherits a base class, the derived class can use all the functions which
are defined in base class, hence making code reusable.

Discussion:
In this lab, use of classes were introduced and many important concepts were used. Base and derived
classes were made. Concept of Inheritance was use. Constructors are also used and the concept of
dynamic memory allocation and polymorphism is also used while writing the above code.

You might also like