Chapter Six - Structures - All Parts
Chapter Six - Structures - All Parts
Programming {
Chapter Six: Structures
in C++
{
1
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Outline
1.Specifying simple structure
2.Defining a structure variable
3.Accessing structure variable
2
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Records
▪ Aggregates of several items of possibly different types that represent related
information called RECORD
E.g
▪ Student Record:
▪ Name a string
▪ HW Grades an array of 3 doubles
▪ Test Grades an array of 2 doubles
▪ Final Average a double
▪ Bank Record:
▪ Customer Name
▪ Account Balance
▪ Amount
3
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Records
▪ Arrays
▪ Recall that elements of arrays must all be of the same type
scores : 85 79 92 57 68 80 . . .
0 1 2 3 4 5 98 99
▪ They are commonly used when you want to represent an object with multiple
attributes
▪ Structures are used to organize related data (variables) into a nice and neat package.
▪ A structure definition is a user-defined variable type: You can create variables from it
just like you would from any other type
5
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Structures in C++
▪ Before creating a structure variable you must create a structure definition
▪ Syntax:
struct structname Tag
{
datatype1 variable1;
Members
datatype2 variable2;
};
▪ This is simply a data blue print
▪ Individual components of a struct type are called members (or fields).
▪ Each member has a name, a type - Names follow the rules for variable names.
▪ Types can be any defined type
6
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
struct Examples
struct Date { struct StudentGrade{
char Name[15];
int day; The “Date” structure The “StudentGrade”
char Course[9];
int month; has 3 members, int Lab[5]; structure has 5
int year; day, month & year. int Homework[3]; members of
}; int Exam[2]; different array types.
};
struct StudentInfo{
int Id; struct BankAccount{
The “StudentInfo” char Name[15]; The “BankAcount”
int age; structure has simple,
structure has 4 int AcountNo[10];
char Gender; array and structure
members double balance;
double CGPA; types as members.
Date Birthday;
}; of different types.
};
7
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Declaring and using structs
▪ By defining a structure you create a new data type.
▪ Once you have defined a structure you can create a variable from it just
as you would any other variable. struct StudentRecord
▪ Syntax: <struct-type> <identifier_list>; {
char Name[15];
▪ Example: StudentRecord Student1, Student2; int Id;
char Dept[5];
char Gender;
};
Name Name
10
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Structure Example
#include<iostream.h>
cin>>s2.id;
struct student
{ cout<<"\nEnter Name";
int id;
char name[15]; cin>>s2.name;
}; cout<<"\nStudents Information";
int main()
{ cout<<"\n Student id\t Student Name";
//creating three student variables cout<<endl<<s1.id<<"\t\t"<<s1.name;
student s1,s2;
cout<<"\n Enter Student Id"; cout<<endl<<s2.id<<"\t\t"<<s2.name;
cin>>s1.id; return 0;
cout<<"\nEnter Name";
cin>>s1.name; }
cout<<"\n Enter Student Id";
11
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Aggregate Operations with Structures
Limitations on aggregate operations
▪ no I/O cout << s1;
cin >> s1;
13
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Array of structs
▪ What if we have a record of 120100 students
▪ Array of records
▪ student stud1[50];
cin>>js.name;
cin>>js.person.id;
cin>>js.person.gpa;
return 0; }
20
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Structure with Member Functions
#include <iostream>
// Member function to display student details
#include <string> void displayInfo() {
struct Student { std::cout << "Student Name: " << name << std::endl;
std::string name; std::cout << "Roll Number: " << rollNumber << std::endl;
std::cout << "Grade: " << grade << std::endl;
int rollNumber;
}
char grade;
};
// Member function to display student details int main() {
void registerStudent() { Student student1;
std::cout << "Student Name: "<<std::endl; // Using the member function
student1.registerStudent();
std::cin>>name;
student1.displayInfo();
std::cout << "Roll Number: " <<std::endl;
return 0;
std::cin>>rollNumber; }
std::cout << "Grade: "<< std::endl;
std::cin>>grade;
}
21
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Chapter 6;
}
22