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

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
1 Techniques of Programming CSCI 131 Lecture 24 Structs.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 Lecture 10 Chapter 7 Functions 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 Lecture 16 Chapter 6 Looping Dale/Weems/Headington.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
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 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
11 Introduction to Object Oriented Programming (Continued) Cats II.
1 Structured Types, Data Abstraction and Classes.
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,
CSCI-383 Object-Oriented Programming & Design Lecture 5.
Chapter 8 Functions. Chapter 8 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
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.
Chapter 9 Structured Data: Structs and ADTs (Data Base Programs with C++) Mr. Dave Clausen La Cañada High School.
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.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
1 Data Structures and Algorithms Week 3 Data Abstraction: Part II Structured Types, and Classes.
11 Introduction to Object Oriented Programming (Continued) Cats.
C++ Classes and Data Structures Jeffrey S. Childs
1 Chapter 7 Functions Dale/Weems/Headington. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task.
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.
Flow of Control and Program Style n Nested if statements n C++ struct n Program Style n Lab Exercise.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Structures (aka records, structs) Textbook Chapter 11 sections:
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
1 Structural Types, Data Abstractions and Classes.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.
1 Introduction to Object Oriented Programming Chapter 10.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
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.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
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
Dale/Weems/Headington
C++ Data Types and Data Abstractions
Chapter 10 Simple Data Types: Built-In and User-Defined
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
C++ Data Types and Data Abstractions
C++ Data Types Simple Structured Address Integral Floating
Structures.
Chapter 5 Function Basics
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Chapter 8 Functions.
Structures Structured Data types Data abstraction structs ---
Presentation transcript:

1 Lecture 25 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 Revising Lecture 24 l What are the structured data types in C++? l One data value==>simple data type l Multiple data values with a common name==>structured data type l How do we declare members of a structure?

4 Revising Lecture 24 l Can we start using the members once we have declared the structure? l Once we have defined a variable of the declared structure type, how can we access its members? l Are the following operations allowed on the struct variable current?

5 Revising Lecture 24 l struct time l { l int hours; l int minutes; l int seconds; l char ampm; l }; l time current; l cout<<current; l current = current*3;

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

7 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 ) ; }

8 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

9 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 ; }

10 Class Exercise l Use the time struct to declare a variable called current and then allocate the various fields by passing it to a function. That function should request the user to input the values

11 Solution l #include l using namespace std; l struct time l { l int hours; l int minutes; l int seconds; l char ampm; l };

12 Solution l void timetake(time&); l void main() l { l time now; l timetake(now); l cout<<now.hours<<endl; l }

13 Solution l void timetake(time& current) l { l cout<<"hours"; l cin>>current.hours; l }

14 Class Exercise l You have your personal library of 1000 books at home. You are asked to write a program to computerize the catalogue. You define an array of structures and input all the data. Now you are asked to implement a search function that searches for a book by its catalogue number (an exact match is required)

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

16 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).

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

18 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