Chapter 10 Introduction to Classes

Slides:



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

Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter Objectives You should be able to describe: Assignment Pointers as Class Members Additional Class Features Common Programming Errors.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 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.
Chapter 11: Classes and Data Abstraction
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
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.
Chapter 15: Operator Overloading
Review of C++ Programming Part II Sheng-Fang Huang.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
C++ for Engineers and Scientists Third Edition
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
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.
Copyright 2008 Oxford Consulting, Ltd 1 October C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to.
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.
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.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
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.
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.
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.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
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?
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Chapter 6 Modularity Using Functions
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
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?
Chapter 3: Using Methods, 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?
Chapter 5 Classes.
Constructor & Destructor
Constructors and destructors
Classes and Objects.
Lecture 16 Oct 30, 02.
More C++ Classes Systems Programming.
Presentation transcript:

Chapter 10 Introduction to Classes A First Book of C++ Chapter 10 Introduction to Classes

Objectives In this chapter, you will learn about: Object-Based Programming Creating Your Own Classes Constructors Using Classes with Examples Class Scope and Duration Categories Common Programming Errors Modeling Objects A First Book of C++ 4th Edition

Object-Based Programming Object: well suited to programming representation Can be specified by two basic characteristics: State: how object appears at the moment Behavior: how object reacts to external inputs Example of an object: an elevator State: size, location, interior decoration Behavior: reaction when one of its buttons is pushed A First Book of C++ 4th Edition

A Class Is a Plan Class can be considered a construction plan for objects and how they can be used Plan lists the required data items and supplies instructions for using the data Many objects can be created from the same class Each different object type requires its own class A First Book of C++ 4th Edition

From Recipe to Class Class can be considered the plan or recipe from which programming objects are created Class typically contains sections for ingredients and methods Class declaration section provides: List of data “ingredients” List of method names and data types A First Book of C++ 4th Edition

Creating Your Own Classes Class: programmer-defined data type Also called an abstract data type Combination of data and their associated operations (capabilities) A C++ class provides mechanism for packaging data structure and functions together in self-contained unit A First Book of C++ 4th Edition

Class Construction Two components: Declaration section: declares data types and function for class Implementation section: defines functions whose prototypes were declared in declaration section Class members: Data members (instance variables) Member functions A First Book of C++ 4th Edition

Class Construction (cont'd.) A First Book of C++ 4th Edition

Class Construction (cont'd.) Example of class declaration section: class Date { private: int month; int day; int year; public: Date(int = 7, int = 4, int = 2012); void setDate(int, int, int); void showDate(void); }; // this is a declaration - don't forget the semicolon A First Book of C++ 4th Edition

Class Construction (cont'd.) Declaration section of class definition Enclosed in braces Includes variables (data members) and function declarations Keywords private and public: define access rights private: class members (month, day, and year) can only be accessed using class functions Enforces data security (data hiding) public: class members (functions Date(), setDate(), showDate()) can be used outside of the class A First Book of C++ 4th Edition

Class Construction (cont'd.) Constructor method: initializes class data members with values Constructor function (Date()) has same name as the class Default values for constructor are 7, 4, 2012 Constructor function has no return type (a requirement for this special function) Two remaining member functions: setDate() and showDate() declared as returning no value (void) A First Book of C++ 4th Edition

Class Construction (cont'd.) A First Book of C++ 4th Edition

Class Construction (cont'd.) A First Book of C++ 4th Edition

Class Construction (cont'd.) Implementation section of class definition As shown in Program 10.1 Except for constructor function, which has no return data type Functions: defined the same as other C++ functions but also include scope resolution operator To identify function as member of class Implementation and declaration sections declare a class Variables of the class (objects) must still be defined A First Book of C++ 4th Edition

Class Construction (cont'd.) Example: Creation of objects of Date class in main() function of Program 10.1 int main() { Date a, b, c(4,1,2000); // declare 3 objects b.setDate(12,25,2009); // assign values to b's // data members a.showDate(); // display object a's values b.showDate(); // display object b's values c.showDate(); // display object c's values cout << endl; return 0; } A First Book of C++ 4th Edition

Class Construction (cont'd.) Description of main(): Three objects of class Date defined Constructor function: Date(), automatically called Memory allocated for each object Data members of the objects initialized Object a: no parameters assigned; therefore, defaults are used: a.month = 7 a.day = 4 a.year = 2012 A First Book of C++ 4th Edition

Class Construction (cont'd.) Description of main(): (cont'd.) Notation to create objects: objectName.attributeName Object b: no parameters assigned, same defaults used Object c: defined with arguments 4, 1, and 1998 Three arguments passed into constructor function resulting in initialization of c’s data members as: c.month = 4 c.day = 1 c.year = 2000 A First Book of C++ 4th Edition

Class Construction (cont'd.) Description of main(): (cont'd.) All functions of class Date are public; therefore: b.setDate(12, 25, 2007) is a valid statement inside main() function Calls setDate() function with arguments 12 ,25, 2009 Important distinction: data members of class Date are private The statement b.month = 12 is invalid in main() A First Book of C++ 4th Edition

Class Construction (cont'd.) Description of main(): (cont'd.) Last three statements in main() call showDate() to operate on a, b, and c objects Calls result in output displayed by Program 10.1 The date is 07/04/12 The date is 12/25/09 The date is 04/01/00 The statement cout << a; is invalid within main() cout does not know how to handle an object of class Date A First Book of C++ 4th Edition

Terminology Class: programmer-defined data type Objects (instances): created from classes Relation of objects to classes is similar to relation of variables to C++ built-in data types int a; // a is a variable of type integer Date a; // a is an object of class Date Instantiation: process of creating a new object Creates new set of data members belonging to new object: determines the object’s state A First Book of C++ 4th Edition

Terminology (cont'd.) Interface: part of class declaration section Includes: Class’s public member function declarations Supporting comments Implementation consists of: Implementation section of class definition Private member functions Public member functions Private data members from class declaration section A First Book of C++ 4th Edition

Terminology (cont'd.) Internal construction of class is not relevant to programmer who just wishes to use class Implementation can and should be hidden from all users A First Book of C++ 4th Edition

Constructors A function that has the same name as its class Multiple constructors can be defined for a class Each must be distinguishable by the number and types of its parameters If no constructor function is written, compiler assigns default constructor Purpose: initialize a new object’s data members May perform other tasks A First Book of C++ 4th Edition

Constructors (cont'd.) Format: same name as class to which it belongs Must have no return type (not even void) Default constructor: does not require arguments when called Two cases: No parameters declared As with compiler-supplied default constructor Arguments have already been given default values in valid prototype statement: Date (int = 7, int = 4, int = 2012) Declaration Date a; initializes the a object with default values: 7, 4, 2012 A First Book of C++ 4th Edition

Constructors (cont'd.) Sample class declaration: class Date { private: int month, day, year; public: void setDate(int, int, int); void showDate() }; A First Book of C++ 4th Edition

Constructors (cont'd.) No constructor has been included Compiler assigns a do-nothing default constructor equivalent to: Date (void) { } This constructor expects no parameters and has an empty body A First Book of C++ 4th Edition

Constructors (cont'd.) A First Book of C++ 4th Edition

Constructors (cont'd.) int main() { Date a; // declare an object Use of constructor in main() : Program 10.2 int main() { Date a; // declare an object Date b; // declare an object Date c (4,1,2009); // declare an object return 0; } A First Book of C++ 4th Edition

Constructors (cont'd.) Output from Program 10.2 Created a new data object with data values 7, 4, 2012 Created a new data object with data values 4, 1, 2009 A First Book of C++ 4th Edition

Calling Constructors Constructors are called whenever an object is created Date c(4,1,2009); Date c = Date(4,1,2009); C style of initialization Date c = 8; Object should never be declared with empty parentheses A First Book of C++ 4th Edition

Overloaded and Inline Constructors Constructors are called automatically each time an object is created Other methods must be called explicitly by name Constructors can: Have default arguments (as in Program 10.1) Be overloaded Be written as inline functions A First Book of C++ 4th Edition

Destructors Counterpart of constructor function Has same name as constructor Preceded with a tilde (~) Date class constructor: ~Date() Default destructor Do-nothing destructor provided by C++ compiler in absence of explicit destructor Can only be one destructor per class Destructors take no values and return no arguments A First Book of C++ 4th Edition

Arrays of Objects Declaring array of objects is the same as declaring array of C++ built-in type Example: Date theDate[5]; Creates five objects named theDate[0] through theDate[4] Member functions for theDate array objects are called using: objectName.functionName A First Book of C++ 4th Edition

Examples In the first example, you develop a class for determining the floor area of a rectangular room In the second example, you construct a single elevator object A First Book of C++ 4th Edition

Example 1: Constructing a Room Object Create class from which room type objects can be constructed Solution: One type of object: rectangular-shaped room Represented by two double precision variables: length and width Functions needed: Constructor: to specify an actual length and width value when a room is instantiated A First Book of C++ 4th Edition

Example 1: Constructing a Room Object (cont'd.) Functions needed: (cont'd.) Accessor: to display room’s length and width Mutator: to change above values Function to calculate room’s floor area from its length and width values A First Book of C++ 4th Edition

Example 2: Constructing an Elevator Object Simulate an elevator’s operation Solution: One type of object: elevator Three attributes: elevator’s number, current location, and highest floor it can reach Functions needed: Constructor: initializes the elevator’s number, starting floor position, and highest floor request(): to alter the elevator’s position A First Book of C++ 4th Edition

Class Scope and Duration Categories Scope of an identifier: defines the portion of a program where the identifier is valid Class data members are local to the class in which they’re declared Class method names are local to the class they’re declared in and can be used only by objects declared for the class A First Book of C++ 4th Edition

Class Scope and Duration Categories (cont’d.) A First Book of C++ 4th Edition

Static Class Members As each class object is created, it gets its own block of memory for its data members In some cases, it’s convenient for every created object to share the same memory location for a specific variable C++ handles this situation by declaring a class variable to be static Static class variables share the same storage space for all class objects They act as global variables for the class and provide a means of communication between objects A First Book of C++ 4th Edition

Friend Functions Friend functions Nonmember methods that are granted the same privileges as its member methods Friends list List of friend functions Series of method prototype declarations preceded with the keyword friend and included in the class declaration section A First Book of C++ 4th Edition

Common Programming Errors Failing to terminate class declaration with semicolon Including return type with constructor’s prototype Failing to include return type with other function’s prototype Using same name for data member as for member method Defining more than one default constructor for class A First Book of C++ 4th Edition

Common Programming Errors (cont'd.) Forgetting to include class name and scope operator, ::, in the header line of all member methods defined in class implementation section Using the static keyword when defining a static data member or member method Using the friend keyword when defining a friend function Failing to instantiate static data members before creating class objects that must access these data members A First Book of C++ 4th Edition

Summary Class: programmer-defined data type Class definition: includes declaration and implementation sections Class members: variables and functions Objects: same relation to class as variables have to C++ built-in data types private keyword: private class members can only be accessed by member functions A First Book of C++ 4th Edition

Summary (cont'd.) Class functions: may be written inline or defined in class implementation section Constructor function: automatically called each time an object is declared If none defined, compiler will supply default constructor Default constructor: constructor that does not require arguments One default constructor per class A First Book of C++ 4th Edition

Summary (cont'd.) Objects created using either C or C++ style of declaration: C++: Date a, b, c(12, 25, 2002) C: Date c = Date(12,25,2006) Constructors may be overloaded If constructor is defined, user-defined default constructor should be written Compiler will not provide it Destructor: called when object goes out of scope Takes no arguments, returns no value A First Book of C++ 4th Edition