20
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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

Page 1: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

An Introduction to Programming with C++

Fifth Edition

Chapter 14Classes and Objects

Page 2: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes 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

Page 3: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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

Page 4: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

An Introduction to Programming with C++, Fifth Edition 4

Concept Lesson

• Object-Oriented Programming• Defining a Class in C++• Instantiating an Object

Page 5: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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

Page 6: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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

Page 7: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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

Page 8: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

An Introduction to Programming with C++, Fifth Edition 8

Object-Oriented Programming (continued)

Page 9: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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

Page 10: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

An Introduction to Programming with C++, Fifth Edition 10

Defining a Class in C++ (continued)

Page 11: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

An Introduction to Programming with C++, Fifth Edition 11

Defining a Class in C++ (continued)

Page 12: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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)

Page 13: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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

Page 14: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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()

Page 15: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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

Page 16: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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

Page 17: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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

Page 18: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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;

Page 19: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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

Page 20: An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects

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