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

OOP Lab 03 (C++ Structures)

This document provides an overview of structures in C++ as part of an Object-Oriented Programming lab. It explains the definition, declaration, and usage of structures, including how to access structure members and the difference between structures and arrays. Additionally, it covers passing structures to functions by value and reference, as well as returning structures from functions.

Uploaded by

Suhaib Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

OOP Lab 03 (C++ Structures)

This document provides an overview of structures in C++ as part of an Object-Oriented Programming lab. It explains the definition, declaration, and usage of structures, including how to access structure members and the difference between structures and arrays. Additionally, it covers passing structures to functions by value and reference, as well as returning structures from functions.

Uploaded by

Suhaib Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

OBJECT ORIENTED PROGRAMMING (LAB)

STRUCTURES IN C++
WEEK # 03

1
OBJECT ORIENTED PROGRAMMING LANGUAGE

Table of Contents
Structure.........................................................................................................................................1
Why we need Structures?.............................................................................................................. 1
Declaring a Structure...................................................................................................................... 1
Structure Variables......................................................................................................................... 3
How to access structure members in C++?.................................................................................... 4
Members of Structures...................................................................................................................5
Structs of Arrays (Array within Structure).......................................................................................6
Arrays of Structs............................................................................................................................. 6
What is an array of structures?............................................................................................... 6
What is a structure pointer?.........................................................................................................10
Structure and Function in C++...................................................................................................... 10
Passing Structure by Value........................................................................................................ 11
Example for passing structure object by value..........................................................................11
Passing Structure by Reference.................................................................................................12
Example for passing structure variable by reference................................................................ 12
Function Returning Structure....................................................................................................13
Example for Function Returning Structure................................................................................13

2
Structure
Structure is a collection of variables under a single name. Variables can be of any type: int, float,
char etc. The main difference between structure and array is that arrays are collections of the
same data type and structure is a collection of variables under a single name.

Why we need Structures?


As we noticed arrays are very powerful device that allow us to group large amounts of data
together under a single variable name.

How would you write a program if, instead of being asked for simple list of either integers or
characters, you were asked to combine integers, floating point numbers, and string with one
variable name? You could not do it by using array.

It is actually quit often we want to group logically connected data that are of different types
together. Just think about writing a program to store student record. You would need string for
your name, integers to store the ID number, and a floating-point number to store the grades.

Declaring a Structure
Example:

Three variables: custnum of type int, salary of type int, commission of type float are structure
members and the structure name is Customer. This structure is declared as follows:

In the above example, it is seen that variables of different types such as int and float are
grouped in a single structure name Customer.

Arrays behave in the same way, declaring structures does not mean that memory is allocated.
Structure declaration gives a skeleton or template for the structure.

1
struct st_name

type l;

type m;

type n;

};

Where

st_name​ Represents the structure name. It is also called Structure tag. It is used to
declare variables of structure type.

type​ Represent the type of the variable.

l,m,n​ Represent members of the structure. These may have different or same data
type. The members are enclosed in braces.

A semicolon after the closing bracket ( } ; ) indicates the end of the structure.

Each member of a structure must have unique name but different structures
may have same names.

A structure is first defined and then variables of structure type are declared. A variable of a
structure type is declared to access data in the members of the structure.

Example: Declare a structure with address as a tag and having two members name of character
type and age of integer type.

struct address

char name [15];

int age;

};

The structure member name is of character type with 15 characters length (including the null
character) and age is of integer type.

2
Structure Variables
After declaring the structure, the next step is to define a structure variable.

A structure is a collection of data items or elements. It is defined to declare its variables. These
are called structure variables. A variable of structure type represents the members of its
structure.

When a structure type variable is declared a space is reserved in the computer memory to hold
all members of the structure. The memory occupied by a structure variable is equal to the sum
of the memory occupied be each member of the structure.

A structure can be defined prior to or inside the main() function. If it is defined inside the main()
function then the structure variable can be declared only inside the main function. If it is
declared prior to the main() function, its variables can be defined anywhere in the program.

struct address

char city[15];

int pcode;

};

address taq , aye;

In the above example, city and pcode are members of the structure address. The variable taq
and aye are declared as structure variables.

The structure address has two members city and pcode. The variable city occupies 15 bytes and
pcode occupies 2 bytes. Thus, each variable of this structure will occupy 17 bytes space in
memory, i.e. 15 bytes for city and 2 bytes pcode.

3
How to access structure members in C++?
To access structure members, the operator used is the dot operator denoted by (.). The dot
operator for accessing structure members is used as:

For example:

A programmer wants to assign 2000 for the structure member salary in the above example of
structure Customer with structure variable cust1 this is written as:

4
Members of Structures
Structures in C++ can contain two types of members:

Data Member: These members are normal C++ variables. We can create a structure with
variables of different data types in C++.

Member Functions: These members are normal C++ functions. Along with variables, we can
also include functions inside a structure declaration.

Example:

5
In the above structure, the data members are three integer variables to store roll number,
age and marks of any student and the member function is printDetails() which is printing
all of the above details of any student.

Structs of Arrays (Array within Structure)

Arrays of Structs
What is an array of structures?
Like other primitive data types, we can create an array of structures.

Instead of declaring multiple variables we can also declare an array of structure in which each
element of the array will represent a structure variable.

6
7
8
Output:

Enter name:
fariba
Enter marks:
90
Enter gpa:
3.5

Enter
name:
Ali
Enter marks:
70
Enter gpa:
3.2

Enter
name:
Fahad
Enter marks:
100
Enter gpa:
3.88

name: fariba
gpa: 3.5
marks: 90

name: Ali
gpa: 3.2
marks: 70

name: Fahad

9
gpa: 3.88
marks: 100

What is a structure pointer?


Like primitive types, we can have pointer to a structure. If we have a pointer to structure,
members are accessed using arrow ( -> ) operator instead of the dot (.) operator.

Structure and Function in C++


Using function, we can pass structure as function argument and we can also return structure
from function.

10
Structure can be passed to function through its variable therefore passing structure to function
or passing structure object to function is same thing because structure object represents the
structure. Like normal variable, structure variable (structure object) can be passed by value or
by references / addresses

Passing Structure by Value

In this approach, the structure object is passed as function argument to the definition of
function, here object is representing the members of structure with their values.

Example for passing structure object by value

11
Passing Structure by Reference

In this approach, the reference/address structure object is passed as function argument to the
definition of function.

Example for passing structure variable by reference

12
Function Returning Structure

Structure is user-defined data type, like built-in data types structure can be returned from
function.

Example for Function Returning Structure

13
In the above example, statement 1 is declaring Input() with return type Employee. As we know
structure is user-defined data type and structure name acts as our new user-defined data type,
therefore we use structure name as function return type.

Input() have local variable E of Employee type. After getting values from user statement 2
returns E to the calling function and display the values.

14

You might also like