0% found this document useful (0 votes)
14 views85 pages

Oop Labmanual

The document is a lab manual for the Object Oriented Programming (CS304) course at Riphah International University, Faisalabad Campus. It outlines the course learning outcomes, mapping of CLOs to PLOs, and a detailed weekly lab plan with specific experiments and tasks aimed at teaching fundamental and advanced OOP concepts. The manual emphasizes the importance of functions, classes, and constructors in programming, providing practical tasks for students to complete and understand these concepts.

Uploaded by

Ali Farhan
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)
14 views85 pages

Oop Labmanual

The document is a lab manual for the Object Oriented Programming (CS304) course at Riphah International University, Faisalabad Campus. It outlines the course learning outcomes, mapping of CLOs to PLOs, and a detailed weekly lab plan with specific experiments and tasks aimed at teaching fundamental and advanced OOP concepts. The manual emphasizes the importance of functions, classes, and constructors in programming, providing practical tasks for students to complete and understand these concepts.

Uploaded by

Ali Farhan
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

Riphah College of Computing Object Oriented Programming

RIPHAH INTERNATIONAL UNIVERSITY


FAISALABAD CAMPUS

RIPHAH COLLEGE OF COMPUTING

OBJECT ORIENTED PROGRAMMING


(CS304)

LAB MANUAL

Riphah International University, Faisalabad Campus


Riphah College of Computing Object Oriented Programming

CLOs / PLOs Mapping

Course Learning Outcomes (CLOs)

The students will be able to:

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)

Mapping of CLOs to PLOs

PLO PLO
CLOs/ PLOs PLO1 PLO2 PLO3 PLO4 PLO5 PLO6 PLO7 PLO8 9 10

CLO1 ✔

CLO2 ✔

CLO3 ✔

CLO4 ✔

Riphah International University, Faisalabad Campus


Riphah College of Computing Object Oriented Programming

List of Experiments

Weekly Lab Plan

Sr. No. Title of Experiment CLO

1 Introduction to the functions how to write programs with the help of 1


functions
Write a program that creates a function that receives three integers, compares
2 them, and displays the smallest one. . 1

Introduction to the Fundamentals and history of OOP Concepts


3 1
with functions and Write a program which demonstrates Classes
and objects.

Write a program which demonstrates Classes and constructors


4 3

Implement method Friend Function, Friend class with programs


5 3
Create an abstract Shape class (with area(), perimeter()), and subclasses
like Rectangle, Circle
Implement method overloading: same method name but different
6 3
parameters

Implement method overriding: same method name but different parameters


7 1
Design an inheritance hierarchy (e.g. Person → Employee → Manager)
8 2
with overridden methods
Design an inheritance hierarchy (e.g. Person → Employee →
9 2
Manager) with overridden methods
Show composition / aggregation: e.g. a Library has many Book objects
10 Demonstrate constructor chaining and use of super / base class constructors 4

11 Implement polymorphism / dynamic binding: call subclass methods via 4


base class references

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:

To understand the concept and importance of functions

To learn how functions support modular and structured programming

To understand how functions improve program efficiency

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.

 IDE (Integrated Development Environment)

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

Tasks: LAB No.1


(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 2:

Using function call to print different symbols

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:

Write a function which input 5 integers and calculate average.

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:

Write a C++ program by using an example of call by value.

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

Task 8: Write a C++ program by using an example of adding two values.

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:

 To understand the limitations of procedural programming


 To study the evolution and need for Object-Oriented Programming
 To learn the basic principles of OOP

3) TOOLS USED:

An Object-Oriented Programming Language Environment is suitable for developing


large and complex programs where code reusability, security, and easy maintenance are
required. It is ideal for real-world problem solving because it represents data and behavior
using objects and classes. This environment is also well suited for applications that need
frequent updates, teamwork, and long-term development.

4) THEORY:

Object-Oriented Programming (OOP) was developed to overcome the drawbacks of procedural


programming, such as lack of data security, poor reusability, and difficulty in managing large
software systems. OOP focuses on modeling real-world entities using objects and classes. Instead
of emphasizing functions, OOP emphasizes data and its associated behavior. This approach
improves software maintainability, scalability, and reliability.

9
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming

Tasks: LAB No.2


(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 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:

Introduction to the Fundamentals and history of OOP Concepts with


functions and Write a program which demonstrates Classes and objects.

2) OBJECTIVES:

 To understand the structure and purpose of classes


 To learn how objects are created from classes

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

Tasks: LAB No.3


(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 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:

Write a class named Book with three data members:

BookID (integer)

Pages (integer)

Price (float)

The class should contain the following member functions:

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:

 To understand how objects are initialized


 To learn the role of constructors in class design

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:

A constructor is a special member function of a class that is automatically invoked when an


object is created. Its main purpose is to initialize the data members of the class. Constructors
ensure that objects start in a valid and consistent state. They play a crucial role in resource
allocation and setup during object creation.

17
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming

Tasks: LAB No.4


(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 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.

Examples: GCC, Turbo C++, Clang

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

Tasks: LAB No.5


(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 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:

Add Two Numbers Using Friend Function

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:

 To understand abstraction as an OOP principle


 To learn how abstract classes define interfaces

3) TOOLS USED:
 Object-Oriented Programming Language

An Object-Oriented Programming (OOP) Language is a programming language that allows


programs to be written using objects and classes. It supports features like encapsulation,
inheritance, polymorphism, and abstraction, which help in creating modular, reusable, and
easy-to-maintain programs.

Examples: C++, Java, Python, C#

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

Tasks: LAB No.6

(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:

Overloading Unary Minus (-) Operator


Write a program to overload the unary minus (-) operator to change the sign of member data.

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:

 To understand compile-time polymorphism


 To improve code flexibility and readability

3) TOOLS USED:
 OOP Compiler

An OOP Compiler is a software tool that translates object-oriented programs (written in


languages like C++ or Java) into machine code so the computer can execute them. It also checks
the program for syntax errors before running.

Examples: GCC, Turbo C++, Visual C++

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.

3) Tools and Concepts Used


 Object-Oriented Programming (OOP)
 Inheritance
 Method Overriding
 Polymorphism
 Virtual Functions
 Constructor Initialization Lists
 Encapsulation

4) THEORY:
 The objectives of this task are:

 To understand and implement inheritance in C++.

 To demonstrate method overriding in derived classes.

 To show how base class references can call overridden methods.

 To design a real-world class hierarchy using OOP principles.

41
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming

Tasks: LAB No.8

(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.

Create a constructor to initialize data.

Make a member function to input data and another to display data.


Create another class Student that inherits Person and has extra attributes roll no, marks.

Student also has functions to input and display its data.

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

Write a class Parent with a protected integer n.

Provide a default constructor and a parameterized constructor to set n.


Create a class Child derived from Parent with a private character ch.

In Child constructors, call Parent constructors using initializer list.

Display values of n and ch.

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

Write a class Move having protected member position.

Constructor sets position = 0

forward() increases position by 1

show() displays position


Create class Move2 derived from Move.

backward() decreases position by 1


In main, demonstrate forward and backward movement.

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:

Study of Multilevel Inheritance Using Person–Employee–Manager Hierarchy

2) OBJECTIVES:

 To understand inheritance and code reuse


 To study hierarchical relationships between classes

3) TOOLS USED:
 OOP Compiler

An OOP Compiler is a software tool that translates object-oriented programs (written in


languages like C++ or Java) into machine code so the computer can execute them. It also
checks the program for syntax errors before running.

Examples: GCC, Turbo C++, Visual C++

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

Tasks: LAB No.9

(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:

 To understand has-a relationships


 To differentiate between composition and aggregation

3) TOOLS USED:
 OOP Compiler

An OOP Compiler is a software tool that translates object-oriented programs (written in


languages like C++ or Java) into machine code so the computer can execute them. It also
checks the program for syntax errors before running.

Examples: GCC, Turbo C++, Visual C++

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++

3) Tools and Concepts Used:


 Concepts:
o Inheritance
o Virtual functions / Method overriding
o Runtime polymorphism
 Tools:
o C++ compiler (e.g., g++, Dev-C++, Code::Blocks)
o Java IDE (optional for Java implementation)

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

Tasks: LAB No.11


(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 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

Open-Ended Lab Activity – Object-Oriented Programming (OOP)

Course Information
Course: Object-Oriented Programming

Semester: 2nd

Lab Title: Designing a Class-Based Mini System

Objective
- Apply fundamental OOP concepts: classes, objects, attributes, methods, encapsulation, and
constructors.
- Encourage creativity through open-ended problem solving.

Lab Task (Open-Ended)


Design a mini real-world system of your choice using at least two interacting classes.

Suggested Domains
- Library System

- Student Course Registration

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.

- Use meaningful names for classes, methods, and variables.

- Add comments to explain your code.

Example (For Guidance Only)


Classes:

- Book (title, author, price)

- Library (list of books, addBook(), displayBooks())

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.

- Soft copy submission via LMS/email.

- Short explanation in lab notebook.

Grading Criteria
- Correct use of OOP concepts (50%)

- Code quality and structure (20%)

- Creativity and uniqueness (20%)

- Output demonstration (10%)

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

3) Tools and Concepts Used:

Tools
 C++ Co

Concepts
 Object-Oriented Programming (OOP)
 File handling: ifstream, ofstream (C++), FileInputStream, FileOutputStream (Java)
 Serialization (Java)
 Exception handling for file operations

 mpiler (Dev-C++, Code::Blocks, g++)


 Java JDK / IDE (Eclipse, NetBeans, IntelliJ)
 Text Editor

4) THEORY:

3.1 File I/O

 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).

3.2 Object Persistence


 Object persistence allows objects to exist beyond the execution of the program.

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

3.3 File Formats


 Text files: Human-readable, store data in plain text.
 Binary files: Machine-readable, store data in byte format; preserves object structure and allows
direct writing/reading of objects.

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)

3) Tools and Concepts Used:

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:

3.1 Generic / Template Classes

 C++ Template syntax: template<class T>


 Java Generics syntax: class Stack<T>
 Templates / generics allow the same class to work with different data types (int, float,
string, etc.)
 Example: Stack<int> or Stack<string>

69
Riphah International University, Faisalabad Campus
Riphah College of Computing Object Oriented Programming
Advantages:

 Reusability: One class works for multiple types


 Type safety: Prevents errors due to wrong types
 Maintainability: Less code duplication

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

You might also like