Title: Lecture 17 Classes and Objects
1Lecture 17Classes and Objects
- ENGR17 Engineering Programming
- Section 1
- Fall 2001
11/21/01
2Quote
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.
3Outline
- 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
4OOP 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
5OOP 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
6OOP 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
7Defining 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
8Defining 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
9Defining a date Class
- Determine the attributes and behaviors
- Use the class statement to create a date class
10Creating 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
11Creating a Header File
12Defining the setDate Function
13Defining the displayDate Function
14Using a Class to Create an Object
15Using 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
16Constructor 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
17Constructor Function
18Constructor Function
19Creating the rectangle Class
- Identify the objects attributes and behaviors in
a class
20Creating the rectangle Class
- Enter the class definition into a header file
21Creating 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
22Creating the rectangle Class
Constructor
23Creating the rectangle Class
24Example 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 ()
25Example Program
Continued
26Example Program
Continued
27Summary
- 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
28Example
- 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 ()
29Code (1)
Continued
30Code (2)
Continued
31Example
- 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???
32Code(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
33Code(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