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

Main PDF Lesson 4

The document discusses structures in C++. It defines a structure as a group of elements of different types identified by member identifiers. The document explains how to declare and define structures, access structure members, pass structures as function arguments by value and reference, and create arrays of structures. Examples are provided to demonstrate these concepts.

Uploaded by

onecarloscruz111
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)
10 views

Main PDF Lesson 4

The document discusses structures in C++. It defines a structure as a group of elements of different types identified by member identifiers. The document explains how to declare and define structures, access structure members, pass structures as function arguments by value and reference, and create arrays of structures. Examples are provided to demonstrate these concepts.

Uploaded by

onecarloscruz111
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/ 26

MODULE 4

STRUCTURES
LESSON #1
Structures
Upon completion of this subtopic, you will be able to:
• Understand what is a structure
• Learn how to declare and use structures in a program
• A structure is a group of elements which are of
different type. Each of the elements is identified by its
own identifier and they are called member.

• A structure can be thought of as an object without any


member functions. The important property of structures
is that the data in a structure can be a collection of
data items of diverse types.
• A structure variable can hold values just like any other
variable can.

• A structure value is a collection of smaller values called


member values.
The struct keyword defines a structure type followed by an
identifier (name of the structure).

Then inside the curly braces, you can declare one or more
members (declare variables inside curly braces) of that
structure
For example:
struct Person
{ char name[50];
int age;
float salary; };

Here a structure person is defined which has three


members: name, age and 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.
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;
#include <iostream> int main()
using namespace std; {
struct Rectangle struct Rectangle r;
{ r.width=8;
int width, height; r.height=5;
cout<<"Area is: "<<(r.width * r.height)<<endl;
}; return 0;
}
#include <iostream> int main() {
using namespace std; Person p1;
struct Person cout << "Enter Full name: ";
{ cin.get(p1.name, 50);
char name[50]; cout << "Enter age: "; cin >> p1.age;
int age; cout << "Enter salary: "; cin >> p1.salary;
float salary; }; cout << "\nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;
return 0; }
If access specifier is not declared explicitly, then by default
access specifier will be public.

The instance of the structure is known as Structure variable.


#include <iostream>
using namespace std;
struct Point {
Like other primitive data
int x, y; }; types, we can create an
array of structures.
int main()
{ struct Point arr[10];
arr[0].x = 10;
arr[0].y = 20;
cout << arr[0].x << " " << arr[0].y;
return 0;
}
https://www.cprogramming.com/tutorial/lesson7.html
https://www.programiz.com/cpp-programming/structure
https://www.tutorialspoint.com/cplusplus/cpp_data_structures.htm
https://beginnersbook.com/2017/09/cpp-structures/
http://www.cplusplus.com/doc/tutorial/structures/
https://www.javatpoint.com/cpp-structs
LESSON #2
Structures as an
Argument
Upon completion of this subtopic, you will be able to:
• Learn how to set structures as a function argument
• Understand how arrays and functions are applied to a
structure.
• Create programs based from structure, member and
built in functions.
A structure variable can be passed to a function in similar
way as normal argument.

Passing of structure to the function can be done in two ways:


•By passing all the elements to the function individually.
•By passing the entire structure to the function.
#include <iostream>
using namespace std;
struct Person Structure
{
char name[50];
int age; float salary; Function Prototype
};
void displayData(Person);
int main()
{
Person p;
cout << "Enter Full name: ";
cin.get(p.name, 50);
cout << "Enter age: ";
cin >> p.age;
cout << "Enter salary: ";
cin >> p.salary;
displayData(p);
return 0; }
void displayData(Person p)
{
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p.name << endl;
cout <<"Age: " << p.age << endl;
cout << "Salary: " << p.salary;
}
Call By Value Call By Reference
original value is not modified original value is modified.
a copy of the variable is passed a variable itself is passed.
actual and formal arguments will be actual and formal arguments will be
created in different memory locations created in the same memory location
#include <iostream>
using namespace std;
struct Distance {
int kilometer;
int meters;
};

void TotalDistance(Distance d1, Distance d2)


{ Distance d;
d.kilometer = (d1.kilometer + d2.kilometer);
d.meters = (d1.meters + d2.meters) ;
cout << "Total distance is " << d.kilometer << " Kilometers "<< endl;
cout << "Total distance is " << d.meters << " meters "<< endl;
}
void initializeFunction()
{ Distance Distance1, Distance2;
Distance1.kilometer = 10;
Distance2.kilometer = 9;
Distance1.meters = Distance1.kilometer * 1000;
Distance2.meters = Distance2.kilometer * 1000;
TotalDistance(Distance1, Distance2); }

int main()
{
initializeFunction();
return 0;
}
#include <iostream>
using namespace std;

struct number {
int n;
};

void increment(number& n2)


{
n2.n++;
}
void initializeFunction()
{
number n1;
n1.n = 10;
cout << "Before calling increment function: " << n1.n << endl;
increment(n1);
cout << "After calling increment function: " << n1.n << endl;
}

int main()
{
initializeFunction();
return 0;
}
https://www.programiz.com/cpp-programming/structure-function
https://codescracker.com/cpp/cpp-pasing-structures-to-functions.htm
https://fresh2refresh.com/c-programming/c-passing-struct-to-function/
https://beginnersbook.com/2017/09/cpp-structure-and-function/

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