[go: up one dir, main page]

0% found this document useful (0 votes)
15 views24 pages

Structure

Uploaded by

depanimokshith
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)
15 views24 pages

Structure

Uploaded by

depanimokshith
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/ 24

User defined data types - struct

Dr. Manjubala Bisi


Assistant Professor,
Department of CSE,
NIT Warangal.

1
structures
• Student is an object which contain attributes like rollno, name,
department, course, etc.
– All these attributes of different types.
• How to store such an information under same name ?
– structures
• A Structure is a collection of related data items, possibly of
different types.
• A structure type in C++ is called struct.
• A struct is heterogeneous in that it can be composed of data of
different types.
• In contrast, array is homogeneous since it can contain only
data of the same type.
2
structures
• Examples:
– Student record: student id, name, major, gender,
start year, …
– Bank account: account number, name, currency,
balance, …
– Address book: name, address, telephone number,

• In database applications, structures are called records.

3
structures
• Individual components of a struct type are called members (or
fields).
• Members can be of different types (simple, array or struct).
struct student
{ int rollno;
char name[20]; Members of the
char department[30]; struct
char course[20];
}s1, s2, s3;

where student is a structure name // like int (int is a primitive data type)

s1, s2 and s3 are variables of structure student.

4
Declaring struct variables

struct student p, q, r;
• Declares and sets aside storage for three variables – p, q,
and r – each of type struct student.

struct student M[25];


• Declares a 25-element array of struct student;
allocates 25 units of storage, each one big enough to hold
the data of one student

struct motor *m;


• Declares a pointer to an object of type struct student
5
Accessing Members of a struct
struct student p;
struct student q[10];
struct motor *r;
• Then
p.rollno — is the roll no
p.name — is the name
p.Department — is the department name
p.course — is the course name

q[i]. rollno — is the of the rollno of ith student


q[i]. name — is the name of the ith student

r -> rollno — is the rollno of the student pointed to by r.


p -> name — is the name of the student pointed by r.
6
Operations on struct
• Copy/assign
struct student p, q;
p = q;
• Get address
struct student p;
struct student *s;
s = &p;
• Access members
p.rollno;
s -> rollno;

7
Example
• Consider the problem discussed in the previous class and do the
solution using structures.
#include<iostream>
using namespace std;
struct student
{ int rollno;
int marks;
};
student s1[60], s2[60], s3[60];
struct total
{ int rollno;
int m1,m2, m3,sum;
char grade; }t[60];
8
Example
int main()
{ int n;
cout << "Enter number of students:"; cin >> n;
// read the data into s1, s2, s3
int i=0;
// get the data into student structure
while(i< n)
{ cout << "\n Enter the " << i+1 << "student details";
cin >> s1[i].rollno >> s1[i].marks >> s2[i].marks >> s3[i].marks;
s2[i].rollno = s3[i].rollno = s1[i].rollno;
i++;
}

9
Example
// read the data from the student and write it into total
i=0;
while(i<=n) // Include the marks validation
{ t[i].rollno = s1[i].rollno;
t[i].m1 = s1[i].marks;
t[i].m2 = s2[i].marks;
t[i].m3 = s3[i].marks;
t[i].sum = t[i].m1 + t[i].m2 + t[i].m3;
if (t[i].m1 >= 40 && t[i].m2 >= 40 && t[i].m3 >= 40)
t[i].grade = 'P';
else t[i].grade = 'F';
i++;
}
10
Example
// display the data
i=0;
while(i<n)
{
cout << endl << t[i].rollno << " " << t[i].m1 << " " << t[i].m2 ;
cout << " " << t[i].m3 << " " ;t[i].sum << t[i].grade;
i++;
}
return 0;
}

11
How to pass or return a structure
to/from a Function
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.

12
#include <iostream>
using namespace std; Using Call By Value
struct Distance { Method
int kilometer;
int meter;
};

// accepts distance as its parameters


void TotalDistance(Distance d1, Distance d2)
{
// creating a new instance of the structure
Distance d;

// assigning value to new instance of structure


d.kilometer = (d1.kilometer + d2.kilometer) / 1000;

d.meter = (d1.meter + d2.meter) / 1000;

cout << "Total distance:";


cout << "kilometer: "
<< d.kilometer << endl;

cout << "meter: " << d.meter


<< endl; 13
}
// Function that initialises the value
// and calls TotalDistance function
void initializeFunction()
{
// creating two instances of Distance
Distance Distance1, Distance2;

// assigning values to structure elements


Distance1.kilometer = 10;
Distance1.meter = 455;

Distance2.kilometer = 9;
Distance2.meter = 745;

// calling function with (structure)


// distance as parameters
TotalDistance(Distance1, Distance2);
}

14
// Driver code0
int main()
{

// Calling function to do required task


initializeFunction();

return 0;
}

15
Using Call By reference
Method
/ C++ program to pass structure as an argument
// to the functions using Call By Reference Method

#include <bits/stdc++.h>
using namespace std;

struct number {
int n;
};

// Accepts structure as an argument


// using call by reference method
void increment(number& n2)
{
n2.n++;
}

16
void initializeFunction()
{
number n1;

// assigning value to n
n1.n = 10;

cout << " number before calling "


<< "increment function:"
<< n1.n << endl;

// calling increment function


increment(n1);

cout << "number after calling"


<< " increment function:" << n1.n;
}
17
// Driver code
int main()
{
// Calling function to do required task
initializeFunction();

return 0;
}

18
How to return a structure from the
functions?
// C++ program to return a structure from
// a function using Call By Value Method

#include <iostream>
#include <stdlib.h>

using namespace std;

// required structure
struct Employee {
int Id;
string Name;
};

19
/ return type of the function is structure
Employee data(Employee E)
{

// Assigning the values to elements


E.Id = 45;
E.Name = "aman";

// returning structure
return (E);
}

20
// Driver code
int main()
{

// creating object of Employee


Employee Emp;

// calling function data to assign value


Emp = data(Emp);

// display the output


cout << "Employee Id: " << Emp.Id;
cout << "\nEmployee Name: " << Emp.Name;

return 0;
}

21
C++ – Pointer to Structure
• Pointer to structure in C++ can also be
referred to as Structure Pointer. A structure
Pointer in C++ is defined as the pointer which
points to the address of the memory block
that stores a structure.
• Syntax:

struct name_of_structure *ptr;

ptr = &structure_variable;

22
#include <iostream>

using namespace std;

// Structure declaration for


// vertices
struct GFG {
int x;
int y;
};
// Structure declaration for
// Square
struct square {

// An object left is declared


// with 'GFG'
struct GFG left;

// An object right is declared


// with 'GFG'
struct GFG right;
};

23
// Function to calculate area of
// the given Square
void area_Square(struct square s)
{
// Find the area of the Square
// using variables of point
// structure where variables of
// point structure is accessed
// by left and right objects
int area = (s.right.x) * (s.left.x);

// Print the area


cout << area << endl;
}

// Driver Code
int main()
{
// Initialize variable 's'
// with vertices of Square
struct square s = { { 4, 4 }, { 4, 4 } };

// Function Call
area_Square(s);

return 0;
} 24

You might also like