Kate Gregory with material from Deitel and Deitel - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Kate Gregory with material from Deitel and Deitel

Description:

Monday, Jan 27, 2003. Kate Gregory. with material from Deitel and Deitel. CO 204 ... Information hiding design information. Monday, Jan 27, 2003. Kate Gregory ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 31
Provided by: kategr
Category:

less

Transcript and Presenter's Notes

Title: Kate Gregory with material from Deitel and Deitel


1
Week 4
  • Questions from Last Week
  • Hand in Lab 2
  • Classes

2
(No Transcript)
3
Data Abstraction
  • An extension of the concept of a structure
  • Gathers together a number of related variables
  • e.g. Bank Account
  • Owner
  • Balance
  • transactions

4
Defining your own Types
  • Built-in types have allowed operations
  • Cant multiply strings
  • Cant use on a float
  • Cant pass an integer to strlen()
  • In the same vein, you choose the operations for
    your own types
  • Bank account can have deposit and withdraw

5
An Object is a Clump
  • Information, Variables, Data, State, Attributes
  • Behaviour, Methods, Functions, Operations,
    Services
  • All belong together

6
Defining a Class
  • //BankAccount.h
  • class BankAccount
  • float Balance
  • void Deposit(float amount)
  • bool Withdraw (float amount)
  • Dont forget that final semi colon!

7
Inside and Outside
8
Defining a Class
  • //BankAccount.h
  • class BankAccount
  • private
  • float Balance
  • public
  • void Deposit(float amount)
  • bool Withdraw (float amount)

9
Creating Objects
  • An object is an instance of a class
  • Class is BankAccount object is my account,
    another object is someone elses
  • include BankAccount.h
  • // ...
  • BankAccount KateChequing
  • BankAccount BillSaving

10
Private means no access
  • All code can access public functions
  • KateChequing.Deposit(50)
  • But not private variables
  • KateChequing.Balance 100

11
Implementing a Class
  • //BankAccount.cpp
  • include BankAccount.h
  • void BankAccountDeposit(float amount)
  • Balance amount
  • is called Scope Resolution Operator

12
Encapsulation
  • Holding all the rules together
  • Not forcing users of a class to know what the
    inside means
  • Retaining the freedom to change the inside of a
    class
  • Variable name
  • Type (int, float)
  • Units or other meaning
  • Information hiding design information

13
Gets and Sets
  • class Truck
  • private
  • float currentweight
  • public
  • float getcurrentweight()
  • void setcurrentweight(float w)

14
Constructors
  • Sometimes your only motivation for a set is to
    give something an initial value
  • A constructor can be used to initialize the
    member variables of an object as it is created
  • Name is always the name of the class
  • Never a return type
  • Parameters vary

15
Default Constructor
  • No parameters
  • Runs when instances are created without
    parameters
  • class BankAccount
  • // ...
  • BankAccount()

16
Default Constructor
  • //BankAccount.cpp
  • include BankAccount.h
  • BankAccountBankAccount()
  • Balance 0

17
Overloaded Functions
  • In C, two functions cannot have the same name
  • In C, they can, as long as something else
    differs
  • Class
  • Parameters
  • Return type or placeholder names dont matter

18
Overloaded Constructors
  • class BankAccount
  • // ...
  • BankAccount()
  • BankAccount(float openingbalance)
  • BankAccount(float a) // not allowed
  • BankAccount(char name)

19
Passing Parameters to Constructors
  • BankAccount KateChequing(50.0)
  • Uses the constructor that takes a float
  • BankAccount BillSaving(Bill)
  • Uses the constructor that takes a char
  • BankAccount KateChequing(1000)
  • Uses the constructor that takes a float, and
    converts the int to float first

20
Dont Do This
  • BankAccount KateChequing()
  • This is declaring a function called KateChequing!
  • BankAccount GetAccount(int x)

21
Initializer Syntax
  • //BankAccount.cpp
  • include BankAccount.h
  • BankAccountBankAccount()
  • Balance(0)
  • Slightly more efficient and readable
  • Becomes important once inheritance enters the
    picture
  • Also useful when a member variable is actually an
    object

22
Destructors
  • Opposite of constructors
  • Name is plus name of class
  • Joke based on boolean syntax
  • Never take arguments
  • No return type
  • Rarely explicitly called

23
Leaving Scope
  • // ... Program fragment
  • int i
  • BankAccount Kate(1000)
  • Destructor runs as Kate leaves scope

24
Coding Destructors
  • class BankAccount
  • // ...
  • BankAccount()
  • //BankAccount.cpp
  • include BankAccount.h
  • BankAccountBankAccount()
  • cout ltlt There goes your money

25
Reuse Thoughts
  • Objects should be self contained
  • Constructors should set good default values
  • If there is no default value for something, every
    constructor should demand it, so that it cannot
    remain uninitialized
  • You should add all the methods others are likely
    to use
  • Dont add gets and sets without thinking
  • The object should handle its own error checking
  • Avoid side-effects such as error messages

26
Inline functions
  • Dramatically improve performance
  • Compiler actually expands the code like a macro
  • You retain the protection of encapsulation at no
    performance cost

27
Inline Gets and Sets
  • class Truck
  • private
  • float currentweight
  • public
  • float getcurrentweight()
  • return currentweight
  • void setcurrentweight(float w)
  • currentweight w

28
Error checking
  • class Truck
  • private
  • float currentweight
  • public
  • float getcurrentweight()
  • return currentweight
  • void setcurrentweight(float w)
  • if (w gt 0) currentweight w

29
Private Functions
  • Serve some useful purpose
  • You dont want the whole system to be able to
    call them
  • You arent willing to commit they will always
    exist
  • Can only be called from inside the object itself

30
For Next class
  • Read chapter 7
  • Get ready for Lab 3 to be handed out
Write a Comment
User Comments (0)
About PowerShow.com