Struct Data Type in C What Are Structures

  • Slides: 17
Download presentation
Struct Data Type in C++ • What Are Structures? • A structure is a

Struct Data Type in C++ • What Are Structures? • A structure is a data type that is an aggregate; that is, it contains other data types, which are grouped together into a single user-defined type. • It is like an array, but the array is homogenous and the structure is heterogeneous • Structures are used when it makes sense to associate two or more data variables. • Structures are made up of member variables. 1

Declaring a Struct Type and Struct Variable • Synatx: struct_name { <type> field 1

Declaring a Struct Type and Struct Variable • Synatx: struct_name { <type> field 1 -name; <type> field 2 -name; … <type> fieldn-name; }; • Declaring a structure introduces a new type of variable into your program, struct variables. • Variables of this new type can be defined just like int, char, or float variables are defined. 2

Declaring a Struct Type and Struct Variable • An example of a structure is

Declaring a Struct Type and Struct Variable • An example of a structure is a payroll record, where the number of hours worked and the pay rate are combined in a structure, as shown below: struct PAYROLL_REC { double hours; double rate; }; • The declaration PAYROLL_REC pay 1; declares pay 1 variable to be of type PAYROLL_REC • It Allocates storage space for two variables: hours, rate. 3

Accessing Members of a Struct • Accessing struct is by using the member access

Accessing Members of a Struct • Accessing struct is by using the member access operator, a period ( a dot) placed between a struct variable and a member name for that struct type. • E. g. , pay 1. hours = 30. 0 pay 1. rate = 2. 5 4

Examples on Struct Example 1: Using a structure to calculate a weekly salary. #include

Examples on Struct Example 1: Using a structure to calculate a weekly salary. #include <iostream. h> struct PAYROLL_REC { double hours; double rate; }; void main() { PAYROLL_REC pay 1; pay 1. hours = 40. 0; pay 1. rate = 3. 75; cout << "This week's payroll information: " << endl; cout << "Hours worked : " << pay 1. hours << endl; cout << "Rate : $" << pay 1. rate << endl; double salary = pay 1. rate * pay 1. hours; cout << "Salary : $" << salary << endl; } 5

Examples on Struct Example 2: Using struct and array to store student’s information. #include

Examples on Struct Example 2: Using struct and array to store student’s information. #include <iostram. h> #include <cstring> struct Exam. Stats { char name [20]; int scores[4]; float average; char grade; }; void print. Stats(Exam. Stats stu. Exams ) { cout<<“Exam scores for “<< stu. Exams. name<<“: “ << stu. Exams. scores[0]<<“ “<< stu. Exams. scores[1]<<“ “<< stu. Exams. scores[2]<<endl; cout <<“Average score: “<<stu. Exams. average<<endl; cout<<“Letter garde: “<<stu. Exams. grade<<endl; } 6 void main( ) { Exam. Stats student; cin. getline(student. name, 20, ‘n’); for (int i = 0; i<3; i++) cin >>student. scores [i]; cin >> student. average; cin >> student. grade; print. Stats (student); }

Array of Structs • Example: We want to store in an array the students

Array of Structs • Example: We want to store in an array the students structs defined previously. 7

Array of Structs #include <iostream. h> #include <cstring> struct Exam. Stats { char name

Array of Structs #include <iostream. h> #include <cstring> struct Exam. Stats { char name [20]; int scores[3]; float average; char grade; }; void print. Stats(Exam. Stats stud[]) { for (int i=0; i<20; i++) { cout<<"Exam scores for "<< stud[i]. name<<": " << stud[i]. scores[0]<<" "<< stud[i]. scores[1]<<" "<< stud[i]. scores[2]<<endl; cout <<"Average score: "<<stud[i]. average<<endl; cout<<"Letter garde: “ << stud[i]. grade<<endl; 8} } void main( ) { Exam. Stats students[20]; //Reading students records and storing them in the array for (int i=0; i<20; i++) { cout<<"Enter student "<<i+1<< "information: "; cin. getline(students[i]. name, 20); for (int j= 0; j<3; j++) cin >>students[i]. scores [j]; cin >> students[i]. average; cin >> students[i]. grade; print. Stats (students); } }

Pointers to Structs • We can declare pointers to structured data. • E. g.

Pointers to Structs • We can declare pointers to structured data. • E. g. , the declaration struct electric { char current[2]; int volts; }; electric *p, *q; Defines the variables p and q to be of type “pointer to electric”. Type electric is a struct of two variables: current and volts. 9

Pointers to Structs (Cont. ) • Accessing elements of a struct by a pointer:

Pointers to Structs (Cont. ) • Accessing elements of a struct by a pointer: p->current [0]= ‘A’; p->current [1]= ‘C’; p->volts = 115; 10

Example: #include <iostream> using namespace std; struct Distance { int feet; float inch; };

Example: #include <iostream> using namespace std; struct Distance { int feet; float inch; }; int main() { Distance *ptr, d; ptr = &d; cout << "Enter feet: "; cin >> (*ptr). feet; cout << "Enter inch: "; cin >> (*ptr). inch; cout << "Displaying information. " << endl; cout << "Distance = " << (*ptr). feet << " feet " << (*ptr). inch << " inches"; return 0; } 11

Returning structure from function in C++ #include <iostream> using namespace std; struct person {

Returning structure from function in C++ #include <iostream> using namespace std; struct person { char name[50]; int age; float salary; }; person get. Data(person); void display. Data(person); int main() { person p; p = get. Data(p); display. Data(p); return 0; } 12

person get. Data(person p 1) { cout << "Enter Full name: "; cin. get(p

person get. Data(person p 1) { cout << "Enter Full name: "; cin. get(p 1. name, 50); cout << "Enter age: "; cin >> p 1. age; cout << "Enter salary: "; cin >> p 1. salary; return p 1; } void display. Data(person p 1) { cout << "n. Displaying Information. " << endl; cout << "Name: " << p 1. name << endl; cout <<"Age: " << p 1. age << endl; cout << "Salary: " << p 1. salary; } 13

Example: #include <iostream> using namespace std; struct temp { int data 1; float data

Example: #include <iostream> using namespace std; struct temp { int data 1; float data 2; void int_data(int d){ data 1=d; cout<<"Number: "<<data 1; } float_data(){ cout<<"n. Enter data: "; cin>>data 2; return data 2; } }; int main(){ temp obj 1, obj 2; obj 1. int_data(12); cout<<"You entered "<<obj 2. float_data(); return 0; } 14

Struct + File • Write a C++ program that reads employees information from a

Struct + File • Write a C++ program that reads employees information from a file, store them in a struct, and finds the maximum salary and the corresponding employee. 15

Struct + File (The code) #include <iostream> #include <string> #include <fstream> using namespace std;

Struct + File (The code) #include <iostream> #include <string> #include <fstream> using namespace std; while (!emp_file. eof()) { size++; emp_file >> emp[size]. id; emp_file >> emp[size]. name; emp_file >> emp[size]. salary; struct employee { int id; string name; float salary; }; void main () { employee *emp = new employee[20]; int size = 0; ifstream emp_file; emp_file. open("emp. txt", ios: : in); emp_file >> emp[size]. id; emp_file >> emp[size]. name; emp_file >> emp[size]. salary; float max_salary = emp[size]. salary; int max_index = size; 16 if (emp[size]. salary > max_salary) { max_salary = emp[size]. salary; max_index = size; } } cout << "The maximium salary is = "<< max_salary << ", for the employee: "<< emp[max_index]. name<< endl; emp_file. close(); }

Struct + File (Output) 17

Struct + File (Output) 17