CLASSES AND DATA ABSTRACTION - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

CLASSES AND DATA ABSTRACTION

Description:

A new candy machine is bought for the gym, but it is not working properly. This candy machine currently sells candies, chips, gum, and cookies ... – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 41
Provided by: charlyn6
Category:

less

Transcript and Presenter's Notes

Title: CLASSES AND DATA ABSTRACTION


1
Chapter 13
  • CLASSES AND DATA ABSTRACTION

2
Objectives
  • In this chapter you will
  • Learn about classes
  • Learn about private, protected, and public
    members of a class
  • Explore how classes are implemented
  • Examine constructors and destructors
  • Learn about the abstract data type (ADT)
  • Explore how classes are used to implement ADT
  • Learn about information hiding
  • Explore how information hiding is implemented in
    C

3
Classes
  • A class is a collection of a fixed number of
    components
  • The components of a class are called members of
    the class
  • The general syntax of defining a class is
  • class classIdentifier
  • classMemberList
  • A member of a class can either be a variable
    (that is, to store some data) or a function

4
Classes
  • If a member of a class is a variable, it is
    declared just like any other variable
  • In the definition of the class, you cannot
    initialize a variable when you declare it
  • If a member of a class is a function, typically
    the function prototype is used to define that
    member
  • A missing semicolon, therefore, will result in a
    syntax error

5
Classes
  • If a member of a class is a function,
  • it can (directly) access any member of the
    classdata members and function members
  • when you write the definition of the member
    function, you can directly access any data member
    of the class without passing it as a parameter
  • the only obvious condition is that you must
    declare an identifier before you can use it
  • Class is a reserved word and it only defines a
    data type, no memory is allocated
  • The semicolon after the right brace is part of
    the syntax

6
Classes
  • The members of a class are classified into three
    categories
  • private
  • public
  • protected
  • Following are some facts about public and private
    members of a class
  • By default, all members of a class are private
  • If a member of a class is private, you cannot
    access it outside the class

7
Classes
  • A public member is accessible outside the class
  • To make a member of a class public, you use the
    label public with a colon
  • In C, private, protected, and public are
    reserved words

8
Variable (Object) Declaration
  • Once a class is defined, you can declare
    variables of that type
  • In C terminology, a class variable is called a
    class object or class instance
  • To become familiar with this terminology, we will
    use the term class object, or simply object, for
    a class variable
  • The syntax for declaring a class object is the
    same as that for declaring any other variable
  • clockType myClock
  • clockType yourClock

9
Accessing Class Members
  • Once an object is declared, it can access the
    public members of a class
  • The general syntax to access the member of a
    class is
  • classVariableName.memberName
  • In C, the dot, . (period), is an operator
    called the member access operator

10
Built-in Operations on Classes
  • Most of Cs built-in operations do not apply to
    classes
  • Arithmetic operators cannot be used to perform
    arithmetic operations on class objects unless
    they are overloaded
  • Also, you cannot use relational operators to
    compare two class objects for equality
  • The two built-in operations that are valid for
    class objects are member access (.) and
    assignment ()

11
Class Scope
  • A class object has the same scope as other
    variables
  • A member of a class is local to the class
  • We access a class member outside the class by
    using the class object name and the member access
    operator (.)

12
Functions and Classes
  • Class objects can be passed as parameters to
    functions and returned as function values
  • As parameters to functions, classes can be passed
    either by value or by reference
  • If a class object is passed by value, the
    contents of the data members of the actual
    parameter are copied into the corresponding data
    members of the formal parameter

13
Reference Parameters Variables
  • As a parameter, a class object can be passed by
    value
  • If a variable is passed by value, the
    corresponding formal parameter receives a copy of
    the data of the variable
  • This operation might require, in addition to a
    large amount of storage space, a considerable
    amount of computer time to copy the value of the
    actual parameter into the formal parameter

14
Reference Parameters Variables
  • If a variable is passed by reference, the formal
    parameter receives only the address of the actual
    parameter
  • Therefore, an efficient way to pass a variable as
    a parameter is by reference
  • If a variable is passed by reference, then when
    the formal parameter changes, the actual
    parameter also changes
  • You can pass a variable by reference and still
    prevent the function from changing its value, by
    using the keyword const in the formal parameter
    declaration

15
Constructors
  • Constructors
  • We can guarantee the initialization of the data
    members of a class by using constructors
  • There are two types of constructors
  • with parameters
  • without parameters
  • The constructor without parameters is called the
    default constructor
  • A constructor that has no parameters, or has all
    default parameters, is called the default
    constructor

16
Constructors
  • 1. The name of a constructor is the same as the
    name of the class
  • 2. A constructor
  • even though it is a function, has no type
  • it is neither a value-returning function nor a
    void function
  • 3. A class can have more than one constructor,
    however, all constructors of a class have the
    same name
  • 4. If a class has more than one constructor, they
    must have different sets of parameters

17
Constructors
  • 5. Constructors
  • are automatically executed when a class variable
    enters its scope
  • since they have no types, they cannot be called
    like other functions
  • 6. Which constructor executes depends on the type
    of values passed to the class variable when the
    class variable is declared

18
Invoking a Constructor
  • A constructor is automatically executed when a
    class variable is declared
  • A class might have more than one constructor,
    including the default constructor

19
Invoking the Default Constructor
  • The syntax to invoke the default constructor is
  • className classVariableName
  • The statement
  • clockType yourClock
  • declares yourClock to be a variable of the type
    clockType
  • In this case, the default constructor is executed
    and the data members of yourClock will be
    initialized to zero

20
Constructor with Parameters
  • The syntax to invoke a constructor with
    parameters is
  • className classVariableName(argument1,
  • argument2,. . . )
  • where argument1, argument2, etc. is either a
    variable or an expression

21
Constructor with Parameters
  • 1. The number of arguments and their type should
    match the formal parameters (in the order given)
    of one of the constructors
  • 2. If the type of the arguments do not match the
    formal parameters of any constructor (in the
    order given), C will use type conversion and
    look for the best match
  • For example,
  • an integer value might be converted to a
    floating-point value with zero decimal part
  • an ambiguity will result in a compile-time error

22
Arrays
  • If a class has constructors and you declare an
    array of class objects, the class must have the
    default constructor
  • The default constructor is used to initialize
    each (array) class object
  • For example
  • clockType clocks100

23
Destructors
  • Like constructors, destructors are also functions
  • The name of a destructor is the character ''
    followed by the name of the class
  • The name of the destructor for the class
    clockType is
  • clockType()
  • A class can have only one destructor and it has
    no parameters
  • The destructor is automatically executed when the
    class object goes out of scope

24
Abstract Data Types
  • The data type clockType has three data members
    and the following basic operations
  • 1. Set the time
  • 2. Return the time
  • 3. Print the time
  • 4. Increment the time by one second
  • 5. Increment the time by one minute
  • 6. Increment the time by one hour
  • 7. Compare the two times to see whether they are
    equal

25
Abstract Data Types
  • Abstract Data Type (ADT) - a data type that
    specifies the logical properties without the
    implementation details
  • An ADT has three things associated with it
  • The name of the ADT, called type name
  • The set of values belonging to the ADT, called
    domain
  • The set of operations on the data

26
A Struct Versus a Class
  • By default the members of a struct are public
  • By default the members of a class are private
  • The member access specifier private can be used
    in a struct to make a member private
  • Both C classes and structs have the same
    capabilities
  • Most programmers restrict their use of structures
    to adhere to their C-like structure form
  • If all of the data members of a class are public
    and the class has no member functions, typically
    a struct is used to group these members

27
Information Hiding
  • The header file has an extension h
  • The implementation file has an extension cpp
  • The implementation file must include the header
    file via the include statement
  • In an include statement, user-defined header
    files are enclosed in double quotes while
    system-provided header files are enclosed between
    angular brackets

28
Files
  • Visual C, C Builder, and CodeWarrior put the
    editor, compiler, and linker all into one package
  • With one command, the program is compiled and
    linked with the other necessary files
  • These systems also manage multiple file programs
    in the form of a project
  • A project consists of several files, called the
    project files

29
Files
  • These systems usually have a command, called
    build, rebuild, or make
  • When the build, rebuild, or make command is
    applied to a project, the system automatically
    compiles and links all files required to create
    the executable code
  • When one or more files in the project change, you
    can use these commands to recompile and relink
    the files

30
Programming Example
  • A common place to buy candy is the candy machine
  • A new candy machine is bought for the gym, but it
    is not working properly
  • This candy machine currently sells candies,
    chips, gum, and cookies
  • You have been asked to write a program for this
    candy machine so it can be put into operation

31
Programming Example
  • The program should
  • 1. Show the customer the different products sold
    by the candy machine
  • 2. Let the customer make the selection
  • 3. Show the customer the cost of the item
    selected
  • 4. Accept money from the customer
  • 5. Release the item
  • Input - item selection and cost of the item
  • Output - selected item

32
Problem Analysis
  • A candy machine has two main components
  • a built in cash register
  • several dispensers to hold and release the
    product

33
Main Program
  • When the program executes, it must
  • 1. Show the different products sold by the candy
    machine
  • 2. Show how to select a particular product
  • 3. Show how to terminate the program
  • These instructions must be displayed after
    processing each selection, except exiting the
    program
  • Once the user has made the appropriate selection,
    the candy machine must act accordingly

34
Main Program
  • If the user has selected to a buy a product and
    if the product is available, the candy machine
    should show the cost of the product and ask the
    user to deposit the money
  • If the money deposited is at least the cost of
    the item, the candy machine should sell the item
    and display an appropriate message

35
Menu
  • 1. Show the selection to the customer
  • 2. Get selection
  • 3. If selection is valid and the dispenser
    corresponding to the selection is not empty, sell
    the product

36
Menu
  • The menu looks like the following
  • showSelection
  • a. Welcome to Shelly's Candy Shop "
  • b. To select an item, enter
  • c. 1 for Candy
  • d. 2 for Chips
  • e. 3 for Gum
  • f. 4 for Cookies
  • g. 9 to exit

37
sellProduct
  • If the dispenser is nonempty
  • show and prompt the customer to enter the cost of
    the item
  • get the amount entered by the customer
  • if the amount entered by the customer is less
    than the cost of the product
  • show and prompt the customer to enter the
    additional amount
  • calculate the total amount entered by the
    customer

38
sellProduct
  • if the amount entered by the customer is at least
    the cost of the product
  • update the amount in the cash register
  • sell the product, that is, decrement the number
    of items in the dispenser by 1, display an
    appropriate message
  • if the amount entered by the user is less than
    the cost of the item, ask the user to deposit
    additional money
  • If the dispenser is empty, tell the user that
    this product is sold out

39
The Function Main
  • 1. Create the cash register, which means that we
    declare a variable of the type cashRegister
  • 2. Create four dispensers, which means that we
    declare four objects of the type dispenserType
    and also initialize these objects
  • For example,
  • the statement dispenserType candy(100, 50)
    creates a dispenser object, candy, to hold
    candies
  • the number of items in the dispenser is 100 and
    the cost of an item is 50 cents

40
The Function Main
  • 3. Declare additional variables as necessary
  • 4. Show selection, call the function
    showSelection
  • 5. Get the selection
  • 6. While not done (a selection of 9 exists the
    program)
  • a. Sell product, call the function sellProduct
  • b. Show selection, call the function
    showSelection
  • c. Get selection
Write a Comment
User Comments (0)
About PowerShow.com