0% found this document useful (0 votes)
4 views5 pages

Week2 (1)

The lab manual outlines the topics covered in Lab 02 of the Artificial Intelligence department at the University of Management and Technology, focusing on Object-Oriented Programming (OOP) concepts such as classes, objects, data members, and member functions. It includes a description of lab objectives, rubrics for assessment, and various programming tasks for students to complete. The manual also provides theoretical explanations and sample code to illustrate OOP principles in C++.

Uploaded by

usmanqasim845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Week2 (1)

The lab manual outlines the topics covered in Lab 02 of the Artificial Intelligence department at the University of Management and Technology, focusing on Object-Oriented Programming (OOP) concepts such as classes, objects, data members, and member functions. It includes a description of lab objectives, rubrics for assessment, and various programming tasks for students to complete. The manual also provides theoretical explanations and sample code to illustrate OOP principles in C++.

Uploaded by

usmanqasim845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

University of Management and Technology,

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?

 What are Classes in OOP?

 What are Objects in OOP?

 What are the Data Members of Classes?

 What are the Member Functions of Classes?

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

 What are class methods


 Member Functions
 C++ Class Methods

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.

What are Classes in OOP?


Classes in OOP are blueprints or templates for creating objects. They define the properties (attributes or
data members) and behaviors (methods or member functions) that objects of that class will have.

What are Objects in OOP?


Objects in OOP are instances of classes. They represent concrete (real) instances of the data structure
and behavior defined by the class. Each object has its own set of data members and member functions,
which are defined by the class but can be manipulated independently for each object.

What are the Data Members of Classes?


Data members of classes are variables that hold data associated with objects of the class. These
variables represent the state of the object and define its characteristics. Data members can be of various
types, including integers, strings, arrays, or other objects.

What are the Member Functions of Classes?


Member functions of classes are methods or functions defined within the class that operate on the
class's data members. They define the behavior of objects of the class and can perform various tasks,
such as modifying data members, performing calculations, or interacting with other 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.

There are two ways to define functions that belongs to a class:

 Inside class definition


 Outside class definition

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”

Sample Code: [CLO 2]


#include <iostream>
using namespace std;

class MyClass { // The class


public: // Access specifier
void myMethod() { // Method/function
cout << "Hello World!";
}
};

int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}

Output: Hello World!

Parameters

You can also add parameters:

Sample Code: [CLO 2]

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

Task 2: [CLO 2] [CLO 3]


Write a program by using a class that takes name, age and city of a person as class attributes. An input Details
class method to input the data, getAge method to return age and Display method to display name, age and
city. Input the data for two persons and display the record of the person who is elder in age.

Input for person 1 Input for person 2 Output


Ali, 23, Lahore. Hassan, 20, Jhang Ali, 23, Lahore
Elder in age

Task 3: [CLO 2] [CLO 3]

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.

Task 4 : [CLO 2] [CLO 3]

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.

You might also like