0% found this document useful (0 votes)
52 views7 pages

DSA Lab Report 2 Anas Zohrab

1. This lab report discusses implementing object-oriented programming concepts like encapsulation, inheritance, and polymorphism in C++. 2. The code creates classes for different employee types like salaried, hourly, and commissioned employees. 3. The code shows the use of inheritance by creating subclasses that inherit from a base employee class. It then demonstrates polymorphism by calculating salaries for different employee types.

Uploaded by

AnasAbbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views7 pages

DSA Lab Report 2 Anas Zohrab

1. This lab report discusses implementing object-oriented programming concepts like encapsulation, inheritance, and polymorphism in C++. 2. The code creates classes for different employee types like salaried, hourly, and commissioned employees. 3. The code shows the use of inheritance by creating subclasses that inherit from a base employee class. It then demonstrates polymorphism by calculating salaries for different employee types.

Uploaded by

AnasAbbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Data Structures and Algorithims

CIS-318
Lab Report no.2
Spring 2023
Obtained Marks
Total Marks

Lab Engineer Signature &


Comments

Student Name
1. Anas Zohrab

Section: A (Electronics)
Experiment No: 02 Date of Submission:
Feburary-28-2023
Experiment Title:
Introduction to Object Oriented Programming (OOP) in C++
Batch: Teacher:
BSEE 2019-23 Dr. Kamran Safdar
Semester Lab Engineer:
7th Mr. Ghaznooq Ahmad

Department of Electrical Engineering


2 Lab 2 : Introduction to Object Oriented Programming (OOP) in C++

2.1 Title: Introduction to Object Oriented Programming (OOP) in


C++
2.2 Objectives:
• Understanding of OOP concepts including data abstraction, encapsulation, inheritance
and polymorphism
• Implementing the OOP concepts in C++
• Understanding of difference between the static vs dynamic arrayCode:

2.3 Introduction:
Data abstraction, encapsulation, inheritance and polymorphism are important OOP concepts
for modelling of real-world problem

2.4 Results
1. Lab Task Code:
#include <iostream>;
using namespace std;

class employee {
public:
string name;
int taxRate;
void getName(string name) {
name;

int calcSalary() {
int salary = 0;
return salary;
}

};

class Salaried_Employee : public employee {


public:
int taxrate = 0.1;
void getName(string name) {
name;

}
int calcSalary() {
int salary = 1000 * (1 - taxrate);
return salary;
}
};

class Hourly_Employee : public employee {


3 Lab 2 : Introduction to Object Oriented Programming (OOP) in C++

public:
int taxrate = 0.1;
int hours = 5;
int hourlyRate = 5;
int SES;
int calcSalary() {

int salary = hours*hourlyRate*(1-taxrate);


return salary;

};
class Commissioned_Employee : public employee{
public:
int sales = 4, commissionrate = 10;
int calcSalary() {
int taxrate = 0.1;
int salary = sales * commissionrate * (1 - taxrate);
return salary;
}

};
void main()

{
Commissioned_Employee Obj1;
Hourly_Employee obj2;
Salaried_Employee obj3;
obj3.getName("Anas");
obj3.name = "Anas";

cout << obj3.name <<" Salary is " << obj3.calcSalary() << endl;

obj2.name = "Idrees";

cout << obj2.name << " Salary is " << obj2.calcSalary() << endl;

Obj1.name = "Mohsin";

cout << Obj1.name << " Salary is " << Obj1.calcSalary() <<
endl;
return ;
}
4 Lab 2 : Introduction to Object Oriented Programming (OOP) in C++

2. Lab Task Result:

Figure 1 output for the code.

2.5 Home tasks

2.4.3 Q2: Write the time-complexity of your code.


Time complexity of code is O(1). It is just adding numbers thus number of steps donot
depend on the size of the input. Numbder of steps remain constant.

2.4.4 Q3: Give brief description of encapsulation, inheritance,


polymorphism and data abstraction.Also show the use of these concepts in
the given problem.
1. Data Abstraction:

Data abstraction is one of the most essential and important features of object-oriented
programming in C++. Abstraction means displaying only essential information and
hiding the details. Data abstraction refers to providing only essential information about
the data to the outside world, hiding the background details or implementation.

Consider a real-life example of a man driving a car. The man only knows that pressing
the accelerators will increase the speed of car or applying brakes will stop the car but
he does not know about how on pressing accelerator the speed is actually increasing,
he does not know about the inner mechanism of the car or the implementation of
accelerator, brakes etc. in the car. This is what abstraction is. We can implement
Abstraction in C++ using classes. Class helps us to group data members and member
functions using available access specifiers. A Class can decide which data member will
be visible to outside world and which is not.

2.Encapsulation:

It is defined as wrapping up of data and information under a single unit. In Object


Oriented Programming, Encapsulation is defined as binding together the data and the
functions that manipulates them. Consider a real-life example of encapsulation, in a
company there are different sections like the accounts section, finance section, sales
5 Lab 2 : Introduction to Object Oriented Programming (OOP) in C++

section etc. The finance section handles all the financial transactions and keep records
of all the data related to finance. Similarly, the sales section handles all the sales related
activities and keep records of all the sales. Now there may arise a situation when for
some reason an official from finance section needs all the data about sales in a particular
month. In this case, he is not allowed to directly access the data of sales section. He will
first have to contact some other officer in the sales section and then request him to give
the particular data. This is what encapsulation is. Here the data of sales section and the
employees that can manipulate them are wrapped under a single name “sales section”.

3. Inheritance:

Inheritance is one of the key features of Object-oriented programming in C++. It


allows us to create a new class (derived class) from an existing class (base class).

• The derived class inherits the features from the base class and can have
additional features of its own.

• Inheritance is an is-a relationship. We use inheritance only if an is-a relationship


is present between the two classes.

4. Polymorphism:

Polymorphism means having many forms. Typically, polymorphism occurs when


there is a hierarchy of classes and they are related by inheritance. C++ polymorphism
means that a call to a member function will cause a different function to be executed
depending on the type of object that invokes the function.

A real-life example of polymorphism, a person at the same time can have different
characteristics. Like a man at the same time is a father, a husband, an employee. So, the
same person possesses different behavior in different situations. This is called
polymorphism. Polymorphism is considered as one of the important features of Object-
Oriented Programming.

2.4.5 Q4: What is the difference between static and dynamic array.

A static array is an array that has a fixed size and cannot be resized. This means that the number
of elements it can hold is determined at compile time and cannot be changed during runtime. On
the other hand, a dynamic array is an array that can change its size during runtime. This means
that elements can be added or removed from it as needed.

Code for Dynamic Array

#include <iostream>;
using namespace std;

void main()

{
6 Lab 2 : Introduction to Object Oriented Programming (OOP) in C++

int size;
cout << "enter size of array";
cin >> size;
int* myArray = new int[size];

for (int i = 0;i < size;++i)


{
cout << "Array[" << i << "]";
cin >> myArray[i];

for (int i = 0;i < size;++i)


{
cout<< myArray[i]<<endl;

return;
}

Output for dynamic Array.

2.6 Discussion and Conclusion:


The lab report discusses the implementation of OOP concepts including data abstraction,
encapsulation, inheritance and polymorphism in C++. The code contains the implementation
of these concepts through the creation of classes and objects. The report also highlights the
difference between static and dynamic arrays. In the lab task, three types of employees are
created, each with different characteristics and methods. The Salaried_Employee,
Hourly_Employee and Commissioned_Employee classes are inherited from the Employee
class. Each employee has a unique tax rate, sales rate, hourly rate, etc., which is used to
calculate their salaries. The code also shows the use of encapsulation, where the data is
wrapped under a single unit, and only the essential information is displayed to the outside
wold, hiding the background details or implementation.

In conclusion, the lab report provides a clear understanding of OOP concepts and their
implementation in C++. It shows the use of inheritance to create new classes from existing
classes, and how polymorphism can be used to execute different functions depending on the
7 Lab 2 : Introduction to Object Oriented Programming (OOP) in C++

type of object that invokes it. The report also highlights the importance of encapsulation in
hiding the implementation details, and data abstraction in displaying only essential
information. The lab task code demonstrates the use of these concepts in a real-world
problem of calculating the salaries of different types of employees. Overall, this lab report is
a great example of how OOP concepts can be used to solve real-world problems in a simple
and efficient way.

2.7 References:
• "W3Schools - The World's Largest Web Development Site." W3Schools.
https://www.w3schools.com/.
• "W3Schools - The World's Largest Web Development Site." W3Schools.
https://www.w3schools.com/.

You might also like