Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Classes

Description:

The behavior of each kind of object is defined in a class. Writing Classes ... a class which can hold objects of any kind. vector int scores(100) ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 14
Provided by: samiro
Category:
Tags: classes | kind

less

Transcript and Presenter's Notes

Title: Classes


1
Classes
2
What is a class?
  • Data and the functions (methods) that operate on
    that data collectively called members
  • Example bank account class
  • Provide structure for organizing programs
  • Almost like an advanced struct

3
Syntax
  • class classname
  • private
  • public
  • private only accessible within the class
  • public accessible from anywhere (like with a
    struct)

4
Member Functions
  • Typically, data (variables) declared private
  • Functions operate on data
  • accessor functions read data, provide access to
    data but do not change it
  • update functions change data
  • examples from bank account, zoo???
  • constructor builds a new object

5
Object-Oriented Design
  • Method for designing computer programs
  • Consider objects interacting in the program
  • Example a zoo, coffee machine, grade book
  • The behavior of each kind of object is defined in
    a class

6
Writing Classes
  • Typically, two files header and source
  • Header declares member variables, provides
    function prototypes and bodies of short functions
  • Source provides code for rest of functions
  • When specifying function bodies outside of class
    definition must be used
  • void BankAccountwithdraw(double amount)

7
Creating and Using Objects
  • BankAccount b(Sami Rollins)
  • //Type Name(constructor parameters)
  • //how would you withdraw funds?

8
Creating and Using Objects
  • BankAccount b(Sami Rollins)
  • Type Name(constructor parameters)
  • //how would you withdraw funds?
  • b.withdraw(300)
  • object_name.method_name(param list)

9
Constructor
  • BankAccountBankAccount(double init_balance)
  • class_name(parameter list)
  • Special-case function called when a new object is
    created
  • Used to initialize member variables
  • Examples?

10
Alternative Initialization
  • BankAccountBankAccount(double init_balance)
  • balance init_balance
  • BankAccountBankAccount(double init_balance)
    balance(init_balance)

11
Destructor
  • BankAccountBankAccount()
  • class_name
  • Used if class allocates memory dynamically (uses
    new)
  • Delete all dynamically declared objects

12
friend
  • Allow a class to access the private members of
    another class
  • Operator overloading
  • Closely related classes

13
STL and Templates
  • STL Standard Template Library
  • collection of classes useful for many programs
  • Templates
  • a class which can hold objects of any kind
  • vectorltintgt scores(100)
  • vectorltBankAccountsgt bank_records(500)
  • Vector resizable array
Write a Comment
User Comments (0)
About PowerShow.com