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

Slides:



Advertisements
Similar presentations
1 Techniques of Programming CSCI 131 Lecture 24 Structs.
Advertisements

1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
Dale/Weems/Headington
1 Lecture 24 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Lecture 25 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
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.
1 Structured Types, Data Abstraction and Classes.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Structured Data Types array array union union struct struct class class.
1 Chapter 10 Simple Data Types: Built-In and User- Defined.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Chapter 10: Records (structs)
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 10 Simple Data Types: Built-In and User- Defined.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
Chapter 10 Simple Data Types: Built-In and User-Defined.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 Data Structures and Algorithms Week 3 Data Abstraction: Part II Structured Types, and Classes.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Arrays in C++: Numeric Character (Part 2). Passing Arrays as Arguments in C++, arrays are always passed by reference (Pointer) whenever an array is passed.
1 Chapter 12-2 Arrays Dale/Weems. 2 Using Arrays as Arguments to Functions Generally, functions that work with arrays require 2 items of information n.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Structures (aka records, structs) Textbook Chapter 11 sections:
1 Structural Types, Data Abstractions and Classes.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Chapter 12 Arrays Dale/Weems. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
1 Chapter 10 & 11 enum & Structured Types Dale/Weems.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
1 Structured Types, Data Abstraction and Classes.
Chapter 10 Simple Data Types: Built-In and User-Defined
Chapter 12 Classes and Abstraction
LESSON 06.
chapter 11 Structured Types, Data Abstraction, and Classes
Dale/Weems/Headington
C++ Data Types and Data Abstractions
Chapter 10 Simple Data Types: Built-In and User-Defined
Chapter 7: Introduction to Classes and Objects
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
C++ Data Types and Data Abstractions
C++ Data Types Simple Structured Address Integral Floating
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Introduction to Structured Data Types and Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS148 Introduction to Programming II
Introduction to Classes and Objects
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Structures Structured Data types Data abstraction structs ---
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

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

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

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

4 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

5 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...

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

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

8 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 ; 8

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

10 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.

11 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.

12 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

13 Valid operations on a struct member depend only on its type thisAnimal.age = 18; thisAnimal.id = ; cin >> thisAnimal.weight; getline ( cin, thisAnimal.species ); thisAnimal.name = “giant panda”; thisAnimal.genus[ 0 ] = toupper (thisAnimal.genus[ 0 ] ) ; thisAnimal.age++;

14 Aggregate Operation l is an operation on a data structure as a whole, as opposed to an operation on an individual component of the data structure

15 Aggregate struct Operations l I/O, arithmetic, and comparisons of entire struct variables are NOT ALLOWED! l operations valid on an entire struct type variable: assignment to another struct variable of same type, pass to a function as argument (by value or by reference), return as value of a function

16 Examples of aggregate struct operations anotherAnimal = thisAnimal ; // assignment WriteOut(thisAnimal); // value parameter ChangeWeightAndAge(thisAnimal); // reference parameter thisAnimal = GetAnimalData( ); // return value of function NOW WE’LL WRITE THE 3 FUNCTIONS USED HERE...

17 void WriteOut( /* in */ AnimalType thisAnimal) // Prints out values of all members of thisAnimal // Precondition: all members of thisAnimal are assigned // Postcondition: all members have been written out { cout << “ID # “ << thisAnimal.id << thisAnimal.name << endl ; cout << thisAnimal.genus << thisAnimal.species << endl ; cout << thisAnimal.country << endl ; cout << thisAnimal.age << “ years “ << endl ; cout << thisAnimal.weight << “ lbs. “ << endl ; cout << “General health : “ ; WriteWord ( thisAnimal.health ) ; } 17

18 void ChangeAge ( /* inout */ AnimalType& thisAnimal ) // Adds 1 to age // Precondition: thisAnimal.age is assigned // Postcondition: thisAnimal.age == + 1 { thisAnimal.age++ ; } Passing a struct Type by Reference

19 AnimalType GetAnimalData ( void ) // Obtains all information about an animal from keyboard // Postcondition: // Function value == AnimalType members entered at kbd { AnimalType thisAnimal ; char response ; do {// have user enter all members until they are correct. } while (response != ‘Y’ ) ; return thisAnimal ; } 19

20 Hierarchical Structures The type of a struct member can be another struct type. This is called nested or hierarchical structures. Hierarchical structures are very useful when there is much detailed information in each record. FOR EXAMPLE...

21 struct MachineRec Information about each machine in a shop contains: an idNumber, a written description, the purchase date, the cost, and a history (including failure rate, number of days down, and date of last service).

22 struct DateType {int month ; // Assume int day ;// Assume int year ; // Assume }; struct StatisticsType {floatfailRate ; DateTypelastServiced ; // DateType is a struct type intdownDays ; } ; struct MachineRec {int idNumber ; string description ; StatisticsType history ; // StatisticsType is a struct type DateType purchaseDate ; float cost ; } ; MachineRec machine ; 22

23 struct type variable machine 7000.idNumber.description. history.purchaseDate.cost.month.day.year 5719 “DRILLING…” failrate.lastServiced.downdays month.day.year machine.history.lastServiced.year has value 1999

24 *

25 **

26 Unions in C++ DEFINITION A union is a struct that holds only one of its members at a time during program execution. EXAMPLE union WeightType { long wtInOunces ; int wtInPounds; only one at a time float wtInTons; } ;

27 Using Unions union WeightType// declares a union type { long wtInOunces ; int wtInPounds; float wtInTons; } ; WeightType weight;// declares a union variable weight.wtInTons = 4.83 ; // Weight in tons is no longer needed. Reuse the memory space. weight.wtInPounds = 35; 27