0% found this document useful (0 votes)
118 views

OOP Lab Manual - W01

This document provides instructions and objectives for a lab on pointers and structures in C++. The lab covers: 1. Using pointers to store and dereference memory addresses. Pointers contain the address of a variable rather than its value. 2. Defining structures to group related data of different types, such as components of a student record. Structures allow passing grouped data to functions with a single variable. Code examples demonstrate defining, initializing, and accessing members of structures. 3. Pointers can also be used to access members of structures. Nested structures are also discussed briefly. The document provides detailed explanations of pointers and structures to help students complete the lab assignments.

Uploaded by

Radish Kumar
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)
118 views

OOP Lab Manual - W01

This document provides instructions and objectives for a lab on pointers and structures in C++. The lab covers: 1. Using pointers to store and dereference memory addresses. Pointers contain the address of a variable rather than its value. 2. Defining structures to group related data of different types, such as components of a student record. Structures allow passing grouped data to functions with a single variable. Code examples demonstrate defining, initializing, and accessing members of structures. 3. Pointers can also be used to access members of structures. Nested structures are also discussed briefly. The document provides detailed explanations of pointers and structures to help students complete the lab assignments.

Uploaded by

Radish Kumar
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/ 17

Lab Manual

CS102L – Object Oriented Programming Lab


Lab No: 01
Topic: Pointers and Structure

Class: BSAI - BSSE


Semester: II A
Session: Spring, 2022
Instructor:
Lab Instructor: Amir Ali

Lab Date: 9 Feb 2022, Wednesday


Lab Time: 8:40 am

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

Instructions
Submission: Use proper naming convention for your submission file. Name the submission file
as Lab_NO_DEGREE_ROLLNUM (e.g. Lab_01_BSAI_00000). Submit the file on Google
Classroom within the deadline. Failure to submit according to the above format would result in
deduction of 10% marks. Submissions on the email will not be accepted.

Plagiarism: Plagiarism cases will be dealt with strictly. If found plagiarized, both the involved
parties will be awarded zero marks in the assignment, all of the remaining assignments, or even an
F grade in the course. Copying from the internet is the easiest way to get caught!

Deadline: The deadlines to submit the assignment are hard. Late submission with marks
deduction will be accepted according to the course policy shared by the instructor. Correct and
timely submission of the assignment is the responsibility of every student; hence no relaxation will
be given to anyone.

Comments: Comment your code properly. Bonus marks (maximum 10%) will be awarded to
well comment code. Write your name and roll number (as a block comment) at the beginning of
the solution to each problem. Objectives
InTip
this: lab,
For you willcompletion
timely learn: of the assignment, start as early as possible. Furthermore, work
smartly
o About- as some of theADT.
Structure problems can be solved using smarter logic.
o1. HowNote:toFollow
define athe given instructions
Structure, to the
initialize and letter,
refer failing to members
to individual do so willofresult in a zero.
a Structure.

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

Objectives
In this lab, you will learn:
• What is Pointers
• How to use pointers.
• To use Structures.
• Structure with Arrays
• Structure with Functions
• Nested Structure

Concepts
1. Pointers:
You have learned that C++’s data types are classified into three categories: simple, pointers, and
structured. Until now, you have studied only the first two data types. This chapter discusses the
third data type called the pointer data type. You will first learn how to declare pointer variables
(or pointers, for short) and manipulate the data to which they point.
o Pointers are a special type of variables in which a memory address is stored. They contain
a memory address, not the value of the variable.

1.1 Example to elaborate Pointers:


To understand pointers we have to understand how variables are stored. Variables are
stored in memory cells inside the computer's memory. The computer's memory is made up
of consecutive memory cells, a byte long, each with a unique address.

But we are not going to think in those terms. We are going to believe that computer memory
is made up of a bunch of houses on one very long street. Thus, each house is a memory
cell. Now, there must be a way for us to find this house. Well, in each house someone lives
there. This person of course has a name and this will be our variable identifier. For example:

int paul;

This will put paul into a vacant house, of size int, somewhere along the street. We do not
decide where paul will live. This is done by the operating system and the compiler at
runtime. In a later section, we will discuss how to get paul to tell us where his house is.

Currently, paul does not have anything stored in his house. But, we all know that wouldn't be
any fun to not store anything. So, each house can of course store a value. Continuing from
above:
paul = 25; This will store the value
25 into Paul’s house.

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

One last thing before we move into other topics. Let us remember that paul's house is a
unique number in memory. In addition, if paul's house was numbered 1234 we know that
his house is between houses 1233 and 1235. This is a very important concept for later sections.

1.2 Where do you live? (&)


When we declare a variable a person is put into the house so we can access what is stored
there.

But, what if we wanted to know where this person lives. This is done by preceding the
variable indentifier with an ampersand sign (&), which means "address of" operator. For
example:

This line would store the melissa = &paul;


address of paul into
melissa's house.

Now, let's suppose paul's address is 1500.


paul = 21;
tom = paul;
melissa = &paul;

So, for this example, the first line will store 21 into paul's house. The second line will store
the value 21 inside tom's house. So far no different then what we have usually done. The third
line, though, will store the address of paul, 1500, into melissa's house.

o The following diagram might help to understand this example:

Figure 1: Shows the “address of” operator workflow

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

1.3 Code Example: “address-of” operator

Figure 2: Shows the concept of “address of”


Output:

1.4 Code Example: “Reference” operator:

Figure 3: Shows the concept of “address of” and “reference”

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

Output:

1.5 Code Example: Pointer

Figure 4: Shows the concept pointer variable and its workflow


Output:

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

Figure 5: Shows the concept of pointer


Output:

2. Structures
“A structure is a collection of variables under a single name. These variables can be of different
types, and each has a name that is used to select it from the structure”.

2.1 Example:
There is always a requirement in most of our data processing applications that the
relevant data should be grouped and handled as a group. This is what the concept of
structure is.

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

Suppose that you want to write a program to process student data. A student record consists
of, among other things, the student’s name, student ID, GPA, courses taken, and course grades.
Thus, various components are associated with a student. However, these components are all of
different types. For example, the student’s name is a string, and the GPA is a floating-point
number.
Because these components are of different types, you cannot use an array to group all of the
items associated with a student. C++ provides a structured data type called struct to group items
of different types. Grouping components that are related but of different types offers several
advantages. For example, a single variable can pass all the components as parameters to a
function.
2.2 General Syntax of Structures in C++:
The components of a struct are called the members of the struct. The general syntax of a
struct in C++ is:

Figure 6: Shows the syntax of structure C++ program

2.3 Definition of Structures:


In C++, struct is a reserved word. The members of a struct, even though they are enclosed in
braces (that is, they form a block), are not considered to form a compound statement. Thus, a
semicolon (after the right brace) is essential to end the struct statement. A semicolon at the
end of the struct definition is, therefore, a part of the syntax.

Figure 7: Structure Definition of Student

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

2.4 Declaration of Structures:


Once a data type is defined, you can declare variables of that type. As we defined a struct
type, studentType in Section 1.5, and then declare variables of that type.

Figure 8: Structure Declaration of Student

These statements declare two struct variables, newStudent and student, of type studentType.
The memory allocated is large enough to store firstName, lastName, courseGrade, testScore,
programmingScore, and GPA. The following Figures visualize the declared variables:

Figure 9: Visualization of members of Structure

2.5 Another way to define and declare the Structures in C++:


You can also declare struct variables when you define the struct. For example, consider the
following statements:

Figure 10: Define and Declare Structure of Student together


Air University Islamabad
FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

These statements define the struct studentType and also declare tempStudent to be a variable
of type studentType.

Typically, in a program, a struct is defined before the definitions of all of the functions in the
program so that the struct can be used throughout the program. Therefore, if you define a
struct and also simultaneously declare a struct variable (as in the preceding statements),
then that struct variable becomes a global variable and thus can be accessed anywhere
in the program.

2.6 Code Example: Initializing and Accessing Structure Members

Output:

2.7 Code Example: Array as a member of Structure


A structure may consist of different types of data. The members can be of simple type
such as int, float, etc. But it can also be complex as arrays. The following example shows:

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

Output:

2.8 Structure with array


An array is a collection of data items of the same type. Each element of the array can be
int, char, float, double, or even a structure. We have seen that a structure allows elements
of different data types to be grouped together under a single name. This structure can then
be thought of as a new data type in itself. So, an array can comprise elements of this new
data type. An array of structures finds its applications in grouping the records together and
provides for fast accessing.

Figure 11
Air University Islamabad
FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

Output:

As you can notice, for each Student you have to create new object and feed their respective
values. It is time consuming. Therefore, we can create array of Student object as we create
array of some built-in data types. The following example shows:

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

Output:

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

2.9 Structure with function


Structure variables can be passed to a function and returned in a similar way as normal
arguments.

Output:

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

From the example above, you can notice the type of function is void. You can also return
structure from a function.

Output:

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

2.10 Nested Structure


When a structure contains another structure, it is called nested structure. For example, we
have two structures named Address and Employee. To make Address nested to Employee, we
have to define Address structure before and outside Employee structure and create an object
of Address structure inside Employee structure. The following the syntax:

Output:

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies
CS112L – Object Oriented Programming Lab Manual

Lab Tasks
1. In each semester there are number of subjects. Each subject has name, credit_hours,
teacher_name, and lab_or_not. Declare, initialize and access(print) the structure of
SUBJECT.
2. Introduce the time of subject class (in previous question). Time can be defined in hour,
minutes and am/pm.
3. Add function to upgrade the values of teacher_name and credit_hours.
4. Create a structure for FAMILY_MEMBER. Each member has name, gender, age,
relation/role, education. Declare, initialize and access(print) the structure of
FAMILY_MEMBER for any 3 members.
5. Add Spouse (of type FAMILY_MEMBER) variable in FAMILY_MEMBER structure. If
family member is married initialize it with value of FAMILY_MEMBER. Also use
function to add spouse if a member is married after initialization.
6. Create array for all the members of a family. Initiate via loop and print via loop.

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Creative Technologies

You might also like