CSci 152: Programming II Fall 2004 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

CSci 152: Programming II Fall 2004

Description:

In the function equalTime, the parameter otherTime is a constant reference parameter. ... of the actual parameter, but otherTime cannot modify the value of the ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 22
Provided by: DerekH71
Category:

less

Transcript and Presenter's Notes

Title: CSci 152: Programming II Fall 2004


1
CSci 152 Programming IIFall 2004
  • Classes and Data Abstraciton

2
  • In this lecture, you will
  • Learn about classes
  • Learn about private, protected, and public
    members of a class

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
  • If a member of a class is a variable, you declare
    it just like any other variable. Also, in the
    definition of the class, you cannot initialize a
    variable when you declare it.
  • If a member of a class is a function, you
    typically use the function prototype to define
    that member.
  • If a member of a class is a function, it can
    (directly) access any member of the classdata
    members and function members. That is, 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.
  • In C, 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.
  • A missing semicolon, therefore, will result in a
    syntax error.

5
  • The members of a class are classified into three
    categories private, public, and protected. This
    chapter mainly discusses the first two typesthat
    is, private and public.
  • 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.
  • 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. (member access specifiers)

6
  • Define a class, clockType, to implement the time
    of day in a program.
  • Time is represented as a set of three integers
    one to represent the hours, one to represent the
    minutes, and one to represent the seconds.
  • Perform the following operations on the time
  • 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 for equality.

7
(No Transcript)
8
  • Some members of the class clockType will be
    private others will be public.
  • Any member that needs to be accessed outside the
    class is declared public any member that should
    not be accessed directly by the user should be
    declared private.
  • The user should be able to set the time and print
    the time. Therefore, the members that set the
    time and print the time should be declared
    public.

9
  • class clockType
  • public
  • void setTime(int, int, int)
  • void getTime(int, int, int)
  • void printTime() const
  • void incrementSeconds()
  • void incrementMinutes()
  • void incrementHours()
  • bool equalTime(const clockType otherTime)
    const
  • private
  • int hr
  • int min
  • int sec

10
  • The class clockType has seven function members
    setTime, getTime, printTime, incrementSeconds,
    incrementMinutes, incrementHours, and equalTime.
  • It has three data members hr, min, and sec.
  • The three data membershr, min, and sec, are
    private to the class and cannot be accessed
    outside the class.
  • The seven function memberssetTime, getTime,
    printTime, incrementSeconds, incrementMinutes,
    incrementHours, and equalTimecan directly access
    the data members (hr, min, and sec).
  • In the function equalTime, the parameter
    otherTime is a constant reference parameter. That
    is, in a call to the function equalTime, the
    parameter otherTime receives the address of the
    actual parameter, but otherTime cannot modify the
    value of the actual parameter.
  • The word const at the end of the member functions
    printTime and equalTime specifies that these
    functions cannot modify the data members of a
    variable of the type clockType.

11
  • 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

12
(No Transcript)
13
  • 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.

14
  • Example 13-1
  • myClock.setTime(5,2,30)
  • myClock.printTime()
  • yourClock.setTime(x,y,z) //Assume x, y, and
  • //z are variables of the type int
  • if(myClock.equalTime(yourClock))
  • .
  • .
  • .
  • myClock.hr 10 //illegal
  • myClock.min yourClock.min //illegal

15
  • Built-in Operations on Classes
  • Most of Cs built-in operations do not apply to
    classes.
  • You cannot use arithmetic operators to perform
    arithmetic operations on class objects (unless
    they are overloaded).
  • For example, you cannot use the operator to add
    two class objects of, say, the type clockType.
  • 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 ().

16
  • The Assignment Operator and Classes
  • Suppose that myClock and yourClock are variables
    of the type clockType.

17
  • The statement
  • myClock yourClock
  • copies the value of yourClock into myClock
  • a. the value of yourClock.hr is copied into
    myClock.hr,
  • b. the value of yourClock.min is copied into
    myClock.min
  • c. the value of yourClock.sec is copied into
    myClock.sec

18
  • Class Scope
  • A class variable 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 class variable name and the member
    selection operator (.).

19
  • 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.

20
  • Reference Parameters and Class Objects
    (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.
  • 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.
  • In C, 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.

21
  • void testTime(const clockType otherClock)
  • clockType dClock
  • ...
Write a Comment
User Comments (0)
About PowerShow.com