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

OOP Lab 7

The document discusses using structures in C++ to organize related data, such as storing information about multiple people by creating a "Person" structure to hold a name, age, and salary rather than separate variables; it provides an example of declaring and defining a Person structure variable before demonstrating how to access members using the dot operator and pass structures to functions.

Uploaded by

Taimur BAIG
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

OOP Lab 7

The document discusses using structures in C++ to organize related data, such as storing information about multiple people by creating a "Person" structure to hold a name, age, and salary rather than separate variables; it provides an example of declaring and defining a Person structure variable before demonstrating how to access members using the dot operator and pass structures to functions.

Uploaded by

Taimur BAIG
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 2

Structures

Objectives

The C++ programming skills that should be acquired in this lab:

 To understand the concept of Structures.


 Implementation in C++.
Structure is a collection of variables of different data types under a single name. It is
similar to a class in that, both holds a collection of data of different data types.

For example: You want to store some information about a person: his/her name,
citizenship number and salary. You can easily create different variables name, citNo,
salary to store this information separately.

However, in the future, you would want to store information about multiple persons.
Now, you'd need to create different variables for each information per person: name1,
citNo1, salary1, name2, citNo2, salary2

You can easily visualize how big and messy the code would look. Also, since no
relation between the variables (information) would exist, it's going to be a daunting
task.

A better approach will be to have a collection of all related information under a single
name Person, and use it for every person. Now, the code looks much cleaner,
readable and efficient as well.

This collection of all related information under a single name Person is a structure.

struct Person

char name[50];

int age;

float salary;

};
Once you declare a structure person as above. You can define a structure variable
as:

Person bill;

Here, a structure variable bill is defined which is of type structure Person.

When structure variable is defined, only then the required memory is allocated by the
compiler.

Considering you have either 32-bit or 64-bit system, the memory of float is 4 bytes,
memory of int is 4 bytes and memory of char is 1 byte.

Hence, 58 bytes of memory is allocated for structure variable bill.

The members of structure variable is accessed using a dot (.) operator.

Suppose, you want to access age of structure variable bill and assign it 50 to it. You
can perform this task by using following code below:

bill.age = 50;
Output

Enter Full name: Magdalena Dankova


Enter age: 27
Enter salary: 1024.4

Displaying Information.
Name: Magdalena Dankova
Age: 27
Salary: 1024.4

Passing structure to function in C++


Output

Enter Full name: Bill Jobs


Enter age: 55
Enter salary: 34233.4

Displaying Information.
Name: Bill Jobs
Age: 55
Salary: 34233.4

Lab Task

1. Implement the examples.

2. Write a C++ Program to Add Two Distances (in inch-feet) using Structures.

3. Write a C++ program to store student information (name, roll no, marks) and
store the 5 student data in structure array and then display the result.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy