Object orented Programming (OOP)
LAB 4 Manual
Khawaja Ammad BadAr
khawaja.badar@iqraisb.edu.pk
Computing and Technology Department, IQRA University Islamabad – H9 Campus - Pakistan
Object Oriented Programming (OOP) Lab Manual
Table of Contents
1. Introduction to Object Oriented Programming in C++....................................................................3
a. Why Use OOP?...................................................................................................................3
b. Example: Real-World Objects in OOP................................................................................3
2. Classes vs Structures.......................................................................................................................3
a. Example Class vs Structure:...............................................................................................3
3. Classes and Objects in C++..............................................................................................................4
4. Access Specifiers in C++..................................................................................................................4
a. Example Using Public and Private Members......................................................................5
5. Encapsulation in C++.......................................................................................................................5
6. Getters and Setters (Accessors & Mutators)...................................................................................5
7. Defining Member functions outside the class................................................................................6
8. Lab Tasks........................................................................................................................................7
1|Page
Instructor: Khawaja Ammad Badar | Email: khawaja.badar@iqraisb.edu.pk
Object Oriented Programming (OOP) Lab Manual
Introduction to Object Oriented Programming in C++
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of
objects, which encapsulate data (attributes) and functions (methods) that operate on the data.
Why Use OOP?
o Organized & Reusable Code – Reduces redundancy and increases modularity
o Easier Maintenance & Scalability – Code is structured and easy to modify
o Real-World Modeling – Represents real-world entities more effectively
Example: Real-World Objects in OOP
Object Attributes (Data Members) Methods (Funtions)
Car Color, Model, Speed Start(), Stop()
Student Name, Age, Grade Study(),GetMarks()
Classes vs Structures
Features Class Structure
Access Specifier Private by default Public by default
Less Secure, primary for data
Encapsulation Support Full Encapsulation
grouping
Used for creating objects with
Usage Used for grouping related Data
behaviors
Example Class vs Structure:
Header File/Library for input output operations
#include <iostream>
Define structure using namespace std;
Enable direct use of Cin and Cout
“StudentStruct”
struct StudentStruct {
Different type of variables string name;
int age;
Define Class };
Access Specifier
“StudentClass”
class StudentClass {
private:
Different type of private string name; Parameters of functions
variables int age
public:
void setData(string n, int a) {
Create Setter Method
Here we set the values of class name = n;
for setting the values of
variables i.e. “name”, “age” age = a; Class variable
}
void display() {
Create Method to display name
and age2of|Student
Page
Instructor: Khawaja Ammad Badar | Email: khawaja.badar@iqraisb.edu.pk
Object Oriented Programming (OOP) Lab Manual
cout << "Name: " << name << " Age: " << age << endl;
}
};
Classes and Objects in C++
A class is a blueprint for objects. An object is an instance of a class.
Define Class “Car”
#include <iostream>
using namespace std;
class Car { Access Specifier
public:
string brand; Here we declare class variables
int speed; i.e. “brand”, “speed”
void display() {
Create Method to display
brand and speed of Car cout << "Brand: " << brand << " Speed: " <<
speed << endl;
}
};
Creating Car class object
int main() {
Car myCar; // Object creation
Setting values of Class variable myCar.brand = "Toyota";
using class object myCar.speed = 120; Using class object call class method
myCar.display();
return 0;
}
Access Specifiers in C++
Access Specifier Description
Public Members are accessible from anywhere
Private Members are accessible only with in a class
Members are accessible with in a class and
Protected
derived class
3|Page
Instructor: Khawaja Ammad Badar | Email: khawaja.badar@iqraisb.edu.pk
Object Oriented Programming (OOP) Lab Manual
Example Using Public and Private Members
class BankAccount {
private:
Private Data members
of class double balance;
public:
void setBalance(double b) {
balance = b;
} Public members Methods
double getBalance() { of class
return balance;
}
};
Encapsulation in C++
Encapsulation means bundling data and methods together while restricting direct
access to data.
#include <iostream>
using namespace std;
class Person {
private:
string name;
public:
void setName(string n) { Encapsulation achieve via data members
name = n; (restricting direct access using private) and
} members methods are bound together
string getName() {
return name;
}
};
Getters and Setters (Accessors & Mutators)
o Mutators (Setters): Modify private data members.
o Accessors (Getters): Retrieve values without modifying them.
4|Page
Instructor: Khawaja Ammad Badar | Email: khawaja.badar@iqraisb.edu.pk
Object Oriented Programming (OOP) Lab Manual
#include <iostream>
Class Rectangle
using namespace std;
Created
class Rectangle { Setter Method created to set
private: values of with and length
int length, width;
public:
void setDimensions(int l, int w) {
length = l;
width = w;
}
int getLength() const { Getter methods created to get the values of
return length; length and width, that was assigned by
} setter method
int getWidth()const { Const is used to prevent changes (data
return width;
members value) from getters
}
};
Defining Member functions outside the class
o keep the class definition clean, member functions can be defined outside the class
using the scope resolution operator ::
class Car {
private:
string brand;
public: Member Methods declared inside the class
void setBrand(string b);
string getBrand();
};
void Car::setBrand(string b) {
brand = b;
}
string Car::getBrand() { Member Methods defined outside the class
return brand;
}
5|Page
Instructor: Khawaja Ammad Badar | Email: khawaja.badar@iqraisb.edu.pk
Object Oriented Programming (OOP) Lab Manual
Lab Tasks
1. Employee Management System.
Scenario: A company is expanding and needs a system to store and manage employee
details efficiently. Currently, employee records are maintained manually, which is time-
consuming and prone to errors. Your task is to create a simple Employee Management
System in C++ using basic OOP concepts.
The system should store employee details like:
Employee ID
Name
Department
Salary
Requirements:
1. Create an Employee class with the above attributes. Make sure the attributes
are private so they cannot be accessed directly.
2. Use getters to retrieve employee details (e.g., getEmployeeID(), getName(), etc.).
3. Use setters to modify employee details (e.g., setName(), setSalary(), etc.).
4. In the main() function:
o Create objects of the Employee class to represent employees.
o Use setters to assign values to employee attributes.
o Use getters to display employee details.
Concepts Used:
Classes & Objects: To represent employees.
Encapsulation: To protect sensitive employee data.
Getters & Setters: For controlled access to private attributes.
Access Specifiers: To manage data visibility and security.
2. Library Book Management System
Scenario: A university library needs an efficient system to manage its collection of
books. Currently, tracking available and borrowed books is challenging due to manual
record-keeping. The library wants an automated Library Book Management
System that will store the following details for each book:
Book Title
Author
6|Page
Instructor: Khawaja Ammad Badar | Email: khawaja.badar@iqraisb.edu.pk
Object Oriented Programming (OOP) Lab Manual
ISBN
Availability Status (e.g., Available or Borrowed)
The system should allow librarians to:
1. Add new books to the library.
2. Check the availability of a specific book.
3. Update the status of a book when it is borrowed or returned.
This system will improve record management, reduce errors, and provide easy access to
book information.
Requirements:
1. Create a Book class with the following:
o Private attributes: title, author, ISBN, and availabilityStatus.
o Public getters and setters for each attribute.
2. In the main() function:
o Create an array or list to store multiple books.
o Allow the user to:
Add new books.
Display the details of all books.
Update the availability status of a book (e.g., mark it as borrowed
or returned).
3. Ensure the program is user-friendly and demonstrates the use of encapsulation,
getters, setters, and access specifiers.
Concepts Used:
Classes & Objects: To structure book information.
Encapsulation: To ensure secure access to book details.
Getters & Setters: For managing book availability.
Access Specifiers: To maintain data integrity.
3. Bank Account System.
Scenario: Banks handle a high volume of transactions daily, making it essential to have a
secure and well-structured system for managing customer accounts. To streamline
operations, a bank wants to develop a Bank Account System that securely stores the
following details for each account:
7|Page
Instructor: Khawaja Ammad Badar | Email: khawaja.badar@iqraisb.edu.pk
Object Oriented Programming (OOP) Lab Manual
Account Number
Account Holder Name
Balance
The system should allow customers to:
1. Deposit money into their account.
2. Withdraw funds (with balance validation to prevent overdrawing).
3. Check their account details.
. Requirements:
1. Create a BankAccount class with the following:
o Private attributes: accountNumber, accountHolderName, and balance.
o Public methods:
deposit(amount): To add funds to the account.
withdraw(amount): To deduct funds from the account (with
validation to ensure sufficient balance).
displayDetails(): To display account details.
o Public getters and setters for accountNumber and accountHolderName.
2. In the main() function:
o Create objects of the BankAccount class to represent customer accounts.
o Demonstrate the functionality of depositing, withdrawing, and displaying
account details.
3. Ensure the program is user-friendly and demonstrates the use of encapsulation,
getters, setters, and access specifiers.
Concepts Used:
Classes & Objects: To model bank accounts.
Encapsulation: To prevent unauthorized modifications to account balances.
Getters & Setters: For secure access to account details.
Access Specifiers: To restrict direct manipulation of sensitive data.
8|Page
Instructor: Khawaja Ammad Badar | Email: khawaja.badar@iqraisb.edu.pk