Lecture 17 Classes and Objects - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Lecture 17 Classes and Objects

Description:

'This method is, to define as the number of a class ... Refers to hiding a class's internal details from user ... A class must be defined for an object before ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 34
Provided by: jimand
Category:

less

Transcript and Presenter's Notes

Title: Lecture 17 Classes and Objects


1
Lecture 17Classes and Objects
  • ENGR17 Engineering Programming
  • Section 1
  • Fall 2001

11/21/01
2
Quote
This method is, to define as the number of a
class the class of all classes similar to the
given class. Bertrand Russell Principles of
Mathematics, Part II, Chapter 11, Section iii,
1903.
3
Outline
  • Create a class
  • Use the public members of a class to manipulate
    the private members
  • Create an object from a programmer-defined class
  • Create a constructor function
  • Include a value-returning function in a class

4
OOP Concepts
  • C is considered a hybrid language
  • Procedure-oriented programs (what weve studied
    so far)
  • Object-oriented programs (what well study now)
  • Object-oriented programming creates reusable
    objects
  • Every object in C is created from a class
  • Built-in classes
  • User-defined classes
  • Structures allow us to package a set of related
    data items
  • Classes allow us to package a set of related
    data items AND the functions that use the data

5
OOP Concepts and Terms
  • Class
  • Is a pattern for creating one or more instances
    of the classone or more objects
  • Encapsulates all of an objects attributes and
    behaviors
  • Attributes
  • Are characteristics that describe the object
  • Behaviors
  • Are operations (actions) that the object either
    can perform or have performed on it
  • Objects created from a class are referred to as
    instances of the class

6
OOP Concepts and Terms
  • Abstraction (data hiding)
  • Refers to hiding a classs internal details from
    user
  • Helps prevent user from making inadvertent
    changes to the object
  • Attributes and behaviors that are not hidden are
    said to be exposed to the user
  • Limited to attributes and behaviors needed by
    user to use the object
  • Public section
  • Contains the attributes and behaviors you want to
    expose
  • Allows program to access the private members
  • Private section
  • Contains the attributes (variables) and behaviors
    (functions) that you want to hide from the user

7
Defining a Class in C
  • A class must be defined for an object before you
    can create an object in C
  • Use the class statement to define a class

8
Defining a Class in C
  • Data members
  • Are the attributes included in the class
  • Are typically listed in the private section of
    the class
  • Member functions
  • Are the functions included in the class
  • Are typically listed in the public section of the
    class

9
Defining a date Class
  • Determine the attributes and behaviors
  • Use the class statement to create a date class

10
Creating a Header File
  • Enter the class statement in a programmer-defined
    header file
  • Choose File ? New ? C/C Header File
  • Give the header file the same name as the class
  • Be sure to put a include your_class.h at the
    above your main function
  • Example of how to create a header file

11
Creating a Header File
12
Defining the setDate Function
13
Defining the displayDate Function
14
Using a Class to Create an Object
15
Using a Class to Create an Object
Sample screen output
Enter the month 7 Enter the day 3 Enter the
year 99 The employee was hired on 7/3/99. Press
any key to continue
16
Constructor Function
  • Member function that C calls automatically each
    time the class instantiates (creates) and object
  • Has the sole purpose of initializing the class
    variables
  • Has the same name as the class
  • Does not have a data type because it cannot
    return a value

17
Constructor Function
18
Constructor Function
19
Creating the rectangle Class
  • Identify the objects attributes and behaviors in
    a class

20
Creating the rectangle Class
  • Enter the class definition into a header file

21
Creating the rectangle Class
  • Enter the function definitions for the member
    functions
  • A member function is needed for each behavior
    listed in the class
  • Member functions can be either void or
    value-returning functions

22
Creating the rectangle Class
Constructor
23
Creating the rectangle Class
24
Example Program
  • Write a program using the rectangle class to
    calculate the area of a yard and the cost to
    install sod
  • Input
  • Length of rectangle (in feet)
  • Width of rectangle (in feet)
  • Price of square yard of sod ()
  • Output
  • Area (in square yards)
  • Total Price ()

25
Example Program
Continued
26
Example Program
Continued
27
Summary
  • Create a programmer-defined class
  • Use the class to instantiate (create) an object
  • Create a constructor function that automatically
    initializes the variables in a class
  • Use value-returning functions in a class

28
Example
  • Write a program using the rectangle class to
    calculate the perimeter of a yard and the cost to
    install a fence around it.
  • Input
  • Length of rectangle (in feet)
  • Width of rectangle (in feet)
  • Cost of fence per foot ()
  • Output
  • Perimeter (in feet)
  • Total Price ()

29
Code (1)
Continued
30
Code (2)
Continued
31
Example
  • Write the class statistics that calculates the
    statistics of an array of integers.
  • Behaviors
  • Constructor Initialize array to all zeros
  • AddMember Add a new value to end of array
  • Average Return the average of the array
  • High Return the max element of the array
  • Attributes???

32
Code(1)
  • // statistics.h
  • class statistics
  • public
  • statistics()
  • void AddMember(int mem)
  • int Average()
  • int High()
  • private
  • int nums10
  • int size
  • statisticsstatistics()
  • int i
  • size 0
  • for (i0 ilt10 i)
  • numsi 0

33
Code(2)
  • void statisticsAddMember(int mem)
  • numssize mem
  • size
  • int statisticsAverage()
  • int i
  • int total0
  • for (i0 iltsize i)
  • total numsi
  • return total/i
  • int statisticsHigh()
  • int i
  • int high
Write a Comment
User Comments (0)
About PowerShow.com