Download presentation
Presentation is loading. Please wait.
1
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.
2
Declaring a Struct Type and Struct Variable
Synatx: struct struct_name { <type> field1-name; <type> field2-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.
3
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 pay1; declares pay1 variable to be of type PAYROLL_REC It Allocates storage space for two variables: hours, rate.
4
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., pay1. hours = 30.0 pay1.rate = 2.5
5
Examples on Struct Example1: Using a structure to calculate a weekly salary. #include <iostream.h> struct PAYROLL_REC { double hours; double rate; }; void main() { PAYROLL_REC pay1; pay1.hours = 40.0; pay1.rate = 3.75; cout << "This week's payroll information:" << endl; cout << "Hours worked : " << pay1.hours << endl; cout << "Rate :$" << pay1.rate << endl; double salary = pay1.rate * pay1.hours; cout << "Salary :$" << salary << endl; }
6
Examples on Struct void main( ) { ExamStats student;
Example 2: Using struct and array to store student’s information. #include <iostram.h> #include <cstring> struct ExamStats { char name [20]; int scores[4]; float average; char grade; }; void printStats(ExamStats stuExams ) { cout<<“Exam scores for “<< stuExams.name<<“: “ << stuExams.scores[0]<<“ “<< stuExams.scores[1]<<“ “<< stuExams.scores[2]<<endl; cout <<“Average score: “<<stuExams.average<<endl; cout<<“Letter garde: “<<stuExams.grade<<endl; } void main( ) { ExamStats student; cin.getline(student.name, 20, ‘\n’); for (int i = 0; i<3; i++) cin >>student.scores [i]; cin >> student.average; cin >> student.grade; printStats (student); }
7
Array of Structs Example: We want to store in an array the students structs defined previously.
8
Array of Structs #include <iostream.h> #include <cstring>
struct ExamStats { char name [20]; int scores[3]; float average; char grade; }; void printStats(ExamStats 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; } } void main( ) { ExamStats 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; printStats (students); }
9
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.
10
Pointers to Structs (Cont.)
Accessing elements of a struct by a pointer: p->current [0]= ‘A’; p->current [1]= ‘C’; p->volts = 115;
11
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; }
12
Returning structure from function in C++
#include <iostream> using namespace std; struct person { char name[50]; int age; float salary; }; person getData(person); void displayData(person); int main() { person p; p = getData(p); displayData(p); return 0; }
13
person getData(person p1) {
cout << "Enter Full name: "; cin.get(p1.name, 50); cout << "Enter age: "; cin >> p1.age; cout << "Enter salary: "; cin >> p1.salary; return p1; } void displayData(person p1) { cout << "\nDisplaying Information." << endl; cout << "Name: " << p1.name << endl; cout <<"Age: " << p1.age << endl; cout << "Salary: " << p1.salary;
14
Example: #include <iostream> using namespace std; struct temp { int data1; float data2; void int_data(int d){ data1=d; cout<<"Number: "<<data1; } float float_data(){ cout<<"\nEnter data: "; cin>>data2; return data2; } }; int main(){ temp obj1, obj2; obj1.int_data(12); cout<<"You entered "<<obj2.float_data(); return 0; }
15
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.
16
Struct + File (The code)
#include <iostream> #include <string> #include <fstream> using namespace std; 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; while (!emp_file.eof()) { size++; emp_file >> emp[size].id; emp_file >> emp[size].name; emp_file >> emp[size].salary; 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();
17
Struct + File (Output)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.