Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 1 Overview – Chapter 10 -- Section 4 Arrays and Classes (10.4)

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
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?
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Structure.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 1- 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
Chapter 15 Geography, Climate, and Natural Resources.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 2 Application Layer. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 2-2.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 13 Income Inequality. Copyright © 2005 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 1 The Facts to Be Explained. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 1-2.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Chapter 18 Conclusion: Where We Stand.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Chapter 1 Computer Networks and the Internet. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 1-2.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 6 Wireless and Mobile Networks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2.
Chapter 3 Transport Layer. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 6 Human Capital. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2.
Chapter 8 The Role of Technology in Growth. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 7 Multimedia Networking. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2.
Chapter 16 Resources and the Environment at the Global Level.
Arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Coyright © 2008 Pearson Addison-Wesley. All rights reserved. Friends, Overloaded Operators, and Arrays in Classes.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 11 Friends, Overloaded Operators, and Arrays in Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Friends, Overloaded Operators, and Arrays in Classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 4 Applications of the Derivative.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1 Functions.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Array, Structure and Union
Chapter 6 Defining Classes. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Introduction Structures (6.1) Classes (6.2)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 10 Defining Classes.
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
1 CS161 Introduction to Computer Science Topic #15.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Pointers and Dynamic Arrays
Friends, Overloaded Operators, and Arrays in Classes
11.0 Class parameters, const.
Section 10.5 The Dot Product
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Presentation transcript:

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 1 Overview – Chapter Section 4 Arrays and Classes (10.4)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide Arrays and Classes Arrays can use structures or classes as their base types Example: struct WindInfo { double velocity; char direction; } WindInfo data_point[10];

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 3 Accessing Members When an array's base type is a structure or a class… Use the dot operator to access the members of an indexed variable Example: for (i = 0; i > data_point[i].velocity; … }

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 4 The Money class of Chapter 8 can be the base type for an array When an array of classes is declared The default constructor is called to initialize the indexed variables An array of class Money is demonstrated in Display (1-2) Display (1) Display (2) An Array of Money

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 5 Arrays as Structure Members A structure can contain an array as a member Example: struct Data { double time[10]; int distance; } Data my_best; my_best contains an array of type double

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 6 Accessing Array Elements To access the array elements within a structure Use the dot operator to identify the array within the structure Use the [ ]'s to identify the indexed variable desired Example: my_best.time[i] references the ith indexed variable of the variable time in the structure my_best

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 7 Arrays as Class Members Class TemperatureList includes an array The array, named list, contains temperatures Member variable size is the number of items stored class TemperatureList { public: TemperatureList( ); //Member functions private: double list [MAX_LIST_SIZE]; int size; }

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 8 Display Display Overview of TemperatureList To create an object of type TemperatureList: TemperatureList my_data; To add a temperature to the list: My_data.add_temperature(77); A check is made to see if the array is full << is overloaded so output of the list is cout << my_data;

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 9 Section 10.4 Conclusion Can you Declare an array as a member of a class? Declare an array of objects of a class? Write code to call a member function of an element in an array of objects of a class? Write code to access an element of an array of integers that is a member of a class?

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 10 20/ Give a type definition for a structure called Score that has two member variables called home_team and opponent. Both member variables are of type int. Declare an array called game that is an array with ten elements of type Score. The array game might be used to record the scores of each of ten games for a sports team.

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 11 Back Next Display (1/2)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 12 Back Next Display (2/2)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 13 Back Next Display (1/2)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 14 Back Next Display (2/2)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 15 Back Next Display 10.15

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 16 Back Next Display 10.16