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

Structures: Fahad Majeed UET Taxila

Structures allow grouping of related data types together. A structure defines members of possibly different data types. Examples of structures include student records with name, id, major etc. and bank accounts with account number, name, balance etc. Structures can contain simple, array and nested structure members. Variables of structure types can be declared and members accessed using dot operator. Arrays of structures allow storing multiple records. Nested structures allow structures within other structures.

Uploaded by

Umer Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Structures: Fahad Majeed UET Taxila

Structures allow grouping of related data types together. A structure defines members of possibly different data types. Examples of structures include student records with name, id, major etc. and bank accounts with account number, name, balance etc. Structures can contain simple, array and nested structure members. Variables of structure types can be declared and members accessed using dot operator. Arrays of structures allow storing multiple records. Nested structures allow structures within other structures.

Uploaded by

Umer Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Structures

Fahad Majeed
UET Taxila
2

Programming

Structures
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.

3
Structures
• Structures hold data that belong together.
• 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.
4
Structures
• Individual components of a struct type are called
members (or fields).
• Members can be of different types (simple, array
or struct).
• A struct is named as a whole while individual
members are named using field identifiers.
• Complex data structures can be formed by
defining arrays of structs.
5
struct basics
• Definition of a structure:
struct <struct-type>{
<type> <identifier_list>; Each identifier
<type> <identifier_list>; defines a member
...
of the structure.
} ;

• Example:
struct Date {
int day; The “Date” structure
int month; has 3 members,
int year;
} ; day, month & year.
6
struct examples
• Example:
struct StudentInfo{
int Id;
int age; The “StudentInfo”
char Gender; structure has 4 members
double CGA;
}; of different types.

• Example:
struct StudentGrade{
char Name[15];
char Course[9]; The “StudentGrade”
int Lab[5];
int Homework[3]; structure has 5
int Exam[2]; members of
};
different array types.
7
struct examples
• Example:
struct BankAccount{
char Name[15]; The “BankAcount”
int AcountNo[10]; structure has simple,
double balance; array and structure
Date Birthday;
types as members.
};

• Example:
struct StudentRecord{ The “StudentRecord”
char Name[15]; structure has 4
int Id;
char Dept[5]; members.
char Gender;
}; 8
Program to assign data to members of a
structure variable and display it.
#include <iostream>
using namespace std;
struct Person
{
char name[50];
int age;
float salary;
};
int main()
{
Person p1;
cout << "Enter Full name: ";
cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary; return 0;
}
OUTPUT

Enter Full name:


XYZ JKL
Enter age: 27
Enter salary: 1024.4

Displaying Information.
Name: XYZ JKL
Age: 27
Salary: 1024.4
Example: Store and Display Information Using
Structure
#include <iostream>
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
student s;
cout << "Enter information," << endl;
cout << "Enter name: ";
cin >> s.name;
cout << "Enter roll number: ";
cin >> s.roll;
cout << "Enter marks: ";
cin >> s.marks;
cout << "\nDisplaying Information," << endl;
cout << "Name: " << s.name << endl;
cout << "Roll: " << s.roll << endl;
cout << "Marks: " << s.marks << endl;
return 0; }
OUTPUT

Enter information,
Enter name: XYZ
Enter roll number: 4
Enter marks: 55.6

Displaying Information,
Name: XYZ
Roll: 4
Marks: 55.6
struct basics
• Declaration of a variable of struct type:
<struct-type> <identifier_list>;
• Example:
StudentRecord Student1, Student2;

Student1 and Student2 are variables of StudentRecord type.

Student1 Name Name Student2


Id Gender Id Gender

Dept Dept

13
struct basics
• The members of a struct type variable are accessed
with the dot (.) operator:
<struct-variable>.<member_name>; Student1

Name
• Example:
strcpy(Student1.Name, "Chan Tai Man"); Id Gender
Student1.Id = 12345;
strcpy(Student1.Dept, "COMP"); Dept
Student1.gender = 'M';
cout << "The student is ";
switch (Student1.gender){
case 'F': cout << "Ms. "; break; Chan Tai Man
case 'M': cout << "Mr. "; break;
}
cout << Student1.Name << endl; 12345 M

COMP
14
15
struct-to-struct assignment
• The values contained in one struct type variable can be
assigned to another variable of the same struct type.
Student1
• Example:
Chan Tai Man
strcpy(Student1.Name,
"Chan Tai Man"); 12345 M
Student1.Id = 12345;
strcpy(Student1.Dept, "COMP");
Student1.gender = 'M'; COMP
Student2 = Student1;

Chan Tai Man

12345 M
Student2
16 COMP
17
Nested structures
• We can nest
structures inside
structures.
(P.x, P.y)

• Examples: (L.p2.x, L.p2.y)


struct point{ (L.p1.x, L.p1.y)
double x, y;
};
point P;
(T.p2.x, T.p2.y)
struct line{
point p1, p2;
};
line L;
struct triangle{ (T.p3.x, T.p3.y)
point p1, p2, p3;
};
triangle T;
(T.p1.x,
18
T.p1.y)
Nested structures
• We can nest structures inside structures.

• struct line{
point p1, p2; (L.p2.x, L.p2.y)
};
line L; (L.p1.x, L.p1.y)

line

p1 p2

x y x y

19
Arrays of structures
• An ordinary array: One type of data

0 1 2 … 98 99

• An array of structs: Multiple types of data in each array


element.

0 1 2 … 98 99
20
Arrays of structures
• We often use arrays of structures.
• Example:
StudentRecord Class[100];
strcpy(Class[98].Name, "Chan Tai Man");
Class[98].Id = 12345;
strcpy(Class[98].Dept, "COMP");
Class[98].gender = 'M';
Class[0] = Class[98]; Chan Tai Man

12345 M

COMP

...

0 1 2 … 21 98 99
Arrays inside structures
• We can use arrays inside structures.
• Example: (4, 3) (10, 3)
struct square{
point vertex[4];
};
square Sq; (4, 1) (10, 1)

• Assign values to Sq using the given square

x y x y x y x y

22
Reference
• https://www.youtube.com/watch?v=-IrueTrxNHA

You might also like