Oop Labmanual
Oop Labmanual
LAB MANUAL
CLO–1 Explain the fundamental principles of object-oriented design, history, advantages, and key
programming concepts including classes, objects, and data encapsulation. (C2 )
CLO–2 Apply object-oriented features such as constructors, destructors, access modifiers, static
members, function overloading, operator overloading, and relationships (composition,
aggregation, inheritance). (C3)
CLO–3 Analyze advanced concepts like multiple inheritance, polymorphism, abstract classes,
interfaces, generic programming (templates), and the Standard Template Library (STL).
(C4)
CLO–4 Implement object streams, serialization, and exception handling mechanisms in real-world
applications. (C3)
PLO PLO
CLOs/ PLOs PLO1 PLO2 PLO3 PLO4 PLO5 PLO6 PLO7 PLO8 9 10
CLO1 ✔
CLO2 ✔
CLO3 ✔
CLO4 ✔
List of Experiments
12 Open-Ended Lab 4
Implement file I/O / persistence: save objects to file and read them back
13 4
Use generic / template classes (e.g. a generic Stack<T>) and Ecxception
14 4
handling
Semester Project if required
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No. 1
1) TITLE:
Introduction to the functions how to write programs with the help of functions
2) OBJECTIVES:
3) Tools Used:
Compiler
A compiler is a software tool that converts a program written in a high-level language into
machine language. It checks the program for errors before execution.
An IDE is a software application that provides all tools needed for programming, such as a code
editor, compiler, debugger, and output window, in one place.
4) THEORY:
Functions are fundamental building blocks of programming. A function represents a logical unit
that performs a specific operation. Instead of writing all code in a single block, functions allow
programmers to divide a program into smaller, manageable modules. This modular approach
simplifies problem-solving and improves program clarity.
Functions enhance reusability, as the same function can be used multiple times without rewriting
code. They also improve maintainability, since changes in functionality can be made in one
place without affecting the entire program. Functions help reduce errors and support collaborative
development by allowing different programmers to work on different modules independently.
1
Riphah College of Computing Object Oriented Programming
Task 2:
Output:
2
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 3:
Apply the concept of line, hash, and dash function.
Output:
3
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 3:
Output:
4
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 4:
Write a program that inputs a number and displays its precessor and successor numbers using function.
Output:
5
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 5:
Write a function which input an integer number and tells whether it is even or odd (using switch).
Output:
6
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 6:
Output:
Task 7:
Write a C++ program by using an example of Passing values to a function.
Output:
7
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Output:
8
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No.2
1) TITLE:
Write a program that creates a function that receives three integers, compares them, and displays
the smallest one.
2) OBJECTIVE:
3) TOOLS USED:
4) THEORY:
9
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 1:
Write a function which takes three integers as parameters and display the smallest integer.
Output:
10
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 2:
Write a program that inputs a number in main function and passes the number to a function. The function
displays table of that number.
Output:
11
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 3:
Write an example of call by reference.
Output:
3
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 4:
Write a program that inputs two integers in main () function and passes the integers to a function by
reference. The function swaps the values. The main () function should display the values before and
after swapping.
Output:
4
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 5:
Write a program that inputs a number in a global variable. The program calls a function that
multiplies the value of global variable by 2. The main function then displays the value of global variable.
Output:
5
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 6:
Write a program that uses a function mul(int, int) to determine for a pair of integers whether the
second integer is a multiple of the first. The function should take two integers’ arguments and return 1
(true) if second is a multiple of the first and 0(false) otherwise. The program should input a series of pairs
of integers.
Output:
6
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 7:
Write a program that inputs two integers. It passes first integer to a function that calculates and
returns its square. It passes second integer to another function that calculates and returns its cube. The
main () functions adds both returned values and displays the result.
Output:
7
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No.3
1) TITLE:
2) OBJECTIVES:
3) TOOLS USED:
OOP Compiler or IDE
An OOP Compiler is a tool that translates object-oriented programs (written in languages like
C++ or Java) into machine code so the computer can execute them. An IDE (Integrated
Development Environment) for OOP provides all the tools needed to write, compile, run, and
debug object-oriented programs in one place. Examples include Visual Studio, Code::Blocks,
and Eclipse.
4) THEORY:
A class is a blueprint or template that defines the properties and behaviors of an object. It contains
data members and member functions. An object is an instance of a class and represents a real-
world entity. Classes provide a structured way to bundle data and functionality together. Objects
interact with each other through method calls, enabling modeling of real-world systems in
software.
8
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 1:
Write a program that declares a class with one integer data member and two members functions:
in () and out (). The function in () should input data into the data member, and out () should
output that data.
Output:
9
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 2:
Write a class Marks with three data members to store three marks. Write three member functions: in () to
input marks, sum () to calculate and return the sum, and avg () to calculate and return the average.
10
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Output
Task 3:
Write a program that declares a class Circle with one data member radius.
The class should have the following member functions: get_radius() – to input the radius, area () – to
calculate and display the area of the circle, and decircum() – to calculate and display the circumference of
the circle.
11
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Output
Task 4:
Write a C++ program that defines a class named Result which stores and processes a student’s
information including roll number, name, and marks in three subjects.
The marks should be stored in an array of integers. The class should include the following
member functions: input () — to input the values of data members (roll number, name, and
marks). show () — to display the values of data members. total () — to calculate and return the
total marks of the student. avg () — to calculate and return the average marks of the student.
The program should create an object of the Result class, call these member functions, and display the
total and average marks of the student.
12
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
13
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Output
14
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 5:
BookID (integer)
Pages (integer)
Price (float)
get () — used to input values for all data members. show () — used to
display the values of all data members. set () — used to assign values to
the data members using parameters. get Price() — used to return the value
of the price.
The program should create two objects of the class and input values for both.
It should then display the details of the most costly book (i.e., the book with the higher price).
15
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Output
16
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No.4
1) TITLE:
Write a program which demonstrates Classes and constructors
2) OBJECIVE:
3) TOOLS USED:
C++ Programming Environment
A C++ Programming Environment is the setup where C++ programs are written, compiled,
executed, and debugged. It usually includes a code editor to write programs, a compiler to
convert the code into machine language, a debugger to find and fix errors, and an output
window to display results. Common C++ environments include Code::Blocks, Dev-C++, and
Visual Studio.
4) THEORY:
17
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 1:
Write a class that displays a simple message on the screen whenever an object of that class is created.
18
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 2:
Write a class that contains two integer data members which are initialized to 100 when an object is
created. The class should have a member function avg() that displays the average of the two data
members.
19
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 3:
Write a class TV that contains attributes of Brand Name, Model, and Retail Price.
Write a method to display all attributes and another method to change the attributes. Also, write a
constructor to initialize all the attributes.
20
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 4:
Write a class that has marks and grade as data members. A constructor with two parameters initializes
these data members with given values. A member function show() displays the values of data members.
Create two objects of the class and display their records.
21
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 5:
Write a C++ program to demonstrate the use of a constructor and a destructor in a class.
22
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No.5
1) TITLE:
Implement method Friend Function, Friend class with programs
2) OBJECTIVES:
To understand controlled access to private data
To learn when friendship is required
3) TOOLS USED:
C++ Compiler
A C++ Compiler is a software tool that translates programs written in C++ into machine
language so the computer can execute them. It also checks the program for syntax errors before
running it.
4) THEORY:
A friend function or friend class is permitted to access private and protected members of another
class. Friendship is explicitly granted and is not inherited. It is useful when two classes or
functions need close cooperation without breaking encapsulation. Friend features provide
flexibility while maintaining controlled access to class internals.
23
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 1:
Write a C++ program to demonstrate how a friend function can access private data members of
two different classes.
24
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 2:
Write a C++ program that demonstrates how one class can be declared as a friend of another
class.
25
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Class A contains two private data members a and b, initialized using a constructor. Class B is
declared as a friend class of A, allowing it to access and display the private data of A
26
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 3:
Output:
27
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 4:
Find Greater Number Using Friend Function
Output:
28
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 5:
Friend Function with Two Classes
Output:
29
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No.6
1) TITLE:
Implement method overloading: same method name but different parameters
2) OBJECTIVES:
3) TOOLS USED:
Object-Oriented Programming Language
4) THEORY:
Abstraction is the process of hiding implementation details and exposing only essential features.
An abstract class cannot be instantiated and contains at least one pure virtual function. It defines a
common interface for derived classes and ensures that they provide specific implementations.
Abstraction helps in reducing complexity and improving code clarity.
30
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
(Students are required to complete all the lab tasks, get it checked by the lab instructor and
attach a copy of it in their lab files)
Task 1:
Output:
31
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
32
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 2:
Write a program to overload the binary operator (+) to add two complex numbers.
Output:
33
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 3:
Write a program to overload the relational operator (==) to compare two objects of a class.
Output:
Task 4:
Write a program to overload the stream insertion (<<) and extraction (>>) operators to input and
output data of a student.
Output:
34
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 5:
Write a program to overload the assignment operator (=) for copying one string object to
another.
Output:
35
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No.7
1) TITLE:
Implement method overriding: same method name but different parameters
2) OBJECTIVES:
3) TOOLS USED:
OOP Compiler
4) THEORY:
Method overloading allows multiple methods with the same name but different parameter lists
within the same class. The compiler determines which method to invoke based on the arguments
passed. Overloading improves program readability and allows the same operation to work with
different data types or input sizes.
.
Tasks: LAB No.7
(Students are required to complete all the lab tasks, get it checked by the lab instructor and attach a copy
of it in their lab files)
Task 1:
Write a program to demonstrate function overriding in C++ where a derived class redefines a function of
the base class.
Output:
37
Riphah International University, Faisalabad Campus
38
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 2:
Write a program to demonstrate function overriding using a base class pointer pointing to a derived
class object.
Output:
39
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 3:
Write a program to demonstrate function overriding using a virtual function so that the correct
function is called at runtime.
Output:
Task 4:
Write a program to demonstrate function overriding in a multi-level inheritance hierarchy.
Output:
40
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No.8
1) TITLE:
Design an inheritance hierarchy (e.g. Person → Employee → Manager) with
overridden methods
2) OBJECTIVES:
The main objective of studying function overriding is to understand how runtime polymorphism
works in C++ and how it enhances the flexibility and reusability of objectoriented programs. It helps
students learn how base and derived classes can share similar interfaces but perform different actions.
The goal is to show how the same function name can behave differently depending on the object type
that invokes it. Another objective is to understand the role of the virtual keyword and the base class
pointer in achieving late binding, where the function call is resolved during program execution instead
of compilation.
4) THEORY:
The objectives of this task are:
41
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
(Students are required to complete all the lab tasks, get it checked by the lab instructor and
attach a copy of it in their lab files)
Task 1:
Write a class Person having attributes id, name, address.
42
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
43
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Output:
44
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 2:
Parent → Child (Calling Parent Constructor)
Statement
45
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Output:
46
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 3:
Move → Move2 (Inheritance using protected)
Statement
47
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
48
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No.9
1) TITLE:
2) OBJECTIVES:
3) TOOLS USED:
OOP Compiler
4) THEORY:
Inheritance allows one class to acquire the properties and behaviors of another class. In
multilevel inheritance, a class is derived from another derived class. This creates a hierarchical
relationship and promotes code reuse, extensibility, and logical organization of software
systems.
49
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
(Students are required to complete all the lab tasks, get it checked by the lab instructor and
attach a copy of it in their lab files)
Task 1:
Design an inheritance hierarchy Person → Employee → Manager.
Each class overrides the display() method.
Output:
50
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 2:
Create a base class Animal and a derived class Dog that overrides a function.
Output:
51
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 3:
Show composition where a Car has an Engine.
Engine cannot exist without Car.
Output:
52
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 4:
Show aggregation where a Library has many Book objects.
Books can exist independently of Library.
Output:
53
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No.10
1) TITLE:
Demonstrate constructor chaining and use of super / base class constructors
2) OBJECTIVES:
3) TOOLS USED:
OOP Compiler
4) THEORY:
Composition and aggregation represent relationships where one class contains objects of
another class. Composition implies strong ownership, where the contained object’s lifecycle
depends on the container. Aggregation represents a weaker relationship where objects can exist
independently. These relationships help model real-world systems accurately.
54
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Tasks: LAB No.10
(Students are required to complete all the lab tasks, get it checked by the lab instructor and attach a
copy of it in their lab files)
Task 1:
Demonstrate constructor chaining where a derived class calls the base class constructor
automatically and explicitly.
Output:
55
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 2:
Show how a derived class explicitly calls a parameterized base class constructor.
Output:
56
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No.11
1) TITLE:
Implement polymorphism / dynamic binding: call subclass methods via base class
references
2) OBJECTIVES:
Understand method overriding in inheritance hierarchies
Use base class references to invoke derived class methods
Implement runtime polymorphism
Learn dynamic method dispatch in C++
4) THEORY:
3.1 Polymorphism
Polymorphism: "One interface, multiple implementations."
Types:
o Compile-time polymorphism (Static Binding): Method overloading
o Runtime polymorphism (Dynamic Binding): Method overriding
3.2 Dynamic Binding
Occurs when a base class reference or pointer refers to a derived class
object.
The method invoked is determined at runtime, based on the actual object
type.
Enables flexible and extensible designs.
4. Benefits of Polymorphism / Dynamic Binding
Flexibility: Same code works for multiple object types
57
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Reusability: Base class references handle multiple derived classes
Extensibility: Adding new derived classes does not require changing
existing code
Maintainability: Reduces code duplication
58
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 1:
Implement dynamic binding using a virtual function.
Call derived class methods using a base class pointe
Output:
59
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 2:
Use base class reference to call overridden methods.
Output:
60
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 3:
Store derived objects in a base class array and invokes overridden methods.
Output:
61
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No.12
Course Information
Course: Object-Oriented Programming
Semester: 2nd
Objective
- Apply fundamental OOP concepts: classes, objects, attributes, methods, encapsulation, and
constructors.
- Encourage creativity through open-ended problem solving.
Suggested Domains
- Library System
Requirements
- Create two or more classes with attributes, methods, constructors, and getters/setters.
- Write a main program that creates objects and shows interaction between classes.
Main program may create book objects, add them to the library, and display stored books.
62
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Deliverables
- Complete working program in C++ or Java.
Grading Criteria
- Correct use of OOP concepts (50%)
63
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Output:
64
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No.13
1) TITLE:
Implement file I/O / persistence: save objects to file and read them back
2) OBJECTIVES:
Understand file input/output operations
Save object data to a file
Read objects from a file to reconstruct program state
Demonstrate data persistence
Handle files in text and binary format
Tools
C++ Co
Concepts
Object-Oriented Programming (OOP)
File handling: ifstream, ofstream (C++), FileInputStream, FileOutputStream (Java)
Serialization (Java)
Exception handling for file operations
4) THEORY:
File Output: Writing data to a file using classes such as ofstream (C++) or
FileOutputStream / ObjectOutputStream (Java).
File Input: Reading data from a file using classes such as ifstream (C++) or
FileInputStream / ObjectInputStream (Java).
65
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Steps:
1. Serialize object data into a file
2. Store object in text or binary format
3. Read the object from the file to reconstruct its state
66
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Tasks: LAB No.13
(Students are required to complete all the lab tasks, get it checked by the lab instructor and
attach a copy of it in their lab files)
Task 1:
Write a program to store student object data into a file and read it back using file handling.
Output:
67
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 2:
Demonstrate object persistence by writing and reading an object using a binary file.
Output:
68
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
LAB No.14
1) TITLE:
Use generic / template classes (e.g. a generic Stack<T>) and Ecxception handling
2) OBJECTIVES:
Understand the concept of generic / template classes
Create type-safe reusable classes like Stack<T>
Handle runtime errors using try-catch (C++) or try-catch / throws (Java)
Demonstrate robust and reusable programming
Apply OOP concepts: encapsulation, inheritance (optional), polymorphism (optional)
Tools
C++ Compiler (Dev-C++, Code::Blocks, g++)
Java JDK / IDE (Eclipse, NetBeans, IntelliJ)
Text Editor
Concepts
Templates (C++) / Generics (Java)
Exception handling
Encapsulation
Reusable type-safe classes
OOP principles
4) THEORY:
69
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Advantages:
70
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Tasks: LAB No.14
(Students are required to complete all the lab tasks, get it checked by the lab instructor and
attach a copy of it in their lab files)
Task 1:
Implement a generic Stack<T> using templates and handle stack overflow and underflow using
exceptions.
Output:
71
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 2:
Use the same template class Stack<T> for string data type.
Output:
72
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Task 3:
Create a generic Pair<T1, T2> class.
Throw an exception if values are not valid.
Output:
73
Riphah International University, Faisalabad Campus