1 Chapter 9 Introduction to Classes and Objects Program Development and Design Using C++, Third Edition.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

C++ Classes & Data Abstraction
Starting Out with C++, 3 rd Edition 1 Chapter 1. Introduction to Computers and Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Lecture 9 Concepts of Programming Languages
ASP.NET Programming with C# and SQL Server First Edition
1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Object Oriented Software Development
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Introduction to Object-oriented programming and software development Lecture 1.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
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
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 9 Classes: A Deeper Look, Part I Part II.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Class 3 (L33) u Client of a Class u Purpose of Public Members u Private Class Members u Controlling Access u Access Functions u Predicate Functions u Utility.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Classes, Interfaces and Packages
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
CompSci 230 S Programming Techniques
Chapter 16: Classes and Data Abstraction
Topic 7 Introduction to Classes and Objects
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Object-Oriented Programming
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
IFS410: Advanced Analysis and Design
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

1 Chapter 9 Introduction to Classes and Objects Program Development and Design Using C++, Third Edition

2 Objectives (Chapter 8 in textbook) You should be able to describe: Abstract Data Types in C++ (Classes) Constructors Problem Solving

3 Objects An object is simply a self-contained entity that has an existence independent of other entities a desk the chair that you are sitting on pens and pencils a book trains buildings paper cups

4 Classes and Objects A class can be expressed as a general form of an object, e.g.: The Golden Gate belongs to the class of bridges; The Sydney Opera House belongs to the class of buildings;

5 Abstract Data Types in C++ (Classes) Object creation capability was provided by extending procedural language C  Extensions became C++ Permits programmer to use and create objects Abstract data type: User-defined data type  Central to creation of objects

6 Abstract Data Types Data type: Combination of data and associated operations Abstract data type: User-defined type  Defines type of data and operations that can be performed on it  Required to create objects more complex than simple integers and characters

7 Abstract Data Types (continued) Once data type developed, programmers need never be concerned with how objects of data type stored or how operations are performed  Need to know what operations do and how to invoke them Abstract data type referred to as a class

8 Class Construction Class defines both data and functions  Construct class in two parts: Declaration section Implementation section Class members: Variables and functions listed in class declaration section Data members: The variables  Instance variables Member methods: The functions

9 Attributes and Operations Properties of the class Employee: Attributes (Data members) Operations (member functions) NameHire Employee numberPromote Job titleGive pay rise SalaryFire Length of employment

10 Object type and Object instance Object type (or class) is a generic description or definition of objects Object instance (or simply object) is a specific individual object of an object type. Every object is an instance of exactly one object type.

11 Object type and Object instance (ctd) We will use the expression “define an object” to mean “define a new object type” or “define a new class” We will use the expression “declare an object” to mean “create a new object instance” An object type (class) must be defined before creating any object instance of that type

12 Object type and Object instance (ctd) Think of the housing construction as an analogy: Defining an object (class) is equivalent to making the blueprint for a house. Just as having the blueprint does not imply the existence of an actual house, defining an object does not imply the existence of any actual object instance.

13 A C++ Class Declaration class Name { public: // Public members go here private: // Private members go here };

14 Public and Private components An object definition contains two basic components: private and public. Everything that is declared in the public section is accessible to the users of an object Everything that is declared in the private section is inaccessible to the users of an object. They can only be accessed by member functions. Default access for members of a class is private.

15 The primary purpose of public members is to present to the class’s clients a view of the services (behaviors) the class provides. This set of services forms the public interface of the class. Clients of the class need not be concerned with how the class accomplishes its tasks. The private members of the class (as well as the definitions of public member functions) are not accessible to the clients of the class. These components form the implementation of the class. Public and Private components

16 The class Time class Time { public: Time(); void setTime(int, int, int); void print24hour(); void print12hour(); private: int hour; int minute; int second; };

17 More on private members Private data does not necessarily mean that clients cannot change these data. Private data can be changed by member functions of that class. Access to these data is carefully controlled by the use of member functions. (e.g. member function setTime modifies the private data hour, minute, second)

18 This modification seems to violate the notion of private data…. BUT a set function (such as setTime) can provide data validation capabilities (such as range checking) to ensure that the value is set properly. (It can also translate between the form of the data used in the interface and the form used in the implementation). Similarly for the print24hour function (it edits the data to be viewed by the client). More on private members

19 How to decide what is made public and what is made private Keep all the data members of the class private. Provide public member functions to set and get the values of private data members. This architecture helps hide the implementation of a class from its clients, which reduces bugs and improves program modifiability. (Not all member functions need be made public. Some member functions may remain private and serve as utility functions (also called helper functions) to other functions of the class.

20 Member functions of class Time void Time::setTime(int h, int m, int s) { if ((h = 0)) hour = h; if ((m = 0)) minute = m; if ((s = 0)) second = s; }

21 Member functions of class Time (ctd) void Time::print24hour() { cout<<hour<<":"<<minute<<":"<<second; } void Time::print12hour() { if ((hour==0) || (hour==12)) cout<<12<<":"; else cout<<hour%12<<":"; cout<<minute<<":"<<second; if (hour<12) cout<<" AM"; else cout <<" PM"; }

22 Creating and using objects void main() { Time t; t.setTime(12,0,0); cout << "The time in 24 hour format is: "; t.print24hour(); cout << endl; cout << "The time in 12 hour format is: "; t.print12hour(); cout << endl; }

23 Constructors Any function with same name as its class  Initialize new object’s data members May also perform other tasks Multiple constructors can be defined for each class  Distinguishable by number and types of parameters No return type (not even void)

24 Constructors (ctd’) // Constructor simply initialises // to zero Time::Time() { hour = 0; minute = 0; second = 0; }

25 Constructors with parameters // Constructor with parameters Time::Time(int h, int m, int s) { hour = h; minute = m; second = s; }

26 Creating an object with a parameterized constructor // t is an object of class Time // and is initialised to midday Time t(12,0,0);

27 The complete program #include using namespace std; class Time { public: Time (); Time(int, int, int); void setTime(int, int, int); void print24hour(); void print12hour(); private: int hour; int minute; int second; }; Time::Time() //constructor without parameters { hour=0; minute=0; second=0; }

28 Time::Time(int x, int y, int z) //constructor with parameters { hour=x; minute=y; second=z; } void Time::setTime(int h, int m, int s) { if ((h =0)) hour =h; if ((m =0)) minute =m; if ((s =0)) second =s; } The complete program (ctd’)

29 void Time::print24hour() { cout<<hour<<":"<<minute<<":"<<second; } void Time::print12hour() { if ((hour==0) || (hour==12)) cout<<12<<":"; else cout<<hour%12<<":"; cout<<minute<<":"<<second; if (hour<12) cout<<" AM"; else cout <<" PM"; } The complete program (ctd’)

30 void main() { Time t1; //create an object instance - contructor without parameters called Time t2(14,5,30); //create an obj. inst. - contructor with parameters called t1.print12hour(); cout<<endl; t2.print24hour(); cout<<endl; } The complete program (ctd’)