Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.

Slides:



Advertisements
Similar presentations
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Advertisements

Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
C++ Classes & Data Abstraction
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
 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.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
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.
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.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
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.
Chapter 10 Introduction to 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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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?
 2008 Pearson Education, Inc. All rights reserved 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.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
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.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Structures and Classes
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Abstract Data Types Programmer-created data types that specify
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
Object Based Programming
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
Classes and Data Abstraction
Learning Objectives Classes Constructors Principles of OOP
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Classes C++ representation of an object
Chapter 9 Introduction To Classes
Classes and Objects Systems Programming.
(4 – 2) Introduction to Classes in C++
Presentation transcript:

Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker2 Procedural Programming Design Collection of functions that operate on another collection of data variables Function calls are done to perform a task and the names are usually verbs Functions have long lists of parameters that must be passed in various ways Functions are often involved and difficult to modify

Copyright © 2002 W. A. Tucker3 Object Oriented Design Design methodology combines data and functions that operate on that data into a class, whose name is usually a noun The data elements represent the attributes of the class The functions (methods) represent the behavior of the class. Their names are usually verbs. Combining behavior and attributes together is called encapsulation

Copyright © Jade Lindquist4 Encapsulation data attributes public member functions

Copyright © 2002 W. A. Tucker5 Users of a Class Only need to know how to interface to the class Do not need to know the inner workings of the class. Are often restricted from being able to directly access or directly modify the data attributes Specific implementation details are hidden from the user. (Abstraction)

Copyright © 2002 W. A. Tucker6 Temperature Class Example Temperature is a noun –Style would have us capitalize the first letter Attributes – (data values) –The temperature in degrees –The temperature scale (F or C) We want the data elements hidden so that the user may not directly modify them –This is done by classifying the data elements as private, which allows only the member functions of the class to directly access the values

Copyright © 2002 W. A. Tucker7 Class Definition Syntax Reserved words are class, private and public Note that the class ends with a semicolon class Temperature { private: double temp; char scale; }; NOTE: You CAN NOT initialize values in the declaration statements in a class definition because no memory has been allocated at this point.

Copyright © 2002 W. A. Tucker8 Class Behavior (Functions) Class functions fall into several categories –Accessor Functions (functions to access (retrieve) the private data elements) –Modification Functions (functions to modify the private data elements) –Constructor Functions (functions to allow for initialization of the private data elements) –Friend Functions (covered in later courses) –Destructor Functions (covered later) –Utility Functions (covered in later courses) The functions may be classified as public or private. Public functions may be called by functions that are not part of the class. Private functions may only be called from other member functions of the same class.

Copyright © 2002 W. A. Tucker9 Class Definition Syntax class Temperature { private: int temp; char scale; public: void setTemp (double);// Modification Function void setScale (char);// Modification Function void convertTemp();// Modification Function double getTemp();// Accessor Function char getScale();// Accessor Function };

Copyright © 2002 Jade Lindquist10 Temperature Class double temp; char scale; double getTemp(); void setTemp(double); char getScale(); void setScale(char); void convertTemp();

Copyright © 2002 W. A. Tucker11 Declaring an Object of a Class A class is considered a “user defined data type”, thus the following declaration statement would be valid in a main function Temperature refrigerator; OOP terminology –The declaration statement creates refrigerator as an object of class Temperature –The declaration statement creates an instance of class Temperature called refrigerator –The declaration statement instantiates the object refrigerator of class Temperature

Copyright © 2002 W. A. Tucker12 Invoking Behaviors To call a member function of a class you need to use the dot member operator (a single period between the object name and the function name) Temperature refrigerator; refrigerator.setTemp(35); refrigerator.setScale(‘F’);

Copyright © 2002 W. A. Tucker13 Member Function Implementation Each one of the member functions must be defined with a complete function definition. Since the function definitions will be in a separate file for hiding, they must be associated with the class name by the use of the scope resolution operator (a double colon :: )

Copyright © 2002 W. A. Tucker14 Accessor Functions double Temperature::getTemp() { return temp; } char Temperature::getScale() { return scale; } NOTE: Member functions may directly access private data elements

Copyright © 2002 W. A. Tucker15 Modification Functions void Temperature::setTemp(double t) { temp = t; } void Temperature::setScale(char s) { scale = s; } NOTE: Member functions may directly access private data elements

Copyright © 2002 W. A. Tucker16 Modification Functions (cont) void Temperature::convertTemp() { if (scale == 'C') { scale = 'F'; temp = 9.0 * temp / ; } else if (scale == 'F') { scale = 'C'; temp = 5.0 *(temp - 32) / 9.0; }

Copyright © 2002 W. A. Tucker17 Constructor Functions Constructor functions are used to provide the capability to initialize objects This provides the capability to optionally initialize the objects when they are instantiated (created) Constructor functions are automatically called when the object is instantiated Constructor functions are often overloaded, especially useful when no arguments are given Temperature oven; Temperature refrigerator (32, ‘F’);

Copyright © 2002 W. A. Tucker18 More on Constructors Constructor functions have the exact same name as the name of the class. Constructor functions are neither void nor value returning. They are automatically called when the object is instantiated and no return type is appropriate. Default parameters may also be used with constructor functions.

Copyright © 2002 W. A. Tucker19 Syntax of Constructor Functions Temperature:: Temperature() // Default Constructor – NO parameters { temp = 0; scale = 'C'; } Temperature::Temperature(double t, char s) { temp = t; scale = s; } NOTE: NO RETURN TYPE is specified (void or datatype) since the constructor functions are called automatically when the objects are instantiated and no return value is appropriate

Copyright © 2002 W. A. Tucker20 Destructor Function Is also neither void nor value returning. Is automatically called to free the memory used by the instance when the class object goes out of scope Is named the same as the Class, but with a tilde (~) in front of the Class name At this point, we can let the compiler take care of it automatically. The syntax in the class definition would be ~Temperature();

Copyright © 2002 W. A. Tucker21 Implementation of a Class To ensure information hiding, a class is implemented with three different C++ files Class definition file Temperature.h Class implementation file Temperature.cpp Client / driver program (test code) tempTest.cpp