Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Starting Out with C++: Early Objects 5th Edition
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Chapter 7: Introduction to Classes and Objects
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
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 Procedural and Object-Oriented Programming 13.1.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
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 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 7: Introduction.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
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 7: Introduction to Classes and Objects Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Structured Data and Classes Chapter 7. Combining Data into Structures Structure: C++ construct that allows multiple variables to be grouped together Structure.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
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.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7. 12: Structures Starting Out with C++ Early Objects Eighth.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Chapter 7: Introduction to Classes and Objects
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
11 Chapter Structured Data
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Chapter 10 Defining Classes. Chapter 10 Defining Classes.
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
Chapter 7: Introduction to Classes and Objects
Chapter 7: Introduction to Classes and Objects
Introduction to Object-oriented Programming
Chapter 10 Defining Classes. Chapter 10 Defining Classes.
Introduction to Classes
Chapter 9 Objects and Classes
Classes C++ representation of an object
Standard Version of Starting Out with C++, 4th Edition
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Topics 7.1 Abstract Data Types 7.2 Object-Oriented Programming 7.3 Introduction to Classes 7.4 Introduction to Objects 7.5 Defining Member Functions 7.6 Constructors 7.7 Destructors 7.8 Private Member Functions 7-2

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Topics (Continued) 7.9 Passing Objects to Functions 7.10 Object Composition 7.11 Separating Class Specification, Implementation, and Client Code 7.15 Introduction to Object-Oriented Analysis and Design 7-3

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7.1 Abstract Data Types Programmer-created data types that specify –legal values that can be stored –operations that can be done on the values The user of an abstract data type (ADT) does not need to know any implementation details (e.g., how the data is stored or how the operations on it are carried out) 7-4

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Abstraction and Data Types Abstraction: a definition that captures general characteristics without details –An abstract triangle is a 3-sided polygon. A specific triangle may be scalene, isosceles, or equilateral Data Type: defines the kind of values that can be stored and the operations that can be performed on it 7-5

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7.2 Object-Oriented Programming Procedural programming uses variables to store data, focuses on the processes/ functions that occur in a program. Data and functions are separate and distinct. Object-oriented programming is based on objects that encapsulate the data and the functions that operate on it. 7-6

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Object-Oriented Programming Terminology Object: software entity that combines data and functions that act on the data in a single unit Attributes: the data items of an object, stored in member variables Member functions (methods): procedures/ functions that act on the attributes of the class 7-7

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More Object-Oriented Programming Terminology Data hiding: restricting access to certain members of an object. The intent is to allow only member functions to directly access and modify the object’s data Encapsulation: the bundling of an object’s data and procedures into a single entity 7-8

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Object Example 7-9 Member variables (attributes) int side; Member functions void setSide(int s) { side = s; } int getSide() { return side; } Square Square object’s data item: side Square object’s functions: setSide - set the size of the side of the square, getSide - return the size of the side of the square

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7.3 Introduction to Classes Class: a programmer-defined datatype used to define objects It is a pattern for creating objects Class declaration format: class className { declaration; }; 7-10 Notice the required ;

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Access Specifiers Used to control access to members of the class. Each member is declared to be either public: can be accessed by functions outside of the class or private: can only be called by or accessed by functions that are members of the class 7-11

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Class Example class Square { private: int side; public: void setSide(int s) { side = s; } int getSide() { return side; } }; 7-12 Access specifiers

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More on Access Specifiers Can be listed in any order in a class Can appear multiple times in a class If not specified, the default is private 7-13

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7.4 Introduction to Objects An object is an instance of a class Defined just like other variables Square sq1, sq2; Can access members using dot operator sq1.setSide(5); cout << sq1.getSide(); 7-14

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Types of Member Functions Acessor, get, getter function: retrieves but does not modify a member variable ex: getSide Mutator, set, setter function: modifies a member variable ex: setSide 7-15

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7.5 Defining Member Functions Member functions are part of a class declaration Can place entire function definition inside the class declaration or Can place just the prototype inside the class declaration and write the function definition after the class or in separate file. 7-16

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Defining Member Functions After the Class Declaration Put a function prototype in the class declaration In the function definition, precede function name with class name and scope resolution operator ( :: ) (function “belongs to class” ) int Square::getSide() { return side; } 7-17

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Defining Member Functions Inside the Class Declaration Member functions defined inside the class declaration are called inline functions Only very short functions, like the one below, should be inline functions int getSide() { return side; } 7-18

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inline Member Function Example class Square { private: int side; public: void setSide(int s) { side = s; } int getSide() { return side; } }; 7-19 inline functions

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Conventions and a Suggestion Conventions: Member variables are usually private Accessor and mutator functions are usually public Use ‘get’ in the name of accessor functions, ‘set’ in the name of mutator functions 7-20

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7.6 Constructors A constructor is a member function that is used to initialize data members of a class Is called automatically when an object of the class is created Must be a public member function Must be named the same as the class Must have no return type 7-21

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Constructor – 2 Examples Inline: class Square {... public: Square(int s) { side = s; }... }; Declaration outside the class: Square(int); //prototype //in class Square::Square(int s) { side = s; } 7-22

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley A class can have more than 1 constructor Overloaded constructors in a class must have different parameter lists Square(); Square(int); 7-23

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Default Constructor Constructors can have any number of parameters, including none A default constructor is one that takes no arguments either due to –No parameters or –All parameters have default values If a class has any programmer-defined constructors, it must also have a default constructor 7-24

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Default Constructor Example class Square { private: int side; public: Square() // default { side = 1; } // constructor... }; 7-25 Has no parameters

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Another Default Constructor Example class Square { private: int side; public: Square(int s = 1) // default { side = s; } // constructor... }; 7-26 Has parameter but it has a default value

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Invoking a Constructor To create an object using the default constructor, use no argument list and no () Square square1; To create an object using a constructor that has parameters, include an argument list Square square1(8); 7-27

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7.7 Destructors Public member function automatically called when an object is destroyed Destructor name is ~ className, e.g., ~Square Has no return type Takes no arguments Only 1 destructor is allowed per class (i.e., it cannot be overloaded) 7-28

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7. 8 Private Member Functions A private member function can only be called by another member function of the same class It is used for internal processing by the class, not for use outside of the class 7-29

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7.9 Passing Objects to Functions A class object can be passed as an argument to a function When passed by value, function makes a local copy of object. Original object in calling environment is unaffected by actions in function When passed by reference, the function can use ‘set’ functions to modify the object. 7-30

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-31 Notes on Passing Objects Using a value parameter for an object can slow down a program and waste space Using a reference parameter speeds up program, but allows the function to modify data in the structure To save space and time, while protecting structure data that should not be changed, use a const reference parameter void showData(const Square &s) // header

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-32 Returning an Object Example Square initSquare() { Square s; // local variable int inputSize; cout << "Enter the length of side: "; cin >> inputSize; s.setSide(inputSize); return s; }

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7.11 Separating Class Specification, Implementation, and Client Code Separating class declaration, member function definitions, and the (client) program that uses the class into separate files is considered good design 7-33

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using Separate Files Place class declaration in a header file that serves as the class specification file ( classname.h ) Place member function definitions in a class implementation file. ( classname.h ). Use #include “classname.h” A client program using the class must use #include “classname.h” and be compiled and linked with the class implementation file. g++ client.cpp classname.cpp 7-34

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Include Guards Prevents a header file from being included twice Format: #ifndef symbol_name #define symbol_name... (normal contents of header file) #endif symbol_name is usually the name of the header file, in all capital letters: #ifndef SQUARE_H #define SQUARE_H... #endif 7-35

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda