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

Chapter 5 Structure in C++ Programming

Uploaded by

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

Chapter 5 Structure in C++ Programming

Uploaded by

Murad Demelash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

1

Chapter 5

Structures

Compiled By dagne Walle


⚫ Suppose I wish to store information about a
student:
⚫ name: string
⚫ student ID: integer
⚫ gpa: double

Compiled By dagne Walle 2


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 Compiled By dagne Walle


Structure : terminology
⚫ A struct is a group of items (variables) which may
be of different types.
⚫ Each item is identified by its own identifier, each
of which is known as a member or field of the
structure.
⚫ A struct is sometimes called a record or structure.
⚫ Structs are the basis of classes in C++ and Java.

Compiled By dagne Walle 4


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.
5 Compiled By dagne Walle
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.
6 Compiled By dagne Walle
⚫ Structs
⚫ a collection of a fixed number of components in which
the components are accessed by name
⚫ components may be of a different type
⚫ Syntax: struct name can be any
valid identifier
struct struct_name {
dataType1 name1; these are variable
dataType2 name2; declarations.
dataType3 name3;

dataTypen namen;
};

Compiled By dagne Walle 7


struct basics

⚫ Definition of a structure:
struct <struct-type>{
Each identifier
<type> <identifier_list>; defines a member
<type> <identifier_list>; of the structure.
...
};
⚫ Example:
struct Date { The “Date” structure
int day;
int month; has 3 members,
int year; day, month & year.
}; 8 Compiled By dagne Walle
struct examples

⚫ Example:
struct StudentInfo{
The “StudentInfo”
int Id;
structure has 4 members
int age;
char Gender; of different types.
double CGA;
};
⚫ Example:
struct StudentGrade{ The “StudentGrade”
char Name[15]; structure has 5
char Course[9];
int Lab[5]; members of
int Homework[3]; different array types.
int Exam[2];
9 Compiled By dagne Walle
};
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; members.
char Dept[5];
char Gender;
}; 10 Compiled By dagne Walle
struct basics
⚫ Declaration of a variable of struct type:
<struct-type> <identifier_list>;

⚫ Example:
StudentRecord Student1, Student2;

Name Name
Student1 Student2
Id Gender Id Gender

Dept Dept

Student1 and Student2 are variables of


StudentRecord type.
11dagne Walle
Compiled By
Ex. 1: struct basics

⚫ The members of a struct type variable are


accessed with the dot (.) operator: Student1
<struct-variable>.<member_name>;
Name
⚫ Example: Id Gender
strcpy(Student1.Name, "Chan Tai Man");
Dept
Student1.Id = 12345;
strcpy(Student1.Dept, "COMP");
Student1.gender = 'M'; Chan Tai Man
cout << "The student is ";
switch (Student1.gender){ 12345 M
case 'F': cout << "Ms. "; break;
COMP
case 'M': cout << "Mr. "; break;
} 12 Compiled By dagne Walle
cout << Student1.Name << endl;
Create Structure Type Student

❖ struct Student {
int ID;
char Name ;
char Sex ;
char Department;
float Result;
char Grade;
};Student Stud [100];
Structure is a data structure that contain different data types

Compiled By dagne Walle 13


Read Student Information Using Structure

void Add_Stud_Data (int NStu) {


for (int i=0; i<100; i++) {
cout << "\n\n\t Please enter ID No. of the Student: \n\n\t";
cin >> Stud [i]. ID;
cout << "\n\n\t Please enter NAME of the Student: \n\n\t";
cin>>Stud [i]. Name;
cout << "\n\n\t Please enter SEX of the Student: \n\n\t";
cin>>Stud [i]. Sex;
cout << "\n\n\t Please enter DEPARTMENT of the Student: \n\n\t";
cin>>Stud [i]. Department;
cout << "\n\n\t Please enter RESULT of the Student: \n\n\t";
cin >> Stud [i]. Result;}}

Compiled By dagne Walle 14


Compute Student Grade Using Structure

char Calculate_Stud_Grade () {
if (Stud [i]. Result >= 90.0)
Stud [i]. Grade = 'A';
else if (Stud [i]. Result >= 80.0)
Stud [i]. Grade = 'B';
else if (Stud [i]. Result >= 70.0)
Stud [i]. Grade = 'C';
else (Stud [i]. Result > =10.0)
Stud [i]. Grade = 'F';
return (Stud [i]. Grade);
}
Compiled By dagne Walle 15
Create Structure type Student

int main()
{#include <iostream>
#include <string>
using namespace std;
// Define a structure to represent student information
struct Student {
string name;
int age;
string address;
string department;
};
Compiled By dagne Walle 16
Read student Information Using Structure

int main() {
const int MAX_STUDENTS = 3;
Student students[MAX_STUDENTS];
for (int i = 0; i < MAX_STUDENTS; ++i) {
cout << "Student " << i + 1 << ":" << endl;
cout << "Name: ";
getline(cin, students[i].name);
cout << "Age: ";
cin >> students[i].age;
cin.ignore(); // Ignore newline character
cout << "Address: ";
getline(cin, students[i].address);
cout << "Department: ";
getline(cin, students[i].department);
Compiled By dagne Walle 17
}
Display Student Information Using Structure

int main() {

// Display student information


cout << "\nDisplaying student information:" << endl;
for (int i = 0; i < MAX_STUDENTS; ++i) {
cout << "Student " << i + 1 << ":" << endl;
cout << "Name: " << students[i].name << endl;
cout << "Age: " << students[i].age << endl;
cout << "Address: " << students[i].address << endl;
cout << "Department: " << students[i].department << endl;
cout << "----------------------------------" << endl;
return 0; }

Compiled By dagne Walle 18


Create Book Structure

#include<iostream>
#include<cstring>
using namespace std;
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};

Compiled By dagne Walle 19


Read Book Information Using Structure

int main()
{
struct Books Book1;
struct Books Book2;
strcpy(Book1.title,"Mathematics for IITJEE");
strcpy(Book1.author,"RD Sharma");
strcpy(Book1.subject,"JEE Mathematics");
Book1.book_id = 6495407;
strcpy( Book2.title,"Physics");
strcpy( Book2.author,"IE Irodov");
strcpy( Book2.subject,"JEE Mechanical Physics");
}

Compiled By dagne Walle 20


Display Book Information Using Structure

int main()
{
cout<<"Book 1 title : "<<Book1.title<<endl;
cout<<"Book 1 author : "<<Book1.author<<endl;
cout<<"Book 1 subject : "<<Book1.subject<<endl;
cout<<"Book 1 id : "<<Book1.book_id<<endl;
cout<<"Book 2 title : "<<Book2.title<<endl;
cout<<"Book 2 author : "<<Book2.author<<endl;
cout<<"Book 2 subject : "<<Book2.subject<<endl;
cout<<"Book 2 id : "<<Book2.book_id<<endl;
return 0;
}

Compiled By dagne Walle 21


Ex. 2: struct-to-struct assignment

⚫ The values contained in one struct type


variable can be assigned to another variable
Student1
of the same struct type.
Chan Tai Man
⚫ Example: 12345 M
strcpy(Student1.Name,
COMP
"Chan Tai Man");
Student1.Id = 12345;
strcpy(Student1.Dept, "COMP");
Student1.gender = 'M';
Chan Tai Man
Student2 = Student1; 12345 M
Student2 COMP
Compiled By dagne Walle 22
Struct Variable

⚫ How to access the variables in a struct?


⚫ use the dot (.) operator
int main() {

struct Date {
int day;
int month;
int year;
};

Date birthday;

birthday.day = 28;
birthday.month = 12;
birthday.year = 86;

cout << "My birth date is "


<< birthday.month << '/'
<< birthday.day << '/'
<< birthday.year << endl;
Compiled By dagne Walle 23
...
Structure

⚫ What if I want two dates?


Without Structs With Structs
int main() { int main() {

int day_birth, int day_grad; struct Date {


int month_birth, int int day;
month_grad; int month;
int year_birth, int year_grad; int year;
};
... Date birth, grad;
...

Compiled By dagne Walle 24


Multiple Structure
struct Date {
int day, month, year;
};
struct Course {
string name;
int start_day, start_month, start_year;
int finish_day, finish_month, finish_year;
};
int main() {
Course cs1620;
cs1620.name = "Computer Science 1620";
cs1620.start_day = 5;
cs1620.start_month = 9;
cs1620.start_year = 2007;
cs1620.finish_day = 7;
cs1620.finish_month = 12;
cs1620.finish_year = 2007;
return 0;
}
Compiled By dagne Walle 25
Exercise

⚫ Read in student information


⚫ Output student data to screen

⚫ Calculate average mark of class

⚫ Output average mark of class

Compiled By dagne Walle 26


Thank You

Compiled By dagne Walle 27

You might also like