Chapter 5 Structure in C++ Programming
Chapter 5 Structure in C++ Programming
Chapter 5
Structures
⚫ 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
❖ 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
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() {
#include<iostream>
#include<cstring>
using namespace std;
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
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");
}
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;
}
struct Date {
int day;
int month;
int year;
};
Date birthday;
birthday.day = 28;
birthday.month = 12;
birthday.year = 86;