Download presentation
Presentation is loading. Please wait.
1
Lec17 Structs
2
Review of Data Types float: floating point numbers
int : integer numbers char: single ASCII character string: a class representing a sequence of characters ifstream, ofstream: classes representing pathways to files
3
These are great but somewhat limited
How would you represent an automobile in a computer? What about an ATM bank account? An employee entry in a database? And so on We need a type that goes beyond simple one type data. Enter…the struct!!
4
Structured Data Types A structured data type is one in which each value is a collection of components and whose organization is characterized by the method used to access individual components (i.e. the dot ‘.’ operator)
5
Structs A record or a struct in C++, is a structured data type with a fixed number of components that are accessed by name. The components may be heterogeneous (mixed types). A field or component is a piece of the record.
6
Syntax of struct definition
struct TypeName { DataType MemberName; … };
7
This would go above main()
Example: StudentRec struct StudentRec { string firstName; string lastName; float GPA; int ID; }; This would go above main()
8
Creating StudentRec variables
Once you declare a struct StudentRec, it becomes a new data type you can use StudentRec Bill, Tamara; Bill Tamara firstName ID firstName ID lastName GPA lastName GPA
9
Accessing a field To assign a value to one of the fields, you use the member selector operator ‘.’ StudentRec Bill; // create a variable of type StudentRec Bill.firstName = “William”; Bill.lastName = “Borroughs”; Bill.ID=333; Bill.GPA=3.87; cout<<Bill.ID; cin>>Bill.lastName; Bill firstName ID William 333 GPA lastName 3.87 Borroughs
10
Your turn Declare a student record for a variable x
Make x store the data for Kalina Noori, who’s ID is 456 and GPA is 3.97 Display the first and last names of x on the same line Input the ID and GPA of x x ID firstName lastName GPA
11
Aggregate Operations (performed on a struct as a whole)
Assignment Arithmetic Comparison Passing Arguments Return from a function Allowed on Structs? No Yes Yes, value and reference Operations labelled ‘no’ can still be performed on a field by field basis (see next slide)
12
Example How to do the operations on StudentRec x, y;
Assignment Arithmetic Comparison Passing to Fn Function return Correct Example cin>>x.firstName>>x.lastName >>x.ID>>x.GPA; (Wrong: cin>>x) y=x; (copies all fields) or x.ID=y.ID y.GPA=x.GPA+y.GPA; (Wrong: x=x+y) if (x.ID < y.ID) (Wrong: if (x<y)) Display(x); OR y.GPA=sqrt(x.GPA) return x; OR return x.GPA
13
Initializing Structs When a struct is created you can initialize it with a list of values (initialization list): StudentRec s={“Isa”, “Corwin”,3.5, 555}; The sequence must match the order of declaration in the struct (see slide 7): NO: StudentRec s={“Isa”,“Corwin”,555,3.5};OOPS You can only do this on declaring StudentRec t; NO: t={“Bert”,“Lance”,3.5, 555}; ERROR
14
Data Abstraction Data abstraction is the separation of a data’s logical properties from its implementation, for example: Logical Properties of a real number: addition, multiplication, positive, negative Implementation of a real number: Hardware, limited bits, mantissa, exponent how these can be used to provide adding, multiplication An Abstract Data Type is a data type whose properties are specified independently of any particular implementation. How we represent real things on a computer This leads us to Classes (2 weeks!)
15
TRY IT NOW!! Prepare for Project 2, Part 2
Practice basic concepts of structs by completing Assignment 11, problems 1-7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.