Download presentation
Presentation is loading. Please wait.
Published byEdgar Hubbard Modified over 9 years ago
1
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that it can hold o Ex. char variables can hold only characters defined by the ASCII or extended ASCII code sets (i.e. 0 to 255) o Ex. short variables can hold only values from -32,768 and 32,767 o Ex. bool variables can hold only true or false in counterpart, has specific values or range of values that it cannot hold o int cannot hold fractional numbers o bool cannot hold characters or floating point are subject to being involved in specific types of operations o Ex. int, float double may be manipulated with the arithmetic and/or logical operators o int can be manipulated with % (modulus) but float/double cannot
2
Primitive Data Types Primitive data types are those data types which are defined as part of the core C++ language: short it, int, long int, double, float, bool, char, etc.
3
Abstract Data Types Abstract data types are data types that are created by the programmer and which are composed of one or more primitive data types or other previously defined abstract data types Programmer defines: Acceptable values Operations that can be performed We will focus on an abstract data type that is just used to group related data.
4
Structures (struct) A programmer can define a new data type using a ‘struct’ declaration. A struct creates a relationship between variables and has the following characteristics: a name which is the name of the abstract data type members which are data types within the ‘struct’ Syntax: struct struct_name { data_type member_name1; data_type member_name2; … };//Note the ; at the end of the definition
5
Example Let’s suppose we have a marathon race and are recording participant information in a computer. We might want the following information about each participant. struct participant { int id_number; char l_name[12]; char f_name[12]; int age; char sex; bool finish; float time; }; We could declare multiple variables to be of the defined type ‘participant’ participant master_list[200];//Array of all registered participants participant over_50_male_winner, over_50_female_winner, overall_winner, under_12_winner;
6
Accessing Structure Members To access the individual members of a structure we use what is called the ‘.’ (dot) operator. This operator allows us to use the members of a struct variable just like any other variable. Syntax: structure_variable_name.member_name1 Examples: over_50_female_winner.age = 54; master_list[i].sex = ‘F’; master_list[7].f_name = “Sharon”; if (master_list[j].finish) PrintCertificate(master_list[j]); // This is a pass by value
7
Initialization Like arrays, structs can be initialized with a list of values. Not all members of a structure have to be initialized in the list but you cannot ‘skip’ a member. Syntax: struct_name var_name = {val_mem1, val_mem2, …); Example: participant youngest = {12300, “Smith”,”Annie”, 10}; Member fields sex, finish, Time are left uninitialized. The following statement would not be legal. participant youngest = {12300,,,10};
8
Things to Note About Structures When checking for equality, like arrays that have to be compared element by element, structures must be compared member by member. Arrays of structures are declared/defined just like any other array (Ex. participant master_list[200];) It is possible to have nested structures, that is, a structure as a member of another structure. The definition of any member must precede that of the structure of which it is a member. Structures may be passed as arguments to functions
9
Structures as Function Arguments Unlike arrays, structures are normally passed by value just like most other variables. This means a copy of the values of the structure members is being passed not the address of the structure variable. HOWEVER – if you have defined a large structure (perhaps it has a member array or just many members) then the performance of your program will suffer if the function is called many times. In such cases, it is desirable to pass the address of the structure. If the function should not be able to modify the contents of the structure include a const specification in the definition and prototype.
10
Returning Structures from Functions Structures types may be used as the return type for functions. In the source code file, the structure type definition must be placed above any function utilizing it as a return type. struct meeting { int room_number; int month; int day; int hour; int minutes; float length; }; meeting GetMeeting( ) { meeting new_meeting; cout << “Enter desired month: “; cin >> new_meeting.month; cout << “\nEnter desired day: “; cin >> new_meeting.day; cout << “\nEnter the desired hour (0 – 23): “; cin << “\nEnter the desired minutes (0-59): “; cout << “\nEnter the length in hours (ex. 1.5): “; cin >> new_meeting.length; return new_meeting; }
11
Calling a Function Returning a Structure int main() { meeting new_session; int room_no; new_session = GetMeeting(); room_no = FindAvailableRoom(new_session); new_session.room_number = room_no; cout << “Your meeting is scheduled to be held in room “ << room_no; … } Note: Using a structure return type for a function allows the function to return more than one value by packaging those values into the members of the returned structure.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.