Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 24 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.

Similar presentations


Presentation on theme: "1 Lecture 24 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington."— Presentation transcript:

1

2 1 Lecture 24 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington

3 2 Chapter 11 Topics l Meaning of a Structured Data Type Declaring and Using a struct Data Type C++ union Data Type l Meaning of an Abstract Data Type Declaring and Using a class Data Type l Using Separate Specification and Implementation Files Invoking class Member Functions in Client Code C++ class Constructors

4 3 Revision l A 2-D array is declared row first and column later. It is stored in a row-major order l float average_temps[50][12]; l What dimension will be fixed and what will be varied if we wish to read average temperatures for all 12 months for a given state?

5 4 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long double

6 5 Structured Data Type A structured data type is a type in which each value is a collection of component items. l the entire collection has a single name l each component can be accessed individually

7 6 C++ Structured Type l often we have related information of various types that we’d like to store together for convenient access under the same identifier, for example...

8 7 Driving License Information on File l Driving License Number l Driver’s name l Driver’s address l Date of Birth l Date of Issue of License l Date the License expires l Issuing location

9 8 thisAnimal 5000.id 2037581.name “giant panda”.genus “Ailuropoda”.species “melanoluka”.country “China”.age 18.weight 234.6.health Good

10 9 anotherAnimal 6000.id 5281003.name “llama”.genus “Lama”.species “peruana”.country “Peru”.age 7.weight 278.5.health Excellent

11 10 struct AnimalType enum HealthType { Poor, Fair, Good, Excellent } ; struct AnimalType// declares a struct data type {// does not allocate memory long id ; string name ; string genus ; string species ; struct members string country ; int age ; float weight ; HealthType health ; } ; AnimalType thisAnimal ; // declare variables of AnimalType AnimalType anotherAnimal ;

12 11 Class Exercise l Develop a Struct definition for driver’s license information file and define a variable of this type. Then allocate the values to different fields within the struct variable

13 12 licenseInfo.id 5281003345.name “David Black”.addr “123 Anytown NY 14444”.dateofissue “10/29/00”.dateofexpiry “11/17/04”.dateofbirth “11/17/65”.location “Tonawanda”.restricted glasses

14 13 struct type Declaration SYNTAX struct TypeName // does not allocate memory { MemberList } ; MemberList SYNTAX DataType MemberName ;...

15 14 struct type Declaration The struct declaration names a type and names the members of the struct. It does not allocate memory for any variables of that type! You still need to declare your struct variables.

16 15 More about struct type declarations If the struct type declaration precedes all functions it will be visible throughout the rest of the file. If it is placed within a function, only that function can use it. It is common to place struct type declarations with TypeNames in a (.h) header file and #include that file. It is possible for members of different struct types to have the same identifiers. Also a non-struct variable may have the same identifier as a structure member.

17 16 Accessing struct Members Dot ( period ) is the member selection operator. After the struct type declaration, the various members can be used in your program only when they are preceded by a struct variable name and a dot. EXAMPLES thisAnimal.weight anotherAnimal.country licenseInfo.name


Download ppt "1 Lecture 24 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington."

Similar presentations


Ads by Google