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

Slides:



Advertisements
Similar presentations
Structures Spring 2013Programming and Data Structure1.
Advertisements

1 Techniques of Programming CSCI 131 Lecture 24 Structs.
Dale/Weems/Headington
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 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.
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 C++ Syntax and Semantics, and the Program Development Process.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
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 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.
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.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
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.
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.
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 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
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.
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.
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.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
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
Chapter 7: Introduction to Classes and Objects
C++ Data Types and Data Abstractions
C++ Data Types Simple Structured Address Integral Floating
Chapter 16-2 Linked 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
Structures Structured Data types Data abstraction structs ---
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

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

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 n The entire collection has a single name n Each component can be accessed individually n Used to bundle together related data of various types for convenient access under the same identifier For example...

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

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

7 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; }; // Declare variables of AnimalType AnimalType thisAnimal; AnimalType anotherAnimal; 7

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

9 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

10 More about struct type declarations Scope of a struct 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 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

11 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

12 Operations on struct Members The type of the member determines the allowable operations 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++;

13 Aggregate Operation An aggregation operation is an operation on a data structure as a whole, as opposed to an operation on an individual component of the data structure

14 Aggregate struct Operations l Operations valid on struct type variables are  Assignment to another struct variable of the same type  Pass as an argument (by value or by reference)  Return as value of a function l I/O, arithmetic, and comparisons of entire struct variables are NOT ALLOWED!

15 Aggregate struct Operations anotherAnimal = thisAnimal; // Assignment WriteOut(thisAnimal); // Value parameter ChangeWeightAndAge(thisAnimal); // Reference parameter thisAnimal = GetAnimalData(); // Function return value

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

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

19 The End of Chapter 11 – Part 1