Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects."— Presentation transcript:

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

2 An Introduction to Programming with C++, Fifth Edition2 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

3 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

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

5 An Introduction to Programming with C++, Fifth Edition5 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

6 An Introduction to Programming with C++, Fifth Edition6 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

7 An Introduction to Programming with C++, Fifth Edition7 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 APIE

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

9 An Introduction to Programming with C++, Fifth Edition9 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

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

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

12 An Introduction to Programming with C++, Fifth Edition12 Instantiating an Object After an object has been instantiated, refer to a public member using objectName.publicMember –reportDate.setDate(month, day, year)

13 An Introduction to Programming with C++, Fifth Edition13 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 indicates that header file is located in the folder that contains the C++ Standard Library header files

14 An Introduction to Programming with C++, Fifth Edition14 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()

15 An Introduction to Programming with C++, Fifth Edition15 Example 3—Using a Class that Contains Two Constructors A MonthDay object has two attributes 1.A month number 2.A day number A MonthDay 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

16 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 Edition16

17 An Introduction to Programming with C++, Fifth Edition17 Example 4—Using a Class that Contains Overloaded Methods A GrossPay 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 parameterLists, they are overloaded

18 An Introduction to Programming with C++, Fifth Edition18 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;

19 An Introduction to Programming with C++, Fifth Edition19 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

20 An Introduction to Programming with C++, Fifth Edition20 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


Download ppt "An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects."

Similar presentations


Ads by Google