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

Slides:



Advertisements
Similar presentations
Chapter 7: Sub and Function Procedures
Advertisements

Overview1 History of Programming Languages n Machine languages n Assembly languages n High-level languages – Procedure-oriented – Object-oriented/Event-driven.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
An Introduction to Programming with C++ Fifth Edition
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Microsoft Visual Basic 2005: Reloaded Second Edition
An Object-Oriented Approach to Programming Logic and Design
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
1.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Chapter 11 An introduction to object-oriented design.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Classes (Part 1) Lecture 3
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Using Methods, Classes, and Objects
More about OOP and ADTs Classes
IFS410: Advanced Analysis and Design
Programming with Microsoft Visual Basic 2008 Fourth Edition
Object-Oriented Programming
More about OOP and ADTs Classes
Week 3 Object-based Programming: Classes and Objects
Object-Oriented Programming
Chapter 11: Inheritance and Composition
CIS16 Application Development and Programming using Visual Basic.net
Object-Oriented Programming
Chapter 9 Introduction To Classes
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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;

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

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