Structured Data Types array array union union struct struct class class.

Slides:



Advertisements
Similar presentations
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Advertisements

1 Records C++ Structs Chapter 14 2 What to do with records?  Declaring records  Accessing records  Accessing the field of a record  What is a union?
Structure.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
C++ Classes & Data Abstraction
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
Computer Science 1620 Loops.
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.
Lecture 3 1. Structures 2. Abstract Data Types. STRUCTURES WHY ARE STRUCTURES NEEDED? If the predefined types are not adequate to model the object, create.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
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 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
 2003 Prentice Hall, Inc. All rights reserved Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
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.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
計算機程式語言 Lecture 13-1 國立臺灣大學生物機電系 13 Structures.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 Chapter 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.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
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.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Structures - Part II aggregate operations arrays of type struct nested structures compared to classes.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
1 CS161 Introduction to Computer Science Topic #15.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
11 Introduction to Object Oriented Programming (Continued) Cats.
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
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.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
A Sample Program #include using namespace std; int main(void) { cout
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
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.
Chapter 12 Classes and Abstraction
CO1401 Program Design and Implementation
Structures - Part II aggregate operations arrays of type struct
DATA HANDLING.
Introduction to Structured Data Types and Classes
Passing Structures Lesson xx
Classes and Data Abstraction
Structured Data Types array union struct class.
Lec17 Structs.
Structures Structured Data types Data abstraction structs ---
Presentation transcript:

Structured Data Types array array union union struct struct class class

Data Types a simple or atomic a structured * char, int, float, double array, union, struct, class

Structured Data Types array - homogeneous struct - heterogenous

Abstract Data Type ADT = a programmer defined defined data type whose properties (domain [values] and operations) are specified independently of any particular implementation. * It has a what and a how.

Data Storage character field field record record table table database database

Structured Data Types = an abstract data type with a fixed number of components that are accessed by name, not by index. struct = an abstract data type with a fixed number of components that are accessed by name, not by index.

Structure Declaration // customarily initial caps struct Date// customarily initial caps { // int month; // // data_type member_name int day; // data_type member_name // int year; // };

Structure Declaration struct TypeName { MemberList // DataType MemberName };};};}; }

structure declaration vs. object declaration (object variable) (object  variable)

Structure Declaration struct Date { int month; int day; int year; };

Object Instantiation int num; double x; Date myBirth; Date today, bill_Date, lily_Bday; data type variable name *

Assigning Values Date myBirth = {2, 29, 1963}; Date today = {4, 30, 2007}; Date bill_Date = {5, 4, 2007}; Date lily_Bday = {1, 20, 1985};

Assigning Values myBirth = {2, 29, 1963}; today = {4, 30, 2007}; bill_Date = {5, 4, 2007}; lily_Bday = {1, 20, 1985};

Assigning Values myBirth. month = 2; myBirth. day = 29; myBirth. year = 1963; bill_Date.month = 5; bill_Date.day = 4; lily_Bday.year = 1985; today.month = 4; * instance of Date member of myBirth instance

elementat_numat_massdensity boron (B) tungsten (W) iodine (I) In Class Assignment 1 1.declare a structure for this table. 2. instantiate variables of the structure type 3.initialize the data for each element (use two different methods)

How to declare and instantiate #include using namespace std; struct Element// the declaration of the struct { char symbol;// note different data types int at_num;// of members double at_mass; double density; }; main (){ Element boron;//an instantiation boron.symbol = 'B';// initialize each member boron.at_num = 5; boron.at_mass = ; boron.density = 2.34; // instantiate and initialize all at once Element tungsten = {'W', 74, , 19.30}; Element iodine = {'I', 53, 126.9, 4.94}; cout << "Atomic mass of tungsten is " // print one piece << tungsten.at_mass << '\n';}

Assigning Values // assign to a variable year = lily_Bday.year new_mo = lily_Bday.month + 1 // assign contents of a variable to it lily_Bday.month = someMonth;

Assigning Values Date today, bill_date; cout << “Enter month, day and year: “; cin >> today.month >>today.day >> today.year; // an aggregate action bill_date = today;// an aggregate action *

Structures as Arguments // in a prototype int Overdue(Date, int); * * * * // in a function call cout << Overdue(today, bill_Date.year); // in a function header int Overdue(Date now, int purchased)

In Class Assignment 2 Elementat. num.at. massdensity hydrogen (H) fluorine (F) Write the code for user input for H and F. 5.Write the code to display data. 6.Put each of these codes into separate functions.

In Class Assignment 2 7.Create a program that uses these functions. a) What needs to be done?  declare (define) the structure  declare the prototypes  in main(): declare the objects  call the functions 8. As a formatting exercise, display the data neatly in a table. a) What needs to be done? * * * * a) What needs to be done? 7.Create a program that uses these functions. a) What needs to be done?

In Class Assign. 2-ans // display data - formatted void showData(Element any_at) { cout << setiosflags(ios::fixed); cout << '\n' << setw(12) << any_at.symbol << setprecision(0) << setw(5) << any_at.at_num << setprecision(3) << setw(10) << any_at.at_mass << setw(10) << any_at.density; cout << endl; } #7 b

In Class Assign. 2-ans #include Using namespace std; struct Element { char symbol ; int at_num; double at_mass; double density; }; void getData(Element&); void showData(Element); void main() { Element hydrogen, fluorine; getData(hydrogen); getData(fluorine); showData(hydrogen); showData(fluorine); cout << endl<<endl; // end main() } // end main()