Upload
belinda-greene
View
215
Download
0
Embed Size (px)
DESCRIPTION
An Introduction to Programming with C++, Fifth Edition3 Objectives (continued) Create a default constructor Create a parameterized constructor Include methods other than constructors in a class Overload the methods in a class
Citation preview
An Introduction to Programming with C++
Fifth Edition
Chapter 14Classes and Objects
An Introduction to Programming with C++, Fifth Edition 2
Objectives
• Differentiate between procedure-oriented and object-oriented programming
• Define the terms used in object-oriented programming
• Create a class definition• Instantiate an object from a class that you define
An Introduction to Programming with C++, Fifth Edition 3
Objectives (continued)
• Create a default constructor• Create a parameterized constructor• Include methods other than constructors in a class• Overload the methods in a class
An Introduction to Programming with C++, Fifth Edition 4
Concept Lesson
• Object-Oriented Programming• Defining a Class in C++• Instantiating an Object
An Introduction to Programming with C++, Fifth Edition 5
Concept Lesson (continued)
• Example 1—Using a Class that Contains Public Data Members Only
• Example 2—Using a Class that Contains a Private Data Member and Public Member Methods
• Example 3—Using a Class that Contains Two Constructors
• Example 4—Using a Class that Contains Overloaded Methods
An Introduction to Programming with C++, Fifth Edition 6
Object-Oriented Programming
• OOP stands for object-oriented programming• OOD stands for object-oriented design• Object: anything that can be seen/touched/used
– E.g., check boxes, list boxes, and buttons– Objects have attributes and behaviors
• Attributes describe the object• Behaviors are operations that the object is capable of
performing, or to which the object can respond• Class: pattern/blueprint used to create an object
– Every object is an instance of some class
An Introduction to Programming with C++, Fifth Edition 7
Object-Oriented Programming (continued)
• Abstraction means hiding the internal details of an object from the user– Attributes may be hidden or exposed to user
• Polymorphism allows the same instruction to be carried out differently depending on the object
• Inheritance means you can create one class from another class– Derived class inherits from base class
• A class encapsulates the attributes and behaviors that describe the object the class creates
AP
IE
An Introduction to Programming with C++, Fifth Edition 8
Object-Oriented Programming (continued)
An Introduction to Programming with C++, Fifth Edition 9
Defining a Class in C++
• Specify attributes and behaviors using a class definition– Declaration section contains the class statement
• Naming convention uses Pascal case– E.g. FormattedDate
– (optional) implementation section contains one definition for each prototype listed in declaration
• A method is a function defined in a class definition– Public members come below keyword public– Private members come below keyword private
An Introduction to Programming with C++, Fifth Edition 10
Defining a Class in C++ (continued)
An Introduction to Programming with C++, Fifth Edition 11
Defining a Class in C++ (continued)
An Introduction to Programming with C++, Fifth Edition 12
Instantiating an Object
• After an object has been instantiated, refer to a public member using objectName.publicMember– reportDate.setDate(month, day, year)
An Introduction to Programming with C++, Fifth Edition 13
Header Files
• Class definitions usually placed in a header file– E.g., Salesperson.h
• #include directive tells compiler to include the contents of header file in program– #include "Salesperson.h"
• " " indicates that header file is located in the same folder as the program file
– #include <iostream>• < > indicates that header file is located in the folder
that contains the C++ Standard Library header files
An Introduction to Programming with C++, Fifth Edition 14
Example 2—Using a Class that Contains a Private Data Member and Public Member Methods (continued)
• Constructor: method automatically processed when object is instantiated – Initializes class’s private variables– A class should have at least one constructor – Constructors have the same name as the class, but
formal parameters (if any) differ– A constructor that has no formal parameters is called
the default constructor• E.g., Square::Square()
An Introduction to Programming with C++, Fifth Edition 15
Example 3—Using a Class that Contains Two Constructors
• A MonthDay object has two attributes1. A month number2. A day number
• A MonthDay object has three behaviors1. Initialize its attributes using values provided by the
class2. Initialize its attributes using values provided by the
program in which it is instantiated3. Return its month number and day number
attributes, separated by a slash
Example 3—Using a Class that Contains Two Constructors (continued)
• Constructors that contain parameters are called parameterized constructors
• The method name combined with its optional parameterList is called the method’s signature
An Introduction to Programming with C++, Fifth Edition 16
An Introduction to Programming with C++, Fifth Edition 17
Example 4—Using a Class that Contains Overloaded Methods
• A GrossPay object has two behaviors1. Calculate and return the gross pay for a salaried
employee• Dividing the employee’s annual salary by 24
2. Calculate and return the gross pay for an hourly employee• Multiplying the number of hours the employee
worked during the week by his or her pay rate
• When two or more methods have the same name but different parameterLists, they are overloaded
An Introduction to Programming with C++, Fifth Edition 18
Summary
• A class is a pattern for creating instances of the class– Each instance is an object– A class encapsulates object’s attributes and behaviors
• Abstraction means hiding an object’s internal details from the user
• Polymorphism allows the same instruction to be carried out differently depending on the object
• Use a class definition to create a class• Create an object with the syntax:
className objectName;
An Introduction to Programming with C++, Fifth Edition 19
Summary (continued)
• Most C++ programmers enter class definitions in header files
• Use a constructor to initialize the data members in a class when an object is created– Class can have more than one constructor
• Name is the same, formal parameters differ
• Can overload the methods in a class– Allows you to use the same name for methods that
require different information to perform the same task
An Introduction to Programming with C++, Fifth Edition 20
Application Lesson: Using Classes and Objects in a C++ Program
• Lab 14.1: Stop and Analyze• Lab 14.2
– Program estimates cost of laying sod on a rectangular piece of land
• Lab 14.3– Modified class will contain another
setDimensions() method, which can accept two integers rather than two double numbers
• Lab 14.4: Desk-Check Lab• Lab 14.5: Debugging Lab