Week2 (1)
Week2 (1)
Lahore Campus
Lab Manual
Lab Instructor: Hafiza Daheem
Department of Artificial Intelligence
Email: [email protected]
Lab: 02 Following are the topics that we will cover in this Lab.
What is OOP?
Lab Description:
This lab is designed to understand the Object- Oriented Concepts (Introduction to Classes and Objects),
Data Members and Member Functions, Constant Member Functions. Use of Access Specifiers (public,
private, Protected), (Scope of class members).
Lab Rubrics:
Identify the objects &
Individual/Team
Problem their relationships to Design/ Development
Areas work towards the
Understanding build object-oriented of Solutions
Assessed Assigned task
solution
(CLO 1) (CLO 2) (CLO 3) (CLO 4)
Not able to No able to identify Not able to Model a Wasn't able to
Poor understand objects and their solution for a given explain
Problem relationships problem experimental work
Modeled an average
Able to partially Partially able to identify Not completely able
solution for a given
Average understand the objects and their to explain
problem using object-
problem relationships experimental work
oriented principles
Able to
Thoroughly Identify the Modeled a proper explain/defend the
Fully Understand objects & their solution for a given assigned task and all
Very Good
the problem relationships to build problem using object- the questions have
object-oriented solution oriented principles been answered
correctly
Objective:
To cover
Scope:
The student should know the following at the end of this lab:
Problem understanding
Problem Solving
Theory: [CLO 1]
What is OOP?
Object-Oriented Programming (OOP) is a programming paradigm that organizes
software design around objects and data rather than actions and logic. It allows
developers to create reusable and modular code by defining classes, which are templates
for creating objects, and encapsulating data and behavior within these objects.
Example 1: [CLO 2]
class MyClass {
public:
// Data members
int myNumber; // Integer data member
string myString; // String data member
// Member functions
void myFunction() {
// Function body
}
};
int main()
{
MyClass obj1; // Creating an object of MyClass
MyClass obj2; // Another object of MyClass
}
Class Methods
Methods are functions that belongs to the class.
Member Function:
A member function of a class is a function that has its definition or its prototype within the class
definition like any other variable. It operates on any object of the class of which it is a member, and has
access to all the members of a class for that object.
“A method is a member function of a class”
int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
Parameters
#include <iostream>
using namespace std;
class Car {
public:
int speed(int maxSpeed){
return maxSpeed; }
};
int main() {
Car myObj;
cout << myObj.speed(200);
return 0;
}
Output: 200
TASKS:
TASK 1: [CLO 2] [CLO 3]
Write a program to input date (day, month, year) by a class method and print the date (day, month,
year) by writing another class method.
Write a program to Create a class BankAccount with attributes accountNumber, accountHolder, and
balance. Add methods for deposit and withdrawal. Simulate one user doing a few transactions.
Write a program to Create a class Student with attributes name, roll number, and marks in 3
subjects. Add a method to calculate total and average marks. Compare two students and show who scored
higher.
ulty.