Presentation is loading. Please wait.

Presentation is loading. Please wait.

72 4/11/98 CSE 143 Abstract Data Types [Sections 1.1-1.5, 3.1-3.2]

Similar presentations


Presentation on theme: "72 4/11/98 CSE 143 Abstract Data Types [Sections 1.1-1.5, 3.1-3.2]"— Presentation transcript:

1 72 4/11/98 CSE 143 Abstract Data Types [Sections 1.1-1.5, 3.1-3.2]

2 73 4/11/98 Data Abstraction What is Abstraction?  An idealization  A focus on essential qualities, disregarding the “details”  An emphasis on the what rather than the how Specification vs. Implementation  A problem-solving technique

3 74 4/11/98 Examples of Abstraction  A Chair  A Computer  Logic gates  Processor  Desktop box  Abstractions can be built from other abstractions

4 75 4/11/98 Abstraction in Programming  The type int is an abstraction for a way of interpreting bits in memory as a number  A struct is an abstraction of a collection of related data items  A function is a programmer-designed abstraction for some computation  A module is a programmer-designed abstraction that groups functions and data together and provides an interface

5 76 4/11/98 Why Abstraction?  Abstractions helps in managing complexity  Don’t need to know details, just interface  Treat abstractions as “black box” components to build upon  Know what inputs go into box, and what outputs come out, but not what goes on inside the box  Hierarchical or layered decomposition

6 77 4/11/98 Data Abstraction and Classes  Data Types have two components  Set of possible values  Operations that can be applied to values  Examples  Integers: arithmetic operations, printing, etc.  Boolean: AND, OR, NOT, test if TRUE, etc.  Grade Transcript: Add, remove classes and grades, change grades, etc.

7 78 4/11/98 Abstract Data Types (ADTs) Composed of two parts:  Specification  Name of new type  Constructors to make instances  Public operators on instances  Implementation  Representation of new type  Implementation of public operators, constructors  Additional private operations, data, etc.

8 79 4/11/98 Types vs. Instances  Types  General category  Usually few in number  Some built in ( int, char, double, etc.)  Programmer-defined (arrays, structs, enums, classes, etc.)  Instances  Particular expressions  May have many instances of a given type

9 80 4/11/98 Classes vs. Structs  C++ adds a class construct for ADTs  A lot like a C struct in syntax: class GradeTranscript { // Class member declarations };  Enhancements over C struct :  Can specify private vs. public members  Members (= components) can be functions, not just data

10 81 4/11/98 A Bank Account Class class BankAccount { public: void init(char name[]); void deposit(double amount); double amount(); private: char owner[30]; double balance; };  Inside of a class definition, you can declare variables (data members) and functions (member functions)  Members can be public or private

11 82 4/11/98 Class Basics  By default, a private interface is assumed.  Members in the private interface are “hidden” from clients. The compiler will not allow client code to access them.  Public members may be used directly by clients  For the BankAccount class, tell me:  How many data members? private? public?  How many member functions?  What can the client use directly?

12 83 4/11/98 How Clients Use a Class  A class is treated like any programmer-defined type:  Declare variables of that type: BankAccount myAccount;  Specify types of arguments (parameters): void doSomething (BankAccount anAccount);  Use it to build other types: class Bank { public:... private: BankAccount accounts[100]; };

13 84 4/11/98 A Class is a Type BankAccount a1, a2;  The code above creates two instances of the BankAccount class.  Each instance has its own copy of the data members of the class: owner: “Jack” balance: 200.17 owner: “Jill” balance: 940.15 a1 a2

14 85 4/11/98 Operations on instances  Most built-in operations DO NOT apply to class instances  You cannot (for example):  use the “+” to add two BankAccount instances  use the “==“ to compare to accounts for equality  To the client, the only valid operations on instances are  assignment (“=“)  member selection (“.”)  any operations defined in the public interface of the class.

15 86 4/11/98 Terminology  Think of a class as a cookie cutter, used to stamp out concrete objects (instances)  Think of objects as simple creatures that we communicate with via “messages.” BankAccount myAccount; myAccount.deposit(300.15); instance receiver message selection argument

16 87 4/11/98 Information Hiding  The private access modifier supports and enforces information hiding // A client program... BankAccount account; account.balance = 10000.0; // NO! why? cout << account.balance; // NO! why? account.init(“Jill”);// ok? account.deposit(40.0); // ok? cout << account.amount();// ok?

17 88 4/11/98 Building the Class: Specification #ifndef BANKACCOUNT_H #define BANKACCOUNT_H class BankAccount { public: void init(char name[]); void deposit(double amount); double amount(); private: char owner[30]; double balance; }; #endif BankAccount.h

18 89 4/11/98 Building the Class: Implementation #include “BankAccount.h” void BankAccount::init(char name[]) { balance = 0.0; strcpy(owner, name); } double BankAccount::amount() { return balance; } void BankAccount::deposit(double amount) { balance = balance + amount; } BankAccount.cpp scope resolution operator need class name here

19 90 4/11/98 Class Scope: Implementation  Implementations of member functions use classname :: prefix to indicate which class  “ :: ” is the scope resolution operator  Within member function body:  omit object name  Refer to members directly  Don’t use class member names for formal parameters and local variables  private members visible to public members, but not clients

20 91 4/11/98 Classes and Modules  Typically put each ADT in its own module  One.h file for interface  One.cpp file for implementation  Can put multiple classes in one module  Why might one want to do this?  In C++, must list private data and functions in the class definition, which goes in.h interface file  Why is this unfortunate?

21 92 4/11/98 Summary  class construct for Abstract Data Types  Function members (operations)  Data members (representation)  Multiple instances, usually  public vs. private members  Member scope  Assignment, parameter passing  Classes vs. Modules


Download ppt "72 4/11/98 CSE 143 Abstract Data Types [Sections 1.1-1.5, 3.1-3.2]"

Similar presentations


Ads by Google