0% found this document useful (0 votes)
29 views11 pages

Lab 01

Uploaded by

JAWERIA ASLAM
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)
29 views11 pages

Lab 01

Uploaded by

JAWERIA ASLAM
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
You are on page 1/ 11

Object Oriented Programming Lab (CS1022L)

Lab Manual (Lab # 01)

Session: Spring ’24


Instructor: Jaweria Aslam

Department of Computer Science


School of Systems and Technology UMT Lahore Pakistan
Objective: (Mapping CLO 01)
• Revision pointer, functions
• Recursion
• Introduction to Structure

Introduction to pointer:
The pointer in C++ language is a variable, it is also known as locator or indicator
that points to an address of a value. The symbol of an address is represented by a
pointer. In addition to creating and modifying dynamic data structures, they allow
programs to emulate call-by-reference. One of the principal applications of pointers
is iterating through the components of arrays or other data structures. The pointer
variable that refers to the same data type as the variable you're dealing with has the
address of that variable set to it (such as an int or string).

How to use a pointer?


Establish a pointer variable. employing the unary operator (&), which yields the
address of the variable, to assign a pointer to a variable's address. Using the unary
operator (*), which gives the variable's value at the address provided by its argument,
one can access the value stored in an address. Since the data type knows how many
bytes the information is held in, we associate it with a reference. The size of the data
type to which a pointer points is added when we increment a pointer.

Advantage of pointer Advantage of pointer:


1) Pointer reduces the code and improves the performance, it is used to retrieving
strings, trees etc. and used with arrays, structures and functions.
2) We can return multiple values from function using pointer.
3) It makes you able to access any memory location in the computer's memor
Function
Recursion
A function that calls itself is known as a recursive function. And, this technique is known as
recursion. The figure below shows how recursion works by calling itself over and over again.

Example:
Output:

Working:
Structure
As structure is a group of data elements grouped together under one name. These data elements, known as
members, can have different types and different lengths. Data structures are declared in C++ using the
following syntax:
struct structure_name
{ member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3; . .
} object_names;
Example:
struct product
{
int weight;
float price;
};
product apple;
Access Structure Members:
To access members of a structure, use the dot syntax (.):
struct product
{
int weight;
float price;
};
product apple;
Cin>>apple.weight;
Cin>>apple.price;

Structures as Function Arguments:


Structures can be passed as function arguments like all other data types. We can pass individual
members of a structure, an entire structure, or a pointer to a structure to a function. Like all
other data types, a structure or a structure member or a pointer to a structure can be returned
by a function.
void printBook(struct Books book);
{
Cout<<book.id;
Cout<<book.name;
}
Int main()
{
Book b1;
b1.id=124;
b1.name=“JAVA”;
printBook(b1);
}
Pointers to Structure
A pointer variable can be created not only for native types like (int, float, double etc.) but they
can also be created for user defined types like structure. If you do not know what pointers are,
visit C++ pointers.
struct employee
{
char name[30];
int age;
float salary;
};
struct employee *sptr1, emp1;
sptr1 = &emp1;
Cout<<sprt1->name<<sprt->age;
Here is how you can create pointer for structures:
LAB TASKS
TASK 1
UMT needs a student management system. A student has a name and registration number.
Design a C++ program to store a list of students and then display student details on the console.
Your program must:
Use a structure for storing student details.
Have a function to take the students’ data from the user
Have a function to display the student’s data on the console screen (4 Marks)
TASK 2
A company needs an employee management system. An employee has an ID, name, designation,
and salary. Design a C++ program that can store a list of employees and then display employee
details on console. (3 Marks).
Your program must:
• Use a struct for storing employee details.
• Have a display function that takes employee struct as input and displays its details
• With an additional constraint user-defined functions must accept pointer parameters.

TASK 3
Write a recursive program that takes an integer as an argument for example n. It should calculate
the sum of all natural numbers up to n. (3 Marks)
For example:
n=7 then it should calculate 1+2+3+4+5+6+7

You might also like