1 By Joaquin Vila Preparedbv Sally Scott ACS 168 Problem Solving Using the Computer Chapter 6 Classes Chapter 6 Classes.

Slides:



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

Copyright © 2003 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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
Road Map Introduction to object oriented programming. Classes
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Defining Classes.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 11: Classes and Data Abstraction
Remarks  Can only manually release memory blocks allocated by new operator delete [] ptr; delete ptr;  Dynamically allocated variables and arrays will.
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.
1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 6 Defining Classes and Abstract Data Types Slides by David B. Teague, Western.
1 Fall 2002 ACS 168 Problem Solving Using the Computer Weeks 9 and 10 Structures and Classes Class Diagrams Weeks 9 and 10 Structures and Classes Class.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
Learners Support Publications Classes and Objects.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Defining Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Classes: Member Functions and Implementation November 22, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
Chapter 10 Defining Classes. Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
1 Chapter 10 Defining Classes. 2 Overview Introduction Classes (10.2) Abstract Data Types and Structures (10.1)(10.3) Abstract Data Types and Structures.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
Chapter 6 Defining Classes. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Introduction Structures (6.1) Classes (6.2)
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 10 Defining Classes.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 10 Defining Classes.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
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 and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 10 Defining Classes.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Friend Functions.  An ordinary function that is given special access to the private members of a class  NOT a member function of the class  Prototype.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
CMSC 202 Lesson 8 Classes II. Warmup Declare a class (.h part) named “Cup” It has a data member that says how full it is One method called “Drink” that.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 10 Defining Classes.
Structures and Classes
Chapter 10 Defining Classes. Chapter 10 Defining Classes.
Chapter 10 Defining Classes
Chapter 10 Defining Classes. Chapter 10 Defining Classes.
Andy Wang Object Oriented Programming in C++ COP 3330
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Chapter 3: Using Methods, Classes, and Objects
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Classes and Data Abstraction
Chapter 9 Objects and Classes
Learning Objectives Classes Constructors Principles of OOP
Classes and Objects.
Defining Classes and Methods
Presentation transcript:

1 By Joaquin Vila Preparedbv Sally Scott ACS 168 Problem Solving Using the Computer Chapter 6 Classes Chapter 6 Classes

2 Chapter 6 Defining Classes and Abstract Data Types  Structures Structures for Diverse Data Structures as Function Arguments Initializing Structures  Classes Defining Classes and Member Functions Public and Private Members Summary of Properties of Classes Constructors for Initialization  Abstract Data Types Classes to Produce ADTs

3 Some terms  Object a special variable with its own special functions as well as data example: cin is an object of the istream class  Class a data type whose variables are objects  Abstract data type a programmer defined data type, such as a class, that meets certain standards.

4 6.1 Structures  A language feature for grouping of related data  Provides a way to represent a collection of related data using a single unit.  A record with fields  A structure definition defines a type struct CDAccount structure tag { double balance; Member names double interest_rate; int term; // months until maturity }; DON’T FORGET THE SEMICOLON

5  Declaring structure variables: CDAccount emp1; // declaration of Account //structure variable emp1  Declaration with initialization: CDAccount emp2 = { ,.07, 24};  Assignment statements for member variables: emp1.balance = ; emp1.interest_rate =.10; emp1.term = 36; emp2 = emp1; // assigns emp2 values of emp1 emp1.balance = emp2.balance; // assigns balance value

6  Specify member variables with the dot operator structure_name. member_variable_name emp1. balance  Use member variables like any other variables  Different structures may have same member variable names example CDAccount //structure for account with balance, interest_rate, and term member variables SavingAccount // structure for savings account with balance, interest_rate, and term member variables

7 Example of program using a StudentRecord structure: #include using namespace std; struct StudentRecord { int student_number; char grade; }; int main( ) { StudentRecord your_record; your_record.student_number = 2001; your_record.grade = ‘A’; cout << “The grade for student “ << your_record.student_number << “ is “ << your_record.grade << endl; }

8 Practice Given the structure and structure variable declaration: Struct CDAccount { double balance; double interest_rate; int term; char initial1; char initial2; } CDAccount account; What is the type of each of the following? a.account.balance b.account.interest_rate c.CDAccount.term d.saving_account.initial1 e.account.intital2 f.account

9 Practice  Define a structure type named PartRecord with the following fields: an integer partNumber a double price an integer numOnHand an integer numOrdered  Declare two variables, partA and PartB of type PartRecord  Declare a PartRecord named partC, initializing it to be part number 151, with price 9.30, 10 on hand, and 0 ordered. Lect. Notes, pgs

10  Write a function that gets information about a part from the user then returns a PartRecord. The function should have no parameters.

Structures as Function Arguments A function can have call-by-value parameters of structure type and/or call-by-reference parameters of structure type and/or return type that is a structure type Example: CDAccount shrink_wrap(double the_balance, double the_rate, int the_term) { CDAccount temp; temp.balance = the_balance; temp.interest_rate = the_rate; temp.term = the_term; return temp; } 11

12 More on Initializing Structures  A structure may be initialized at the time it is declared. struct Date { int month; int day; int year; }; Date due_date = { 12, 31, 2001};  The sequence of values is used to initialize the successive variables in the struct. The order is essential.  It is an error to have more initializers than variables.  If there are fewer initializers than variables, the initializers provided are used to initialize the data members. The remainder are initialized to 0 for primitive types.

13 Use of Hierarchical Structures  If a structure has a subset of its members that may be considered an entity, consider nested structures.  Example: A PersonInfo struct might include a birthday structure struct Date { int month; int day; int year; }; struct PersonInfo { double height; // inches int weight; // pounds Date birthday; // Date structure };

14 Use Hierarchical Structures, cont. Declare a variable of PersonInfo type as usual: PersonInfo person1; person1.birthday // This is a Date structure, with members accessible // as in any other structure variable. If the structure variable person1 has been set, the year a person was born can be output as follows: cout << person1.birthday.year; structure person1 structure birthday structure variable member member

6.2 Classes Defining Classes and Member Functions  A class is also a user defined data type  Class variables are called objects  An object is a variable that Has member variables (data members) and can hold multiple values (like a structure) Also has member functions (methods)  A class definition specifies the data members (much like a structure definition) and the function members 15 variables functions class

16 A class is a blueprint from which objects are created

17 Defining a Class class DayOfYear { public: void output( ); member function prototype int month; data members int day; };  The scope of a class starts at the name of the class in the class definition and runs to the closing curly brace and semicolon.  Member functions are declared inside of the class definition, but are usually defined outside of it (unless very short)

// Display 6.3 Class with a Member Function (1 of 2) // Program to demonstrate a very simple example of a class. // A better version of the class DayOfYear will be given in Display 6.4. #include using namespace std; class DayOfYear { public: void output( ); member function prototype int month; int day; }; int main( ) { DayOfYear today, birthday; // declaration of two objects of DayOfYear class cout << "Enter today's date:\n"; cout << "Enter month as a number: "; cin >> today.month; cout << "Enter the day of the month: "; cin >> today.day; cout << "Enter your birthday:\n"; cout << "Enter month as a number: "; cin >> birthday.month; cout << "Enter the day of the month: "; cin >> birthday.day; 18

// Display 6.3 Class with a Member Function (1 of 2) cout << "Today's date is "; today. output( ); calls to the member function output cout << "Your birthday is "; birthday.output( ); the calling object if (today.month == birthday.month && today.day == birthday.day) cout << "Happy Birthday!\n"; else cout << "Happy Unbirthday!\n"; return 0; } scope resolution operator //Uses iostream: void DayOfYear :: output( ) member function definition { cout << "month = " << month << ", day = " << day << endl; } 19

20 Classes  Objects of class type are declared much the same as structure variables  Class member variables are also specified using the dot operator  Programmer defined member functions of a class are called exactly like structure member variables, using the dot operator. birthday.monthbirthday.output(); class dot variable class dot member name operator name name operator function

21 Defining Member Functions, cont.  C++ provides the scope resolution operator :: to define a member function outside of the class definition similar to dot operator that is used with object members used with a class name  When defined outside, the function heading must include the class name with the scope resolution operator. The class name is called the type qualifier return_type Class_name :: Function_name(Parameter_list) void DayOfYear :: output( ) { // body of function member function definition }

22 Defining Member Functions, cont.  Anything in the block of the function definition is also considered to be in the scope of the class. You can use names of any members of that class (both data members and function members) in the definition of a member function without the dot operator.  When a member function is called, as in today.output( ); the object today is called the calling object. void DayOfYear::output( ) { cout << “month = “ << month << “, day = “ << day << endl; } void DayOfYear::output( ) { cout << “month = “ << month << “, day = “ << day << endl; }

23 Encapsulation and Information Hiding  Combining several items such as variables, or variables and functions, into a single package, such as an object of some class, is called encapsulation  Each object knows how to perform its own functions and how to interact (interface) with other objects.  Information hiding – the black box concept  The goal is to allow the user to know how to use the functions, but does not know how the function is implemented

24 Access Control with Access Specifiers  use in class definition to label the member variables and functions  public members public: can be accessed from any function, including main( )  private members private: these members can only be accessed inside the class’ own member functions, not by outside functions. this is default best to make all member variables private  order and number of access specifiers in class definition does not matter

Display 6.4 Class with Private Members (1 of 3) //Program to demonstrate the class DayOfYear. #include using namespace std; class DayOfYear { public: void input( ); void output( ); void set(int new_month, int new_day); //Precondition: new_month and new_day form a possible date. //Postcondition: The date is reset according to the arguments. int get_month( ); //Returns the month, 1 for January, 2 for February, etc. int get_day( ); //Returns the day of the month. private: int month; int day; }; 25 A better class definition:

Display 6.4 Class with Private Members (2 of 3) int main( ) { DayOfYear today, bach_birthday; cout << "Enter today's date:\n"; today.input( ); cout << "Today's date is "; today.output( ); bach_birthday.set(3, 21); cout << "J. S. Bach's birthday is "; bach_birthday.output( ); if ( today.get_month( ) == bach_birthday.get_month( ) && today.get_day( ) == bach_birthday.get_day( ) ) cout << "Happy Birthday Johann Sebastian!\n"; else cout << "Happy Unbirthday Johann Sebastian!\n"; return 0; } 26

Display 6.4 Class with Private Members (3 of 3) //Uses iostream: void DayOfYear::input( ) { cout << "Enter the month as a number: "; cin >> month; cout << "Enter the day of the month: "; cin >> day; } void DayOfYear::output( ) { cout << "month = " << month << ", day = " << day << endl; } void DayOfYear::set(int new_month, int new_day) { month = new_month; day = new_day; } int DayOfYear::get_month( ) { return month; } int DayOfYear::get_day( ) { return day; } 27

28 Why Public and Private Members? With an ideal class definition, the class author should be able to change the details of the class implementation without necessitating changes in any program using the class. Use accessor functions to access the data members that are private and cannot be accessed directly.

29 There can be several public and private sections in a class. Members defined after “public:” are accessible by all functions. Members defined after “private:” are accessible only by all functions defined in the class. Public and Private Members

Display 6.5 The Bank Account Class (1 of 3) //Program to demonstrate the class BankAccount. #include using namespace std; //Class for a bank account: class BankAccount { public: void set(int dollars, int cents, double rate); //Postcondition: The account balance has been set to $dollars.cents; //The interest rate has been set to rate percent. void set(int dollars, double rate); //Postcondition: The account balance has been set to $dollars.00. //The interest rate has been set to rate percent. void update( ); //Postcondition: One year of simple interest has been //added to the account balance. double get_balance( ); //Returns the current account balance. double get_rate( ); //Returns the current account interest rate as a percent. void output(ostream& outs); //Precondition: If outs is a file output stream, then //outs has already been connected to a file. //Postcondition: Account balance and interest rate have been written // to the stream outs. 30

Display 6.5 The Bank Account Class (2 of 3) // class BankAccount private: double balance; double interest_rate; double fraction(double percent); //Converts a percent to a fraction. For example, fraction(50.3) returns }; int main( ) { BankAccount account1, account2; cout << "Start of Test:\n"; account1.set(123, 99, 3.0); cout << "account1 initial statement:\n"; account1.output(cout); account1.set(100, 5.0); cout << "account1 with new setup:\n"; account1.output(cout); account1.update( ); cout << "account1 after update:\n"; account1.output(cout); account2 = account1; cout << "account2:\n"; account2.output(cout); return 0; } 31

Display 6.5 The Bank Account Class (3 of 3) void BankAccount::set(int dollars, int cents, double rate) { balance = dollars * cents; interest_rate = rate; } void BankAccount::set(int dollars, double rate) { balance = dollars; interest_rate = rate; } void BankAccount::update( ) { balance = balance + fraction(interest_rate)*balance; } double BankAccount::fraction(double percent) { return (percent/100.0); } double BankAccount::get_balance( ) { return balance; } double BankAccount::get_rate( ) { return interest_rate; } void BankAccount::output(ostream& outs) { outs.setf(ios::fixed); outs.setf(ios::showpoint); outs.precision(2); outs << "Account balance $" << balance << endl; outs << "Interest rate " << interest_rate << "%" << endl; } 32

33 Programming Tips  Make Data Members private make all member variables private. This means these variables can only be accessed or changed using member functions.  Define Accessor Functions. consider providing a complete set of accessors to data in useful formats.  Use the Assignment Operator with Objects: The assignment operator = applies to struct and class objects. You can use it to “copy” the same values from one object to another.

Summary of Properties of Classes 1. Classes have both member variables and member functions. 2. A member (either variable or function) may be public or private. 3. Normally, all variable members of a class are private. 4. A private member of a class cannot be used except in the definition of a function member of the same class. 5. The name of a member function for a class may be overloaded just as the name of an ordinary function. 6. A class may use another class as the type for a member variable. 7. A function may have formal parameters with class type. 8. A function may return an object; that is, a class may be the type for the value returned by a function. (This works correctly with all the classes we have seen so far. Under circumstances we will encounter later, there are special members of the class that must be defined for this to work correctly. ) 34

35 Constructors for Initialization constructor is what “builds” the object. You can write your own constructors or C++ will provide a default constructor for you. A class constructor has the same name as the class. A constructor does not return a value, not even void. (This is the only type of function that does not have a return value.) C++ provides a special kind of member function known as a constructor for automatic initialization of class objects at definition.

36 Constructors for Initialization  Each constructor should provide a value for every member variable.  If the programmer does not write an explicit constructor, a default constructor is automatically provided. However, if any constructor is coded, this automatic default constructor is not provided.  Constructors can be written to allow input of values for some or all variables.  Class constructors may be overloaded as needed.  A default constructor should ALWAYS be included in every class definition. If you write any constructor at all, you should also write a default constructor

37 Constructors for Initialization class BankAccount { public: // Constructors BankAccount(); // default constructor BankAccount( int dollars, int cents, double rate);... Private: double balance; double interest_rate; }; BankAccount::BankAccount(int dollars, int cents, double rate) { balance = dollars +.01 * cents; interest_rate = rate; }

Display 6.6 Class with Constructors (1 of 4) //Program to demonstrate the class BankAccount. #include using namespace std; //Class for a bank account: class BankAccount { public: // Constructors BankAccount(); //Initializes the account balance to $0.00 and the interest rate to 0.0%. BankAccount(int dollars, int cents, double rate); //Initializes the account balance to $dollars.cents and //initializes the interest rate to rate percent. BankAccount(int dollars, double rate); //Initializes the account balance to $dollars.00 and //initializes the interest rate to rate percent. // Accessor functions double get_balance(); //Returns the current account balance. double get_rate(); //Returns the current account interest rate as a percent. 38

Display 6.6 Class with Constructors (2 of 4) // other member functions void update(); //Postcondition: One year of simple interest has been added to the account balance. void output(ostream& outs); //Precondition: If outs is a file output stream, then outs has already been connected to a file. //Postcondition: Account balance and interest rate have been written to the //stream outs. private: double balance; double interest_rate; // private helper function double fraction(double percent); //Converts a percent to a decimal fraction. For example, fraction(50.3) returns }; int main() { BankAccount account1, // uses default constructor account2(100, 2.3); // uses third constructor cout << "account1 initialized as follows:\n"; account1.output(cout); cout << "account2 initialized as follows:\n"; account2.output(cout); 39

Display 6.6 Class with Constructors (3 of 4) account1 = BankAccount(999, 99, 5.5); // calls second constructor to update account1 cout << "account1 reset to the following:\n"; account1.output(cout); return 0; } BankAccount::BankAccount() { balance = 0; interest_rate = 0.0; } BankAccount::BankAccount(int dollars, int cents, double rate) { balance = dollars *cents; interest_rate = rate; } BankAccount::BankAccount(int dollars, double rate) { balance = dollars; interest_rate = rate; } 40

Display 6.6 Class with Constructors (4 of 4) void BankAccount::update( ) { balance = balance + fraction(interest_rate)*balance; } double BankAccount::fraction(double percent) { return (percent/100.0); } double BankAccount::get_balance( ) { return balance; } double BankAccount::get_rate( ) { return interest_rate; } //Uses iostream: void BankAccount::output(ostream& outs) { outs.setf(ios::fixed); outs.setf(ios::showpoint); outs.precision(2); outs << "Account balance $" << balance << endl; outs << "Interest rate " << interest_rate << "%" << endl; } 41

Calling a Constructor A constructor is called automatically when an object is declared. If you are not using the default constructor, you must give the arguments for the constructor when you declare the object. Syntax (for an object declaration when using a declared constructor other than the default constructor): ClassName ObjectName(Arguments_for_Constructor); Example: BankAccount account1(100, 2.3); Calling a constructor can be called after object declaration: Syntax (for an explicit constructor call): ObjectName = ConstructorName(Arguments_for_Constructor); Example: account1 = BankAccount(200, 3.5); A constructor must have the same name as the class of which it is a member. Hence Class_Name and Constructor_Name are the same identifier. 42

Pitfall: Constructors with no arguments  The declaration BankAccount object_name(100, 2.3); invokes the BankAccount constructor that requires two parameters.  The function call func(); invokes a function func that takes no parameters  Conversely, BankAccount objectname(); does NOT invoke the no-parameter constructor.  Rather, this line of code defines a function that returns an object of BankAccount type. 43

Constructors with No Arguments When you declare an object and want the constructor with zero arguments to be called, you do not include parentheses. For example, to declare an object and pass two arguments, you might do this: BankAccount account(100, 2.3); However, to cause the constructor with NO arguments, to be called, you declare the object: BankAccount account You do NOT declare the object BankAccount account(); 44

6.3 Abstract Data Types  A data type is called an Abstract Data Type (ADT) if the programmers who use the type do not have access to the details of how the values and operations are implemented.  Programmer defined types are not automatically ADTs. Care is required in construction of programmer defined types to prevent unintuitive and difficult-to- modify code. 45

Classes to Produce ADTs: How to make an ADT  Member variables - make private.  Basic operations that the programmer needs make a public member function of the class for each one, and fully specify how to use each such function (use clear comments).  Helping functions make private member functions.  The interface the public member functions along with commentary telling how to use the member functions. The interface of an ADT should tell all the programmer need to know to use the ADT.  The implementation private members of the class and the definitions of all member functions. This is information the programmer should NOT NEED to use the class. 46

Remember:  The client programmer does not need to know how data is stored, nor how the functions are implemented.  Consequently alternative implementations may store different value types. 47

Display 6.7 Alternative BankAccount Implementation(1 of 6) //Demonstrates an alternative implementation of the class BankAccount. #include using namespace std; Notice that the public members of //Class for a bank account: BankAccount look and behave class BankAccount exactly the same as in Display 6.6 { public: BankAccount(int dollars, int cents, double rate); //Initializes the account balance to $dollars.cents and //initializes the interest rate to rate percent. BankAccount(int dollars, double rate); //Initializes the account balance to $dollars.00 and //initializes the interest rate to rate percent. BankAccount( ); //Initializes the account balance to $0.00 and the interest rate to 0.0%. void update( ); //Postcondition: One year of simple interest has been added to // account balance. 48

Display 6.7 Alternative BankAccount Implementation(2 of 6) double get_balance( ); //Returns the current account balance. double get_rate( ); //Returns the current account interest rate as a percent. void output(ostream& outs); //Precondition: If outs is a file output stream, then //outs has already been connected to a file. //Postcondition: Account balance and interest rate have been //written to the stream outs. private: int dollars_part; int cents_part; double interest_rate;//expressed as a fraction, e.g., for 5.7% double fraction(double percent); New //Converts a percent to a fraction. For example, fraction(50.3) //returns double percent(double fraction_value); //Converts a fraction to a percent. For example, percent(0.503) //returns }; 49

Display 6.7 Alternative BankAccount Implementation(3 of 6) int main( ) { BankAccount account1(100, 2.3), account2; cout << "account1 initialized as follows:\n"; The body of main is identical account1.output(cout); to that in Display 6.6, the cout << "account2 initialized as follows:\n"; screen output is also identical account2.output(cout); Display 6.6. account1 = BankAccount(999, 99, 5.5); cout << "account1 reset to the following:\n"; account1.output(cout); return 0; } BankAccount::BankAccount(int dollars, int cents, double rate) { dollars_part = dollars; cents_part = cents; interest_rate = fraction(rate); } 50

Display 6.7 Alternative BankAccount Implementation(4 of 6) BankAccount::BankAccount(int dollars, double rate) { dollars_part = dollars; cents_part = 0; interest_rate = fraction(rate); } BankAccount::BankAccount( ) { dollars_part = 0; cents_part = 0; interest_rate = 0.0; } double BankAccount::fraction(double percent) { return (percent/100.0); } 51

Display 6.7 Alternative BankAccount Implementation(5 of 6) //Uses cmath: void BankAccount::update( ) { double balance = get_balance( ); balance = balance + interest_rate*balance; dollars_part = floor(balance); cents_part = floor((balance - dollars_part)*100); } double BankAccount::get_balance( ) { return (dollars_part *cents_part); } double BankAccount::percent(double fraction_value) { return (fraction_value*100); } double BankAccount::get_rate( ) { return percent(interest_rate); } 52

Display 6.7 Alternative BankAccount Implementation(6 of 6) //Uses iostream: void BankAccount::output(ostream& outs) { outs.setf(ios::fixed); new definition outs.setf(ios::showpoint); outs.precision(2); outs << "Account balance $" << get_balance( ) << endl; outs << "Interest rate " << get_rate( ) << "%" << endl; } The new definitions of get_balance and get_rate ensure that the output will still be in the correct units. 53

Information Hiding  You should write the functions so that they can be used with no knowledge of how they were written: as if they were black boxes. We know only the interface and specification.  All the programmer needs to know about a function is its prototype and accompanying comment that explains how to use the function.  The use of private member variables and private member functions in the definition of an abstract data type is another way to implement information hiding, where we now apply the principle to data values as well as to functions. 54

55 The Class Diagram (lec. notes, p.97) BankAccount -balance: double -interest_rate: double +BankAccount (dollars, cents, rate) +BankAccount(dollars, rate) +BankAccount( ) +update( ): void +get_balance( ): double +get_rate( ): double +output(outs): void -fraction(percent): double Class name member variables member functions (with parameters) function return type + public members _ private members