OOP Lab Manual - W01
OOP Lab Manual - W01
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.
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.
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.
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.
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:
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.
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.
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:
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:
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.
Output:
Output:
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:
Output:
Output:
From the example above, you can notice the type of function is void. You can also return
structure from a function.
Output:
Output:
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.