Download presentation
Presentation is loading. Please wait.
1
CPS120: Introduction to Computer Science
Structures
2
Using enum enum allows you to create your own simple data types for special purposes Create a type Give it a name Specify values that are acceptable enum sizes {small, medium, large, jumbo}; The compiler assigns an integer to each enum item Open enumtest.cpp #include <iostream.h> // necessary for cout command int main() { enum sizes {small, medium, large, jumbo}; sizes drink_size, popcorn_size; drink_size = large; popcorn_size = jumbo; if (drink_size == large) { cout << "You could have a jumbo for another quarter. \n";} if ((popcorn_size == jumbo) && (drink_size != jumbo)) { cout << "You need more drink to wash down a jumbo popcorn.\n";} return 0; }
3
Composite Data Types Records
A record is a named heterogeneous collection of items in which individual items are accessed by name The elements in the collection can be of various types
4
Structures Structures group variables together in order to make one's programming task more efficient. Any combination of variables can be combined into one structure. This is a useful and efficient way to store data. struct Student { string socSecNum; string lastName; string firstName; int pointsEarned; double gpa; };
5
Using Structures Each of the different variables are called members of the structure Structures allow us to keep related data referring to individual members together Strings, integer, and floating-point variables may be grouped together into one structure. In effect, we have created our own customized data type. The semicolon after the closing curly brace is required
6
Composite Data Types
7
Using the new data structure
The structure definition should be placed above the main function of a program but below the compiler directives Declare an actual variable of this programmer-created data type within a function (such as main) in order to make use of this structureDone with a declaration statement like Student freshmen; This reates a variable called freshmen of the data type Student Open struct2.cpp: #include<iostream.h> #include<iomanip.h> #include<string.h> struct inventory_item { char item_ID[11]; char description[31]; int quantity_on_hand; int reorder_point; double cost; double retail_price; }; int main() inventory_item todays_special; strcpy(todays_special.item_ID, "RGG "); strcpy(todays_special.description, "Remote Control Monster Truck"); todays_special.quantity_on_hand = 19; todays_special.reorder_point = 3; todays_special.cost = 47.80; todays_special.retail_price = 98.99; cout << "Today's Special \n"; cout << " Item ID: " << todays_special.item_ID << endl; cout << " Description: " << todays_special.description << endl; cout << " Quantity: " << todays_special.quantity_on_hand <<endl; cout << "Regular Price: " << setprecision(2) << todays_special.retail_price << endl; cout << " Sale Price: " << todays_special.retail_price * 0.8 << endl; return 0; }
8
Assigning values to the structure
To assign a grade point average (GPA) of 3.4 to the gpa member of the variable freshmen, use the statement: freshmen.gpa = 3.4; The period (.) that is used between the variable name freshmen and the member gpa is called the dot operator. The dot operator simply us to reference individual members of a structure struct Student { string socSecNum; string lastName; string firstName; int pointsEarned; double gpa; }; To assign a last name, you could use the statement, freshmen.lastName = "Smith";
9
Composite Data Types Page 259
10
Nested Structures You can use a previously defined structure as a member of another structure Address is nested inside of the Customer structure. Since Address is used within Customer, the structure definition for Address must be placed above Customer in order to avoid compile errors struct Address { string street; string city; string state; int zip; }; struct Customer { string name; string phone; Address homeAddress; Address businessAddress; }; #include <iostream> #include <string> using namespace std; struct Address { string street; string city; string state; int zip; }; struct Customer string name; string phone; Address homeAddress; Address businessAddress; int main ( ) Customer customer1; customer1.name = "Paul J. Millis"; customer1.phone = " "; customer1.homeAddress.street = "1520 Northbrook Dr"; customer1.homeAddress.city = "Ann Arbor"; customer1.homeAddress.state = "Mi"; customer1.homeAddress.zip = 48103; customer1.businessAddress.street = "1600 Pennsylvania Ave"; customer1.businessAddress.city = "Washington"; customer1.businessAddress.state = "DC"; customer1.businessAddress.zip = ; return 0; }
11
Pointer Use in C++. A pointer is a variable or constant that holds a memory address a) Hexadecimal numbers are used for representing memory locations 216793 iptr … i 216802
12
Classes The definition of an object is know as a class
It is similar to using basic data structures in C++ When you declare an object, you are said to have instantiated it (given it instances) Objects are members of a class Paul Millis, George Bush and George Washington being members of the human being class The design of a class is as important as its implementation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.