Download presentation
Presentation is loading. Please wait.
1
CO1401 Program Design and Implementation
Week 7 Akiyo Kano
2
This Week Structures What are structures? How to define structures.
How to define members of structures. How to create an instance of structures. Arrays of structures.
3
Structures With an array, all the elements have to be the same type and it is accessed by a numerical position in the array. But what if we want to group data of different types? e.g. Details about employees - name, age, rate, etc. which are different data types. C++ allows you to group several variables together in a single item known as a structure.
4
Structure A structure is a collection of related data, which does not have to be the same type, and each field is accessed by name. Structure Name ID Num D.O.B Rate Address[] D.O.B is Date of Birth. Employee
5
Example A company payroll system might keep the following variables, which relate to a single employee: int EmpNumber; // Employee Number char Name[SIZE]; // Employee’s Name double Hours; // Hours worked double PayRate; // Hourly Rate paid double GrossPay; // Gross pay All these variables are related because they hold data about the same employee.
6
Making It Clear These variables on their own does not make it clear that they belong together. We can package them in a structure to create a relationship between them. Group them into a structure.
7
Declaring a Structure Before we can use a structure, we must first declare it. Syntax for declaring structure: struct tag { variable declaration; }; “Members” Don’t forget the semi-colon at the end of the brackets!!
8
Example - PayRoll Structure
const int SIZE = 25; // Array Size struct PayRoll { int EmpNumber; //Employee Number char Name[SIZE]; // Empolyee’s Name double Hours; // Hours worked double PayRate; // Hourly rate double GrossPay; // Gross Pay };
9
Structure Declaration
Declaring a structure does not define the values of a varaible. It is bit like defining what the data type int is. It tells the compiler what the structure PayRoll is made of. Creates a new data type named PayRoll like int, char, float, but defined by you.
10
Defining An Instance of A Structure
Now that we have defined a new data type, we can create variables that is of the new data type. int Num; Num is a variable of type int. PayRoll DeptHead; DeptHead is a variaible of type PayRoll you defined. We say DeptHead is an instance of the structure PayRoll
11
Template & Forms Defining your structure is like creating a template for someone to fill in. Declaring a DeptHead variable of the type PayRoll is like photocopying the form, and assigning it to that person. EmpNumber Name Hours PayRate GrossPay PayRoll “Template” EmpNumber Name Hours PayRate GrossPay DeptHead
12
Declaring Multiple Instances
Just as you can declare multiple variables of the same data type on one line, you can do the same for multiple instances of a structure: int Num1, Num2, Num3; PayRoll DeptHead, Foreman; Associate; Seperate instrances of PayRoll structure, containing Their own members with the same name.
13
PayRoll DeptHead, Foreman; Associate;
EmpNumber Name Hours PayRate GrossPay DeptHead Foreman Associate EmpNumber Name Hours PayRate GrossPay PayRoll “Template”
14
More Examples struct Time { int Hour; int Minutes; int Seconds; }
Time Now; Time Before; struct Date { int Day; int Month; int Year; } Time Today; Time Yesterday;
15
Steps In Implementing Structures
There are three steps to implementing and using structures: Create the structure Define instances of the structure Use the instances
16
Accessing Structure Members
In arrays, to access the elements in it, we had to define where abouts in the array it is at. In structures, we don’t have to worry about where it is, as we can access each member by name. we do this by using the dot operator (.)
17
Assigning a Value to a Member
To assign a value to a member of an instance: DeptHead.EmpNumber = 457; Foreman.EmpNumber = 897; EmpNumber Name Hours PayRate GrossPay 475 DeptHead EmpNumber Name Hours PayRate GrossPay 897 Foreman
18
Also... You can initialise values of members when the variable is declared: Struct Time { int Hour; int Minute; int Seconds; }; main() Time Now = {12, 30, 10}; }
19
Accessing Value of a Member
If you want to print out all details of DeptHead: cout << DeptHead.EmpNumber << endl; cout << DeptHead.Name << endl; cout << DeptHead.Hours << endl; cout << DeptHead.PayRate << endl; cout << DeptHead.GrossPay << endl;
20
Entire Program #include <stdafx.h> #include <iostream>
#include <iomanip> using namespace std; const int SIZE = 25; struct PayRoll { int EmpNumber; // Employee number char Name[SIZE]; // Employee’s name double Hours; // Hours worked double PayRate; // Hourly Pay Rate double GrossPay; // Gross Pay };
21
// Get the Employee’s Number
void main() { PayRoll Employee; // Get the Employee’s Number cout << “Enter the Employee’s number: “ << endl; cin >> Employee.EmpNumber; // Get the employee’s Name cout << “Enter employee’s name: “ << endl; cin.ignore(); //skip remaining \n char cin.getline(Employee.Name, SIZE); You will need the ignore function for the cin to ignore the next character in the input buffer. This is necessary for the cin.getline statement to work properly in the program.
22
// Get the hours worked by Employee
cout << “Enter hours worked: “ << endl; cin >> Employee.Hours; // Get the employee’s hourly rate cout << “What is the Hourly pay rate?: “ << endl; cin >> Employee.PayRate; //calculate the gross pay Employee.GrossPay = Employee.Hours * Employee.PayRate;
23
// Display the employee Data
cout << “Here is the employee data\n”; cout << “Name: “ << Employee.Name << endl; cout << “Number: “ << Employee.EmpNumber << endl; cout << “Hours worked: “ << Employee.Hours << endl; cout << “Hourly Rate: “ << Employee.PayRate << endl; cout << fixed << showpoint << setprecision(2); cout << “Gross Pay: £” << Employee.GrossPay << endl; system(“pause”); }
24
Displaying Members Note that the contents of a structure variable cannot be displayed by passing the entire variable to cout. cout << Employee << endl; will not work!!
25
Comparing Members Similarly, we cannot just compare if two variables are the same: if (DeptHead == Associate) will not work! if (DeptHead.PayRate == Associate PayRate)
26
Arrays of Structures An array can only hold variables of the same type. But structure is a data type. So we can create an array of a particular structure! int Array[5] PayRoll Array[5] This can hold all the data about each employee in each element of the array.
27
Example - Book Info struct BookInfo { char Title[50]; char Author[30];
char Publisher[25]; double price; }; BookInfo BookList[20]; This array will hold information about 20 books
28
Arrays of Structures To find the title of the book in BookList[5]:
BookList[5].title To display all the info on all the books: for (int x=0; x < 20; x++) { cout << BookList[x].Title << endl; cout << BookList[x].Author << endl; cout << BookList[x].Publisher << endl; cout << BookList[x].Price << endl; }
29
Also... As the members Title, Author and Publishers are also arrays, their individual elements may be accessed as well: cout << BookList[10].Title[0]; This will cout the first letter of the title of the eleventh book in the BookList array.
30
Functions With Structures
Just like any other variable, a function can accept a structure as a parameter. Functions can also return a structure. Function prototype should come AFTER the structure declaration. This allows us to return several variables (members) from a function, as long as the function returns one structure.
31
struct Time { int Hour; int Minute; int Second; }; Time AddHour(Time); main() Time Now, Soon; Now.Hour = 12; Now.Minute = 30; Now.Second = 10; Soon = AddHour(Now); }
32
Time AddHour(Time Now){
Time Soon; Soon.Hour = (Now.Hour+1); Soon.Minute = Now.Minute; Soon.Second = Now.Second; return Soon; }
33
Summary Structure is a form of data type that you can define, made up of collection of members of various data types. A variable can be declared as having a structure type you defined. (instance of a structure) You can access members of a variable by using the dot operator (.) Arrays can be constructed to hold variables of one structure type.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.