An Introduction to Programming with C Fifth Edition




















- Slides: 20

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

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 2

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 3

Concept Lesson • Object-Oriented Programming • Defining a Class in C++ • Instantiating an Object An Introduction to Programming with C++, Fifth Edition 4

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 5

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 6

Object-Oriented Programming (continued) • Abstraction means hiding the internal details of an object from the user APIE – 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 An Introduction to Programming with C++, Fifth Edition 7

Object-Oriented Programming (continued) An Introduction to Programming with C++, Fifth Edition 8

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. Formatted. Date – (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 9

Defining a Class in C++ (continued) An Introduction to Programming with C++, Fifth Edition 10

Defining a Class in C++ (continued) An Introduction to Programming with C++, Fifth Edition 11

Instantiating an Object • After an object has been instantiated, refer to a public member using object. Name. public. Member – report. Date. set. Date(month, day, year) An Introduction to Programming with C++, Fifth Edition 12

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 13

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 14

Example 3—Using a Class that Contains Two Constructors • A Month. Day object has two attributes 1. A month number 2. A day number • A Month. Day object has three behaviors 1. Initialize its attributes using values provided by the class 2. Initialize its attributes using values provided by the program in which it is instantiated 3. Return its month number and day number attributes, separated by a slash An Introduction to Programming with C++, Fifth Edition 15

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 parameter. List is called the method’s signature An Introduction to Programming with C++, Fifth Edition 16

Example 4—Using a Class that Contains Overloaded Methods • A Gross. Pay object has two behaviors 1. 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 parameter. Lists, they are overloaded An Introduction to Programming with C++, Fifth Edition 17

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: class. Name object. Name; An Introduction to Programming with C++, Fifth Edition 18

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 19

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 set. Dimensions() method, which can accept two integers rather than two double numbers • • Lab 14. 4: Desk-Check Lab 14. 5: Debugging Lab An Introduction to Programming with C++, Fifth Edition 20