Introduction To Object Oriented Programming (OOP) in C++: Objectives
Introduction To Object Oriented Programming (OOP) in C++: Objectives
Introduction To Object Oriented Programming (OOP) in C++: Objectives
Objectives:
The objectives of this lab are:
• Understanding of OOP concepts including encapsulation, inheritance, polymorphism
and data abstraction.
• Developing an OOP model for a given problem.
• Implementing the OOP concepts in C++.
Problem Statement:
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.
Abstract:
This lab focuses on key concepts of Object-Oriented Programming like Encapsulation,
inheritance, polymorphism and data abstraction. We apply these concepts to model and
solve a real-world problem consisting of developing a simple payroll system for different
kinds of employees.
OOP Model:
The OOP model for the given problem used is as follows:
Parent Class
Employee
name getName()
taxRate CalcSalary()
hourlyRate Com_rate
Derived Classes
1
Code:
#include <iostream>
using namespace std;
//Parent class Employee
class Employee {
public:
double taxRate; //Public attribute specifying the tax rate
private:
string name; //Private attribute specfying the name
public:
string getName() //Public method to access the private
attribute name
{
return name;
}
//Method to calculate Salary
//Virtual method so that it can be overridden
virtual double CalcSalary()
{
return 0;
}
//Parent class constructor to initialize instances
Employee(string nam, double rate)
{
name = nam;
taxRate = rate;
}
//Parent class default constructor
Employee()
{
name = "";
taxRate = 0;
}
};
//Child class Salaried_Employee inherited from Parent class Employee
class Salaried_Employee : public Employee {
public:
double Salary; //Public attribute salary
//Child class constructor, passing the Parent class attributes to
Parent constructor
Salaried_Employee (string n, double tax, double Sal) : Employee
(n, tax)
{
Salary = Sal;
}
2
//Child class default constructor
Salaried_Employee () : Employee()
{
Salary = 0;
}
//overridden instance of Parent class method to calculate salary
virtual double CalcSalary()
{
double tax = taxRate * Salary;
return (Salary - tax);
}
};
3
public:
int Sales;
float Commision_rate;
//Child class constructor, passing the Parent class
attributes to Parent constructor
Comissioned_Employee(string n, double tax, int sal, float rate):
Employee(n, tax)
{
Sales = sal;
Commision_rate = rate;
}
//Child class default constructor
Comissioned_Employee():Employee()
{
Sales = 0;
Commision_rate = 0;
}
//overridden instance of Parent class method to calculate salary
double CalcSalary()
{
double sal = Sales*Commision_rate;
double tax = sal*taxRate;
return (sal-tax);
}
};
int main() {
//Creating three variables of parent class
Employee *members[2];
//Creating an instance of each Child Classe
members[0] = new Salaried_Employee("Ali", 0.15, 20050.0);
members[1] = new hourly_Employee("Umer", 0.13, 180, 84.67);
members[2] = new Comissioned_Employee("Talha", 0.17, 782, 26.89);
//Looping to print the names and salary of each employee
for (int i = 0; i<3; ++i)
{
cout << "The salary of " << members[i]->getName()<<" is " <<
members[i]->CalcSalary()<<endl;
}
return 0;
//End of program
}
4
Output:
The output after executing the above code is shown below:
Time Complexity:
Time complexity of a code refers to the number of execution cycles taken for the execution.
In simple words, this is proportional to the loops used within the used. In the code shown
above, there is a single loop that executes three times (equal to the number of Employee
objects) so the time complexity of the code is O(n) where n refers to the number of
Employee objects.
OOP Concepts:
Brief descriptions of various OOP Concepts are given below:
➢ Encapsulation:
This is the basic OOP concept that refers to the binding of the data and member functions
together within a class in a such a way as to keep them away from outside interference.
This is done to restrict the access to critical data elements. In our case, this is done by using
private specifier for the attribute ‘name’.
➢ Inheritance:
Inheritance refers to the process of making derived classes from a parent class. The main
advantage of inheritance lies in the fact that the child class inherits and thus can use all the
features of the parent class while providing additional features. This means that the
common functionality of different classes can be kept in a common parent class whereas
their individual characteristics can be added within the child classes. This helps in reducing
code space and complexity.
5
➢ Polymorphism:
In simple terms, polymorphism refers to the existence of a material in several different
forms. In OOP, this is directly related to the use of virtual functions which allow us to use
the same function name in different classes. A call to such a function then depends upon
the object calling it and the corresponding function is executed. In our case, the CalcSalary
is an example of this concept being implemented which has a different body in each class.
➢ Data Abstraction:
Data Abstraction refers to the technique of separation of interface (the functions) and their
implementation. This greatly improves the data integrity and ensures that external
interference upon the implementation of the functions is greatly reduced. This is a key
feature of Classes as they allow us to protect the different attributes and the member
functions as required.
Conclusions:
In this lab we used the basic OOP features to solve a simple problem of developing a
payroll system. We used the key concepts of data encapsulation and abstraction,
inheritance of classes and achieved polymorphism through the use of virtual function. We
then also considered the time complexity of our code. This provides us a basic foundation
for our further study of data structures later on.