Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

1
Final and Abstract Classes
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Chapter 8 Operator Overloading, Friends, and References.
Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 3 CPUs.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
Custom Statutory Programs Chapter 3. Customary Statutory Programs and Titles 3-2 Objectives Add Local Statutory Programs Create Customer Application For.
Programming Language Concepts
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Turing Machines.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
PP Test Review Sections 6-1 to 6-6
Chapter 17 Linked Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Lists Chapter 6 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Chapter 24 Lists, Stacks, and Queues
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Data Structures Using C++
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Understanding class definitions Looking inside classes 5.0.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Analyzing Genes and Genomes
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
Pointers and Arrays Chapter 12
Essential Cell Biology
Chapter 12 OOP: Creating Object- Oriented Programs Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Energy Generation in Mitochondria and Chlorplasts
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Learning Objectives Classes Constructors Principles of OOP
Constructors and Other Tools
Presentation transcript:

Chapter 7 Constructors and Other Tools

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions Calling More Tools const parameter modifier Inline functions Static member data Vectors Introduction to vector class

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-3 Constructors Initialization of objects Initialize some or all member variables Other actions possible as well A special kind of member function Automatically called when object declared Very useful tool Key principle of OOP

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-4 Constructor Definitions Constructors defined like any member function Except: 1. Must have same name as class 2. Cannot return a value; not even void!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-5 Constructor Definition Example Class definition with constructor: class DayOfYear { public: DayOfYear(int monthValue, int dayValue); //Constructor initializes month & day void input(); void output(); … private: int month; int day; }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-6 Constructor Notes Notice name of constructor: DayOfYear Same name as class itself! Constructor declaration has no return-type Not even void! Constructor in public section Its called when objects are declared If private, could never declare objects!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-7 Calling Constructors Declare objects: DayOfYear date1(7, 4), date2(5, 5); Objects are created here Constructor is called Values in parens passed as arguments to constructor Member variables month, day initialized: date1.month 7 date2.month 5 date1.dat 4 date2.day 5

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-8 Constructor Equivalency Consider: DayOfYear date1, date2 date1.DayOfYear(7, 4);// ILLEGAL! date2.DayOfYear(5, 5);// ILLEGAL! Seemingly OK… CANNOT call constructors like other member functions!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-9 Constructor Code Constructor definition is like all other member functions: DayOfYear::DayOfYear(int monthValue, int dayValue) { month = monthValue; day = dayValue; } Note same name around :: Clearly identifies a constructor Note no return type Just as in class definition

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Alternative Definition Previous definition equivalent to: DayOfYear::DayOfYear(int monthValue, int dayValue) : month(monthValue), day(dayValue) {…} Third line called "Initialization Section" Body left empty Preferable definition version

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Constructor Additional Purpose Not just initialize data Body doesnt have to be empty In initializer version Validate the data! Ensure only appropriate data is assigned to class private member variables Powerful OOP principle

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Overloaded Constructors Can overload constructors just like other functions Recall: a signature consists of: Name of function Parameter list Provide constructors for all possible argument-lists Particularly "how many"

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Class with Constructors Example: Display 7.1 Class with Constructors (1 of 3)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Class with Constructors Example: Display 7.1 Class with Constructors (2 of 3)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Class with Constructors Example: Display 7.1 Class with Constructors (3 of 3)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Constructor with No Arguments Can be confusing Standard functions with no arguments: Called with syntax: callMyFunction(); Including empty parentheses Object declarations with no "initializers": DayOfYear date1;// This way! DayOfYear date(); // NO! What is this really? Compiler sees a function declaration/prototype! Yes! Look closely!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Explicit Constructor Calls Can also call constructor AGAIN After object declared Recall: constructor was automatically called then Can call via objects name; standard member function call Convenient method of setting member variables Method quite different from standard member function call

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Explicit Constructor Call Example Such a call returns "anonymous object" Which can then be assigned In Action: DayOfYear holiday(7, 4); Constructor called at objects declaration Now to "re-initialize": holiday = DayOfYear(5, 5); Explicit constructor call Returns new "anonymous object" Assigned back to current object

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Default Constructor Defined as: constructor w/ no arguments One should always be defined Auto-Generated? Yes & No If no constructors AT ALL are defined Yes If any constructors are defined No If no default constructor: Cannot declare: MyClass myObject; With no initializers

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Class Type Member Variables Class member variables can be any type Including objects of other classes! Type of class relationship Powerful OOP principle Need special notation for constructors So they can call "back" to member objects constructor

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Class Member Variable Example: Display 7.3 A Class Member Variable (1 of 5)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Class Member Variable Example: Display 7.3 A Class Member Variable (2 of 5)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Class Member Variable Example: Display 7.3 A Class Member Variable (3 of 5)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Class Member Variable Example: Display 7.3 A Class Member Variable (4 of 5)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Class Member Variable Example: Display 7.3 A Class Member Variable (5 of 5)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Parameter Passing Methods Efficiency of parameter passing Call-by-value Requires copy be made Overhead Call-by-reference Placeholder for actual argument Most efficient method Negligible difference for simple types For class types clear advantage Call-by-reference desirable Especially for "large" data, like class types

Copyright © 2006 Pearson Addison-Wesley. All rights reserved The const Parameter Modifier Large data types (typically classes) Desirable to use pass-by-reference Even if function will not make modifications Protect argument Use constant parameter Also called constant call-by-reference parameter Place keyword const before type Makes parameter "read-only" Attempts to modify result in compiler error

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Use of const All-or-nothing If no need for function modifications Protect parameter with const Protect ALL such parameters This includes class member function parameters

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Inline Functions For non-member functions: Use keyword inline in function declaration and function heading For class member functions: Place implementation (code) for function IN class definition automatically inline Use for very short functions only Code actually inserted in place of call Eliminates overhead More efficient, but only when short!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Inline Member Functions Member function definitions Typically defined separately, in different file Can be defined IN class definition Makes function "in-line" Again: use for very short functions only More efficient If too long actually less efficient!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Static Members Static member variables All objects of class "share" one copy One object changes it all see change Useful for "tracking" How often a member function is called How many objects exist at given time Place keyword static before type

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Static Functions Member functions can be static If no access to object data needed And still "must" be member of the class Make it a static function Can then be called outside class From non-class objects: E.g., Server::getTurn(); As well as via class objects Standard method: myObject.getTurn(); Can only use static data, functions!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Static Members Example: Display 7.6 Static Members (1 of 4)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Static Members Example: Display 7.6 Static Members (2 of 4)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Static Members Example: Display 7.6 Static Members (3 of 4)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Static Members Example: Display 7.6 Static Members (4 of 4)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Vectors Vector Introduction Recall: arrays are fixed size Vectors: "arrays that grow and shrink" During program execution Formed from Standard Template Library (STL) Using template class

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Vector Basics Similar to array: Has base type Stores collection of base type values Declared differently: Syntax: vector Indicates template class Any type can be "plugged in" to Base_Type Produces "new" class for vectors with that type Example declaration: vector v;

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Vector Use vector v; "v is vector of type int" Calls class default constructor Empty vector object created Indexed like arrays for access But to add elements: Must call member function push_back Member function size() Returns current number of elements

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Vector Example: Display 7.7 Using a Vector (1 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Vector Example: Display 7.7 Using a Vector (2 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Vector Efficiency Member function capacity() Returns memory currently allocated Not same as size() Capacity typically > size Automatically increased as needed If efficiency critical: Can set behaviors manually v.reserve(32); //sets capacity to 32 v.reserve(v.size()+10); //sets capacity to 10 more than size

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Summary 1 Constructors: automatic initialization of class data Called when objects are declared Constructor has same name as class Default constructor has no parameters Should always be defined Class member variables Can be objects of other classes Require initialization-section

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Summary 2 Constant call-by-reference parameters More efficient than call-by-value Can inline very short function definitions Can improve efficiency Static member variables Shared by all objects of a class Vector classes Like: "arrays that grow and shrink"