Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More about.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Class and Objects.
Chapter 14: Overloading and Templates
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
C++ data types. Structs vs. Classes C++ Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 15 – Digital Clock Application: Building Your.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Data Structures.
Operator Overloading in C++
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
1 3. More About Data Structures and ADTs: C++ Data types (Read §3.1 & §3.2) C++ types an be classified as: Fundamental (or simple or scalar): A data object.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
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.
Learners Support Publications Classes and Objects.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Brief Review.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 Classes and Data Abstraction 2 Objectives Understand encapsulation and data hiding Understand data abstraction and Abstract Data Types (ADTs) Create.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Review Dr. Yingwu Zhu. Outline ADT concept C++ Class List with class implementation.
 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 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Brief Review of ADTs and Class Implementation
A First C++ Class – a Circle
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
More about OOP and ADTs Classes
Data Structures and Abstract Data Types
Object Based Programming
Introduction to Classes
Brief Review of ADTs and Class Implementation
More about OOP and ADTs Classes
Learning Objectives Classes Constructors Principles of OOP
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
C++ types an be classified as: Fundamental (or simple or scalar):
Classes C++ representation of an object
Data Structures and ADTs
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
C++ data types.
Lecture 8 Object Oriented Programming (OOP)
Object Oriented Programming
Presentation transcript:

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about OOP and ADTs Classes Chapter 4

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Chapter Contents 4.1 Procedural vs. Object-Oriented Programming 4.2 Classes 4.3 Example: A First Version of a User- Defined Time Class 4.4 Class Constructors 4.5 Other Class Operators

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Chapter Objectives Contrast OOP with procedural programming Review classes in C++ Study in detail a specific example of how a class is built Show how operators can be overloaded for new types Show how conditional compilation directives are used to avoid redundant declarations Discuss pointers to class objects – the this pointer, in particular

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Contrast Procedural, Object Oriented Paradigms Procedural Action-oriented — concentrates on the verbs Programmers: –Identify basic tasks to solve problem –Implement actions to do tasks as subprograms (procedures/functions/ subroutines) –Group subprograms into programs/modules/libraries, –together make up a complete system for solving the problem Object-oriented Focuses on the nouns of problem specification Programmers: –Determine objects needed for problem –Determine how they should work together to solve the problem. –Create types called classes made up of data members function members to operate on the data. –Instances of a type (class) called objects.

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Structs and Classes Similarities Essentially the same syntax Both are used to model objects with multiple attributes (characteristics) –represented as data members –also called fields … or … –instance or attribute variables). Thus, both are used to process non- homogeneous data sets.

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Structs vs. Classes Differences No classes in C Members public by default Can be specified private Both structs and classes in C++ Structs can have members declared private Class members are private by default Can be specified public

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Advantages in C++ (structs and Classes) C++ structs and classes model objects which have: –Attributes represented as data members –Operations represented as functions (or methods) Leads to object oriented programming –Objects are self contained –"I can do it myself" mentality –They do not pass a parameter to an external function

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Class Declaration Syntax class ClassName { public: Declarations of public members private: Declarations of private members };

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Designing a Class Data members normally placed in private: section of a class Function members usually in public: section Typically public: section followed by private: –although not required by compiler

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Class Libraries Class declarations placed in header file –Given.h extension –Contains data items and prototypes Implementation file –Same prefix name as header file –Given.cpp extension Programs which use this class library called client programs

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Translating a Library

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Example of User-Defined Time Class Recall Time struct from previous chapter –Actions done to Time object required use of Time parameter in the functions Now we create a Time class –Actions done to Time object, done by the object itself Note interface for Time class object, Fig. 4.2 Fig. 4.2 –Data members private – inaccessible to users of the class –Information hiding

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Constructors Note constructor definition in Time.cpp example example Syntax ClassName::ClassName (parameter_list) : member_initializer_list { // body of constructor definition }

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Constructors Results of default constructor Results of explicit-value constructor

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Overloading Functions Note existence of multiple functions with the same name Time(); Time(unsigned initHours, unsigned initMinutes, char initAMPM); –Known as overloading Compiler compares numbers and types of arguments of overloaded functions –Checks the "signature" of the functions

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Default Arguments Possible to specify default values for constructor arguments Time(unsigned initHours = 12, unsigned initMinutes = 0, char initAMPM = 'A'); Consider Time t1, t2(5), t3(6,30), t4(8,15,'P');

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Copy Operations During initialization Time t = bedTime During Assignment t = midnight;

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Other Class Operations Accessors and Mutators –See "get" and "set" functionsgetset Overloading operators –Same symbol can be used more than one way –Note declaration for I/O operators >I/O operators –Note definition of overloaded I/O operatorsdefinition

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Friend Functions Note use of two functions used for output –display() and operator<<() Possible to specify operator<<() as a "friend" function –Thus given "permission" to access private data elements Declaration in.h file (but not inside class) friend ostream & operator<<( ostream & out, const Time & t)

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Friend Functions Definition in.cpp file ostream & operator<<( ostream & out, const Time & t) {out << t.myHours<<":" <<(t.myMinutes< 10? "0": "") <<t.myMinutes << ' '<<t.myAMorPM<<".M."; return out; } Note - a friend function not member function –not qualified with class name and :: –receives class object on which it operates as a parameter Author prefers not to use friend function Violates principle of information hiding Author prefers not to use friend function Violates principle of information hiding

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Other Operations Advance OperationAdvance –Time object receives a number of hours and minutes –Advances itself by adding to myHours, myMinutes Relational Operators –Time object compares itself with another –Determines if it is less than the other

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Redundant Declarations Note use of #include "Time.h" in –Time.cppTime.cpp –Client programClient program Causes "redeclaration" errors at compile time Solution is to use conditional compilation –Use #ifndef and #define and #endif compiler directives#ifndef and #define #endif

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Pointers to Class Objects Possible to declare pointers to class objects Time * timePtr = &t; Access with timePtr->getMilTime() or (*timePtr).getMilTime()

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved The this Pointer Every class has a keyword, this –a pointer whose value is the address of the object –Value of *this would be the object itself