Chapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Similar documents
Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 10 Defining Classes

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 8. Strings and Vectors. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Python Programming: An Introduction to Computer Science

Chapter 5. Functions for All Subtasks. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 8. Strings and Vectors. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

CS 11 C track: lecture 1

Structures and Classes CS 16: Solving Problems with Computers I Lecture #15

Classes and Objects. Again: Distance between points within the first quadrant. José Valente de Oliveira 4-1

. Written in factored form it is easy to see that the roots are 2, 2, i,

From last week. Lecture 5. Outline. Principles of programming languages

Java Inheritance. Class ADT (Abstract Data Type) Interface. Classes implement the concept of ADT: Interfaces define interaction contracts: Rui Moreira

Computers and Scientific Thinking

n We have discussed classes in previous lectures n Here, we discuss design of classes n Library design considerations

Python Programming: An Introduction to Computer Science

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

CMPT 125 Assignment 2 Solutions

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs

The Magma Database file formats

Chapter 3. More Flow of Control. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Elementary Educational Computer

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Last class. n Scheme. n Equality testing. n eq? vs. equal? n Higher-order functions. n map, foldr, foldl. n Tail recursion

Parabolic Path to a Best Best-Fit Line:

Threads and Concurrency in Java: Part 1

Threads and Concurrency in Java: Part 1

Overview. Chapter 18 Vectors and Arrays. Reminder. vector. Bjarne Stroustrup

9.1. Sequences and Series. Sequences. What you should learn. Why you should learn it. Definition of Sequence

CS : Programming for Non-Majors, Summer 2007 Programming Project #3: Two Little Calculations Due by 12:00pm (noon) Wednesday June

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

Topics. Instance object. Instance object. Fundamentals of OT. Object notation. How do objects collaborate? Pearson Education 2007 Appendix (RASD 3/e)

Schema for the DCE Security Registry Server

Lecturers: Sanjam Garg and Prasad Raghavendra Feb 21, Midterm 1 Solutions

Last Class. Announcements. Lecture Outline. Types. Structural Equivalence. Type Equivalence. Read: Scott, Chapters 7 and 8. T2 y; x = y; n Types

Lecture 1: Introduction and Strassen s Algorithm

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

10/23/18. File class in Java. Scanner reminder. Files. Opening a file for reading. Scanner reminder. File Input and Output

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Ones Assignment Method for Solving Traveling Salesman Problem

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Analysis of Algorithms

COSC 1P03. Ch 7 Recursion. Introduction to Data Structures 8.1

top() Applications of Stacks

n The C++ template facility provides the ability to define n A generic facility allows code to be written once then

BEA WebLogic Process Integrator

n Some thoughts on software development n The idea of a calculator n Using a grammar n Expression evaluation n Program organization n Analysis

Code Review Defects. Authors: Mika V. Mäntylä and Casper Lassenius Original version: 4 Sep, 2007 Made available online: 24 April, 2013

6.854J / J Advanced Algorithms Fall 2008

% Sun Logo for. X3T10/95-229, Revision 0. April 18, 1998

Data Structures and Algorithms. Analysis of Algorithms

Exceptions. Your computer takes exception. The Exception Class. Causes of Exceptions

Baan Finance Financial Statements

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

Abstract Syntax Trees. AST Data Structure. Visitor Interface. Accept methods. Visitor Methodology for AST Traversal CS412/CS413

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

1.2 Binomial Coefficients and Subsets

Analysis of Algorithms

One advantage that SONAR has over any other music-sequencing product I ve worked

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

APPLICATION NOTE PACE1750AE BUILT-IN FUNCTIONS

! Given the following Structure: ! We can define a pointer to a structure. ! Now studentptr points to the s1 structure.

% Sun Logo for Frame. X3T10/95-229, Revision 2. September 28, 1995

MOTIF XF Extension Owner s Manual

Term Project Report. This component works to detect gesture from the patient as a sign of emergency message and send it to the emergency manager.

CS211 Fall 2003 Prelim 2 Solutions and Grading Guide

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs

Evaluation scheme for Tracking in AMI

The Implementation of Data Structures in Version 5 of Icon* Ralph E. Gr is wo Id TR 85-8

Recursion. Recursion. Mathematical induction: example. Recursion. The sum of the first n odd numbers is n 2 : Informal proof: Principle:

Linked Lists 11/16/18. Preliminaries. Java References. Objects and references. Self references. Linking self-referential nodes

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms.

Ch 9.3 Geometric Sequences and Series Lessons

EE 459/500 HDL Based Digital Design with Programmable Logic. Lecture 13 Control and Sequencing: Hardwired and Microprogrammed Control

Java Expressions & Flow Control

MR-2010I %MktBSize Macro 989. %MktBSize Macro

Behavioral Modeling in Verilog

CIS 121. Introduction to Trees

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

Appendix D. Controller Implementation

Bezier curves. Figure 2 shows cubic Bezier curves for various control points. In a Bezier curve, only

Abstract. Chapter 4 Computation. Overview 8/13/18. Bjarne Stroustrup Note:

Analysis of Algorithms

Global Support Guide. Verizon WIreless. For the BlackBerry 8830 World Edition Smartphone and the Motorola Z6c

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

Introduction to GAMIT/GLOBK Applications of GLOBK. Lecture 11 OVERVIEW

A SOFTWARE MODEL FOR THE MULTILAYER PERCEPTRON

Sharing Collections. Share a Collection via . Share a Collection via Google Classroom. Quick Reference Guide

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames

Floristic Quality Assessment (FQA) Calculator for Colorado User s Guide

CS 111: Program Design I Lecture # 7: First Loop, Web Crawler, Functions

Chapter 24. Sorting. Objectives. 1. To study and analyze time efficiency of various sorting algorithms

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

Modern Systems Analysis and Design Seventh Edition

EE123 Digital Signal Processing

Getting Started. Getting Started - 1

Transcription:

Chapter 10 Defiig Classes Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.

Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types 10.4 Itroductio to Iheritace Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-3

10.1 Structures Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.

What Is a Class? A class is a data type whose variables are objects Some pre-defied data types you have used are it char A pre-defied class you have used is ifstream You ca defie your ow classes as well Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-5

Class Defiitios A class defiitio icludes A descriptio of the kids of values the variable ca hold A descriptio of the member fuctios We will start by defiig structures as a first step toward defiig classes Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-6

Structures A structure ca be viewed as a object Cotais o member fuctios (The structures used here have o member fuctios) Cotais multiple values of possibly differet types The multiple values are logically related as a sigle item Example: A bak Certificate of Deposit (CD) has the followig values: a balace a iterest rate a term (moths to maturity) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-7

The CD Defiitio The Certificate of Deposit structure ca be defied as struct CDAccout { double balace; double iterest_rate; it term; //moths to maturity }; Keyword struct begis a structure defiitio Remember this semicolo! CDAccout is the structure tag or the structure s type Member ames are idetifiers declared i the braces Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-8

Usig the Structure Structure defiitio is geerally placed outside ay fuctio defiitio This makes the structure type available to all code that follows the structure defiitio To declare two variables of type CDAccout: CDAccout my_accout, your_accout; My_accout ad your_accout cotai distict member variables balace, iterest_rate, ad term Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-9

The Structure Value The Structure Value Cosists of the values of the member variables The value of a object of type CDAccout Cosists of the values of the member variables balace iterest_rate term Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-10

Specifyig Member Variables Member variables are specific to the structure variable i which they are declared Sytax to specify a member variable: Structure_Variable_Name. Member_Variable_Name Give the declaratio: CDAccout my_accout, your_accout; Use the dot operator to specify a member variable my_accout.balace my_accout.iterest_rate my_accout.term Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-11

Usig Member Variables Member variables ca be used just as ay other variable of the same type my_accout.balace = 1000; your_accout.balace = 2500; Display 10.1 (1) Display 10.1 (2) Notice that my_accout.balace ad your_accout.balace are differet variables! my_accout.balace = my_accout.balace + iterest; Display 10.2 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-12

Duplicate Names Member variable ames duplicated betwee structure types are ot a problem. struct FertilizerStock { double quatity; double itroge_cotet; }; struct CropYield { it quatity; double size; }; FertilizerStock super_grow; CropYield apples; super_grow.quatity ad apples.quatity are differet variables stored i differet locatios Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-13

Structures as Argumets Structures ca be argumets i fuctio calls The formal parameter ca be call-by-value The formal parameter ca be call-by-referece Example: void get_data(cdaccout& the_accout); Uses the structure type CDAccout we saw earlier as the type for a call-by-referece parameter Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-14

Structures as Retur Types Structures ca be the type of a value retured by a fuctio Example: CDAccout shrik_wrap(double the_balace, double the_rate, it the_term) { CDAccout temp; temp.balace = the_balace; temp.iterest_rate = the_rate; temp.term = the_term; retur temp; } Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-15

Usig Fuctio shrik_wrap shrik_wrap builds a complete structure value i temp, which is retured by the fuctio We ca use shrik_wrap to give a variable of type CDAccout a value i this way: CDAccout ew_accout; ew_accout = shrik_wrap(1000.00, 5.1, 11); Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-16

Assigmet ad Structures The assigmet operator ca be used to assig values to structure types Usig the CDAccout structure agai: CDAccout my_accout, your_accout; my_accout.balace = 1000.00; my_accout.iterest_rate = 5.1; my_accout.term = 12; your_accout = my_accout; Assigs all member variables i your_accout the correspodig values i my_accout Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-17

Hierarchical Structures Structures ca cotai member variables that are also structures struct Date { it moth; it day; it year; }; struct PersoIfo { double height; it weight; Date birthday; }; struct PersoIfo cotais a Date structure Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-18

Usig PersoIfo A variable of type PersoIfo is declared by PersoIfo perso1; To display the birth year of perso1, first access the birthday member of perso1 cout << perso1.birthday But we wat the year, so we ow specify the year member of the birthday member cout << perso1.birthday.year; Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-19

Iitializig Classes A structure ca be iitialized whe declared Example: struct Date { it moth; it day; it year; }; Ca be iitialized i this way Date due_date = {12, 31, 2004}; Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-20

Sectio 10.1 Coclusio Ca you Write a defiitio for a structure type for records cosistig of a perso s wage rate, accrued vacatio (i whole days), ad status (hourly or salaried). Represet the status as oe of the two character values H ad S. Call the type EmployeeRecord. Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-21

10.2 Classes Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.

Classes A class is a data type whose variables are objects The defiitio of a class icludes Descriptio of the kids of values of the member variables Descriptio of the member fuctios A class descriptio is somewhat like a structure defiitio plus the member variables Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-23

A Class Example To create a ew type amed DayOfYear as a class defiitio Decide o the values to represet This example s values are dates such as July 4 usig a iteger for the umber of the moth Member variable moth is a it (Ja = 1, Feb = 2, etc.) Member variable day is a it Decide o the member fuctios eeded We use just oe member fuctio amed output Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-24

Class DayOfYear Defiitio class DayOfYear { public: void output( ); it moth; it day; }; Member Fuctio Declaratio Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-25

Defiig a Member Fuctio Member fuctios are declared i the class declaratio Member fuctio defiitios idetify the class i which the fuctio is a member void DayOfYear::output() { cout << moth = << moth <<, day = << day << edl; } Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-26

Member Fuctio Defiitio Member fuctio defiitio sytax: Retured_Type Class_Name::Fuctio_Name(Parameter_List) { Fuctio Body Statemets } Example: void DayOfYear::output( ) { cout << moth = << moth <<, day = << day << edl; } Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-27

The :: Operator :: is the scope resolutio operator Tells the class a member fuctio is a member of void DayOfYear::output( ) idicates that fuctio output is a member of the DayOfYear class The class ame that precedes :: is a type qualifier Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-28

:: ad. :: used with classes to idetify a member void DayOfYear::output( ) { // fuctio body }. used with variables to idetify a member DayOfYear birthday; birthday.output( ); Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-29

Callig Member Fuctios Callig the DayOfYear member fuctio output is doe i this way: DayOfYear today, birthday; today.output( ); birthday.output( ); Note that today ad birthday have their ow versios of the moth ad day variables for use by the output fuctio Display 10.3 (1) Display 10.3 (2) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-30

Ecapsulatio Ecapsulatio is Combiig a umber of items, such as variables ad fuctios, ito a sigle package such as a object of a class Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-31

Problems With DayOfYear Chagig how the moth is stored i the class DayOfYear requires chages to the program If we decide to store the moth as three characters (JAN, FEB, etc.) istead of a it ci >> today.moth will o loger work because we ow have three character variables to read if(today.moth == birthday.moth) will o loger work to compare moths The member fuctio output o loger works Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-32

Ideal Class Defiitios Chagig the implemetatio of DayOfYear requires chages to the program that uses DayOfYear A ideal class defiitio of DayOfYear could be chaged without requirig chages to the program that uses DayOfYear Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-33

Fixig DayOfYear To fix DayOfYear We eed to add member fuctios to use whe chagig or accessig the member variables If the program ever directly refereces the member variables, chagig how the variables are stored will ot require chagig the program We eed to be sure that the program does ot ever directly referece the member variables Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-34

Public Or Private? C++ helps us restrict the program from directly referecig member variables private members of a class ca oly be refereced withi the defiitios of member fuctios If the program tries to access a private member, the compiler gives a error message Private members ca be variables or fuctios Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-35

Private Variables Private variables caot be accessed directly by the program Chagig their values requires the use of public member fuctios of the class To set the private moth ad day variables i a ew DayOfYear class use a member fuctio such as void DayOfYear::set(it ew_moth, it ew_day) { moth = ew_moth; day = ew_day; } Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-36

Public or Private Members The keyword private idetifies the members of a class that ca be accessed oly by member fuctios of the class Members that follow the keyword private are private members of the class The keyword public idetifies the members of a class that ca be accessed from outside the class Members that follow the keyword public are public members of the class Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-37

A New DayOfYear The ew DayOfYear class demostrated i Display 10.4 Uses all private member variables Uses member fuctios to do all maipulatio of the private member variables Member variables ad member fuctio defiitios ca be chaged without chages to the program that uses DayOfYear Display 10.4 (1) Display 10.4 (2) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-38

Usig Private Variables It is ormal to make all member variables private Private variables require member fuctios to perform all chagig ad retrievig of values Accessor fuctios allow you to obtai the values of member variables Example: get_day i class DayOfYear Mutator fuctios allow you to chage the values of member variables Example: set i class DayOfYear Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-39

Geeral Class Defiitios The sytax for a class defiitio is class Class_Name { public: Member_Specificatio_1 Member_Specificatio_2 Member_Specificatio_3 private: Member_Specificatio_+1 Member_Specificatio_+2 }; Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-40

Declarig a Object Oce a class is defied, a object of the class is declared just as variables of ay other type Example: To create two objects of type Bicycle: class Bicycle { // class defiitio lies }; Bicycle my_bike, your_bike; Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-41

The Assigmet Operator Objects ad structures ca be assiged values with the assigmet operator (=) Example: DayOfYear due_date, tomorrow; tomorrow.set(11, 19); due_date = tomorrow; Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-42

Program Example: BakAccout Class This bak accout class allows Withdrawal of moey at ay time All operatios ormally expected of a bak accout (implemeted with member fuctios) Storig a accout balace Storig the accout s iterest rate Display 10.5 ( 1) Display 10.5 ( 2) Display 10.5 ( 3) Display 10.5 ( 4) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-43

Callig Public Members Recall that if callig a member fuctio from the mai fuctio of a program, you must iclude the the object ame: accout1.update( ); Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-44

Callig Private Members Whe a member fuctio calls a private member fuctio, a object ame is ot used fractio (double percet); is a private member of the BakAccout class fractio is called by member fuctio update void BakAccout::update( ) { balace = balace + fractio(iterest_rate)* balace; } Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-45

Costructors A costructor ca be used to iitialize member variables whe a object is declared A costructor is a member fuctio that is usually public A costructor is automatically called whe a object of the class is declared A costructor s ame must be the ame of the class A costructor caot retur a value No retur type, ot eve void, is used i declarig or defiig a costructor Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-46

Costructor Declaratio A costructor for the BakAccout class could be declared as: class BakAccout { public: BakAccout(it dollars, it cets, double rate); //iitializes the balace to $dollars.cets //iitializes the iterest rate to rate percet }; //The rest of the BakAccout defiitio Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-47

Costructor Defiitio The costructor for the BakAccout class could be defied as BakAccout::BakAccout(it dollars, it cets, double rate) { if ((dollars < 0) (cets < 0) ( rate < 0 )) { cout << Illegal values for moey or rate\ ; exit(1); } balace = dollars + 0.01 * cets; iterest_rate = rate; } Note that the class ame ad fuctio ame are the same Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-48

Callig A Costructor (1) A costructor is ot called like a ormal member fuctio: BakAccout accout1; accout1.bakaccout(10, 50, 2.0); Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-49

Callig A Costructor (2) A costructor is called i the object declaratio BakAccout accout1(10, 50, 2.0); Creates a BakAccout object ad calls the costructor to iitialize the member variables Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-50

Overloadig Costructors Costructors ca be overloaded by defiig costructors with differet parameter lists Other possible costructors for the BakAccout class might be BakAccout (double balace, double iterest_rate); BakAccout (double balace); BakAccout (double iterest_rate); BakAccout ( ); Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-51

The Default Costructor A default costructor uses o parameters A default costructor for the BakAccout class could be declared i this way class BakAccout { public: BakAccout( ); // iitializes balace to $0.00 // iitializes rate to 0.0% // The rest of the class defiitio }; Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-52

Default Costructor Defiitio The default costructor for the BakAccout class could be defied as BakAccout::BakAccout( ) { balace = 0; rate = 0.0; } It is a good idea to always iclude a default costructor eve if you do ot wat to iitialize variables Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-53

Callig the Default Costructor The default costructor is called durig declaratio of a object A argumet list is ot used BakAccout accout1; // uses the default BakAccout costructor BakAccout accout1( ); // Is ot legal Display 10.6 (1) Display 10.6 (2) Display 10.6 (3) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-54

Iitializatio Sectios A iitializatio sectio i a fuctio defiitio provides a alterative way to iitialize member variables BakAccout::BakAccout( ): balace(0), iterest_rate(0.0); { // No code eeded i this example } The values i parethesis are the iitial values for the member variables listed Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-55

Parameters ad Iitializatio Member fuctios with parameters ca use iitializatio sectios BakAccout::BakAccout(it dollars, it cets, double rate) : balace (dollars + 0.01 * cets), iterest_rate(rate) { if (( dollars < 0) (cets < 0) (rate < 0)) { cout << Illegal values for moey or rate\ ; exit(1); } } Notice that the parameters ca be argumets i the iitializatio Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-56

Member Iitializers C++11 supports a feature called member iitializatio Simply set member variables i the class Ex: class Coordiate { }; private:... it x=1; it y=2; Creatig a Coordiate object will iitialize its x variable to 1 ad y to 2 (assumig a costructor is t called that sets the values to somethig else) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 1-57

Costructor Delegatio C++11 also supports costructor delegatio. This lets you have a costructor ivoke aother costructor i the iitializatio sectio. For example, make the default costructor call a secod costructor that sets X to 99 ad Y to 99: Coordiate::Coordiate() : Coordiate(99,99) { } Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 1-58

Sectio 10.2 Coclusio Ca you Describe the differece betwee a class ad a structure? Explai why member variables are usually private? Describe the purpose of a costructor? Use a iitializatio sectio i a fuctio defiitio? Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-59

10.3 Abstract Data Types Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.

Abstract Data Types A data type cosists of a collectio of values together with a set of basic operatios defied o the values A data type is a Abstract Data Type (ADT) if programmers usig the type do ot have access to the details of how the values ad operatios are implemeted Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-61

Classes To Produce ADTs To defie a class so it is a ADT Separate the specificatio of how the type is used by a programmer from the details of how the type is implemeted Make all member variables private members Basic operatios a programmer eeds should be public member fuctios Fully specify how to use each public fuctio Helper fuctios should be private members Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-62

ADT Iterface The ADT iterface tells how to use the ADT i a program The iterface cosists of The public member fuctios The commets that explai how to use the fuctios The iterface should be all that is eeded to kow how to use the ADT i a program Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-63

ADT Implemetatio The ADT implemetatio tells how the iterface is realized i C++ The implemetatio cosists of The private members of the class The defiitios of public ad private member fuctios The implemetatio is eeded to ru a program The implemetatio is ot eeded to write the mai part of a program or ay o-member fuctios Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-64

ADT Beefits Chagig a ADT implemetatio does require chagig a program that uses the ADT ADT s make it easier to divide work amog differet programmers Oe or more ca write the ADT Oe or more ca write code that uses the ADT Writig ad usig ADTs breaks the larger programmig task ito smaller tasks Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-65

Program Example The BakAccout ADT I this versio of the BakAccout ADT Data is stored as three member variables The dollars part of the accout balace The cets part of the accout balace The iterest rate This versio stores the iterest rate as a fractio The public portio of the class defiitio remais uchaged from the versio of Display 10.6 Display 10.7 (1) Display 10.7 (2) Display 10.7 (3) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-66

Iterface Preservatio To preserve the iterface of a ADT so that programs usig it do ot eed to be chaged Public member declaratios caot be chaged Public member defiitios ca be chaged Private member fuctios ca be added, deleted, or chaged Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-67

Iformatio Hidig Iformatio hidig was refered to earlier as writig fuctios so they ca be used like black boxes ADT s implemet iformatio hidig because The iterface is all that is eeded to use the ADT Implemetatio details of the ADT are ot eeded to kow how to use the ADT Implemetatio details of the data values are ot eeded to kow how to use the ADT Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-68

Sectio 10.3 Coclusio Ca you Describe a ADT? Describe how to implemet a ADT i C++? Defie the iterface of a ADT? Defie the implemetatio of a ADT? Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-69

10.4 Itroductio to Iheritace Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.

Iheritace Iheritace refers to derived classes Derived classes are obtaied from aother class by addig features A derived class iherits the member fuctios ad variables from its paret class without havig to rewrite them Example I Chapter 6 we saw that the class of iput-file streams is derived from the class of all iput streams by addig member fuctios such as ope ad close ci belogs to the class of all iput streams, but ot the class of iput-file streams Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-71

Iheritace Example Natural hierarchy of bak accouts Most geeral: A Bak Accout stores a balace A Checkig Accout IS A Bak Accout that allows customers to write checks A Savigs Accout IS A Bak Accout without checks but higher iterest Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Accouts are more specific as we go dow the hierarchy Each box ca be a class Slide 10-72

Iheritace Relatioships The more specific class is a derived or child class The more geeral class is the base, super, or paret class If class B is derived from class A Class B is a derived class of class A Class B is a child of class A Class A is the paret of class B Class B iherits the member fuctios ad variables of class A Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-73

Defiig Derived Classes Give the class ame as ormal, but add a colo ad the the ame of the base class class SavigsAccout : public BakAccout { } Objects of type SavigsAccout ca access member fuctios defied i SavigsAccout or BakAccout Display 10.9 (1-3) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 1-74

Sectio 10.4 Coclusio Ca you Defie object? Defie class? Describe the relatioship betwee paret ad child classes? Describe the beefit of iheritace? Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-75

Chapter 10 -- Ed Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-76

Display 10.1 (1/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-77

Display 10.1 (2/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-78

Display 10.2 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-79

Display 10.3 (1/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-80

Display 10.3 (2/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-81

Display 10.4 (1/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-82

Display 10.4 (2/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-83

Display 10.5 (1/4) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-84

Display 10.5 (2/4) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-85

Display 10.5 (3/4) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-86

Display 10.5 (4/4) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-87

Display 10.6 (1/3) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-88

Display 10.6 (2/3) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-89

Display 10.6 (3/3) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-90

Display 10.7 (1/3) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-91

Display 10.7 (2/3) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-92

Display 10.7 (3/3) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-93

Display 10.8 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-94

Display 10.9 (1/3) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-95

Display 10.9 (2/3) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-96

Display 10.9 (3/3) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 10-97