DSA Lab Report 2 Anas Zohrab
DSA Lab Report 2 Anas Zohrab
CIS-318
Lab Report no.2
Spring 2023
Obtained Marks
Total Marks
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
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;
}
};
}
int calcSalary() {
int salary = 1000 * (1 - taxrate);
return salary;
}
};
public:
int taxrate = 0.1;
int hours = 5;
int hourlyRate = 5;
int SES;
int calcSalary() {
};
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++
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:
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:
• The derived class inherits the features from the base class and can have
additional features of its own.
4. Polymorphism:
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.
#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];
return;
}
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/.