J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Abstract Data Types Development and Implementation.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Abstract Data Type Fraction Example
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Functions Computational assistants.
More on Classes. COMP104 ADT / Slide 2 Abstract Data Type * An Abstract Data Type is a class with some special restrictions. * These restrictions can.
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
Class and Objects.
Abstract Data Types Development and Implementation JPC and JWD © 2002 McGraw-Hill, Inc.
CS-240 Operator Overloading Dick Steflik. Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of.
C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Pointers and Dynamic Objects Mechanisms for developing flexible list representations.
Abstract Data Type. COMP104 Slide 2 Summary l A class can be used not only to combine data but also to combine data and functions into a single (compound)
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
Engineering Problem Solving With C++ An Object Based Approach Chapter 8 Introduction to Classes.
CMSC 2021 Stream I/O Operators and Friend Functions.
Programmer-defined classes Part 3. A class for representing Fractions public class Fraction { private int numerator; private int denominator; public Fraction(int.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Chapter 10 Defining Classes. User-defined Types Are data types defined by programmers. Include: – typedef: simple data definition – struct: a structure.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
Case Study - Fractions Timothy Budd Oregon State University.
CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH1, C++ INTERLUDE 1.
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 1Winter Quarter C++: I/O and Classes Lecture 25.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO , APP D, C++ BOOK.
1 CSC 222: Computer Programming II Spring 2004  classes and objects  abstract data types, classes and objects  using existing classes, #include, member.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Programming Abstract Data Types. COMP104 Lecture 30 / Slide 2 Review: class l Name one reason why we need the class construct. l Why do we need "inspector.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
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.
Andy Wang Object Oriented Programming in C++ COP 3330
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value A copy of the argument’s value.
Chapter 12 Classes and Abstraction
CSCE 210 Data Structures and Algorithms
Andy Wang Object Oriented Programming in C++ COP 3330
Object-Oriented Design (OOD) and C++
Operator Overloading Part 2
A First C++ Class – a Circle
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Chapter Structured Types, Data Abstraction and Classes
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Fraction Abstract Data Type
Classes and Data Abstraction
Operator Overloading.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
COMS 261 Computer Science I
COMS 261 Computer Science I
COP 3330 Object-oriented Programming in C++
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Development and Implementation
CS 144 Advanced C++ Programming February 21 Class Meeting
Class rational part2.
Presentation transcript:

J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Abstract Data Types Development and Implementation

Ch 9 / Foil 2 OO Programming and Design l Goal n Well-defined abstractions that allow objects to be created and used in an intuitive manner –User should not have to bother with unnecessary details l Example programming a microwave l Common practice n Use information hiding principle and encapsulation to support integrity of data l Result n Abstract Data Type or ADT

Ch 9 / Foil 3 Abstract Data Type l Consider Rational a(1,2); // a = 1/2 Rational b(2,3); // b = 2/3 cout << a << " + " << b << " = " << a + b; Rational s; // s = 0/1 Rational // t = 0/1 cin >> s >> t; cout << s << " * " << t << " = " << s * t; l Observation n Natural look that is analogous to fundamental-type arithmetic objects

Ch 9 / Foil 4 n Multiplication n Division Rational Number Review l Rational number n Ratio of two integers: a/b –Numerator over the denominator l Standard operations n Addition n Subtraction

Ch 9 / Foil 5 Rational Number Representation l Requirements n Represent a numerator and denominator –Implies in part a class representation with two int data members l NumeratorValue and DenominatorValue l Data members private to support information hiding n Public arithmetic behaviors (member functions) –Rational addition, subtraction, multiplication and division n Public relational behaviors –Equality and less than comparisons l Practice rule of class minimality

Ch 9 / Foil 6 Rational Number Representation l Other requirements n Public object behaviors –Construction l Default construction -- 0/1 -- design decision l Specific numerator and denominator construction l Copy construction (provided automatically) –Assignment (provided automatically) –Value insertion and extraction n Non-public object behaviors –Inspection and mutation of data members Clients deal with a Rational object!

Ch 9 / Foil 7 Rational Number Representation l Other requirements n Auxiliary operations (necessarily public) –Arithmetic, relational, insertion, and extraction operations l Provides the natural form we expect n Class definition provides a functional form that auxiliary operators use l Provides commutativity consistency For C++ reasons 1 + r and r + 1 would not be treated the same if addition was a member operation

Ch 9/ Foil 8

Ch 9 / Foil 9 #include "rational.h" Rational r; Rational s; cout << "Enter two rationals(a/b): "; cin >> r >> s; Rational t(r); Rational Sum = r + s; Rational Product = r * s; cout << r << " + " << s << " = " << Sum; cout << r << " * " << s << " = " << Product; Consider

Ch 9 / Foil 10 Implementation Components l Header file n Define class and prototype library functions –rational.h l Rational class implementation n Define member functions –rational.cpp l Auxiliary function implementations n Define assisting functions that provide expected but non- member capabilities –rational.cpp

Ch 9 / Foil 11 Rational ADT Header File l File layout n Class definition and library prototypes nested within preprocessor statements –Ensures one inclusion per translation unit n Class definition proceeds library prototypes #ifndef RATIONAL_H #define RATIONAL_H class Rational { // … } // library prototypes … #endif

Ch 9 / Foil 12 Class Rational Overview class Rational { // from rational.h public: // for everybody including clients protected: // for Rational member functions and for // member functions from classes derived // from rational private: // for Rational member functions } ; // auxiliary prototyping

Ch 9 / Foil 13 Rational Public Section public: // default constructor Rational(); // specific constructor Rational(int numer, int denom = 1); // arithmetic facilitators Rational Add(const Rational &r) const; Rational Multiply(const Rational &r) const; // stream facilitators void Insert(ostream &sout) const; void Extract(istream &sin);

Ch 9 / Foil 14 Rational Protected Section protected: // inspectors int GetNumerator() const; int GetDenominator() const; // mutators void SetNumerator(int numer); void SetDenominator(int denom);

Ch 9 / Foil 15 Rational Private Section private: // data members int NumeratorValue; int DenominatorValue;

Ch 9 / Foil 16 // after the class definition in rational.h Rational operator+( const Rational &r, const Rational &s); Rational operator*( const Rational &r, const Rational &s); ostream& operator<<( ostream &sout, const Rational &s); istream& operator>>(istream &sin, Rational &r); Auxiliary Operator Prototyping

Ch 9 / Foil 17 Auxiliary Operator Importance Rational r; Rational s; r.Extract(cin); s.Extract(cin); Rational t = r.Add(s); t.Insert(cout); Rational r; Rational s; cin >> r; cin >> s; Rational t = r + s; cout << t; l Natural look l Should << be a member? n Consider r << cout;

Ch 9 / Foil 18 Const Power const Rational OneHalf(1,2); cout << OneHalf; // legal cin >> OneHalf; // illegal

Ch 9 / Foil 19 Rational ADT Implementation #include // Start of rational.cpp #include using namespace std; #include "rational.h" // default constructor Rational::Rational() { SetNumerator(0); SetDenominator(1); } l Example Rational r; // r = 0/1 Is this necessary? Which objects are being referenced?

Ch 9 / Foil 20 Remember l Every class object n Has its own data members n Has its own member functions –When a member function accesses a data member l By default the function accesses the data member of the object to which it belongs! n No special notation needed l Auxiliary functions n Are not class members n To access a public member of an object, an auxiliary function must use the dot operator on the desired object object.member

Ch 9 / Foil 21 Specific Constructor // (numer, denom) constructor Rational::Rational(int numer, int denom) { SetNumerator(numer); SetDenominator(denom); } l Example Rational u(2); // u = 2/1 (why?) Rational t(2,3); // t = 2/3 // we’ll be using t in // future examples

Ch 9 / Foil 22 Inspectors int Rational::GetNumerator() const { return NumeratorValue; } int Rational::GetDenominator() const { return DenominatorValue; } l Where are the following legal? int a = GetNumerator(); int b = t.GetNumerator(); Which object is being referenced? Why the const?

Ch 9 / Foil 23 Numerator Mutator void Rational::SetNumerator(int numer) { NumeratorValue = numer; } l Where are the following legal? SetNumerator(1); t.SetNumerator(2); Why no const?

Ch 9 / Foil 24 Denominator Mutator void Rational::SetDenominator(int denom) { if (denom != 0) { DenominatorValue = denom; } else { cerr << "Illegal denominator: " << denom << "using 1" << endl; DenominatorValue = 1; } l Example SetDenominator(5);

Ch 9 / Foil 25 Addition Facilitator Rational Rational::Add(const Rational &r) const { int a = GetNumerator(); int b = GetDenominator(); int c = r.GetNumerator(); int d = r.GetDenominator(); return Rational(a*d + b*c, b*d); } l Example cout << t.Add(u);

Ch 9 / Foil 26 Multiplication Facilitator Rational Rational::Multiply(const Rational &r) const { int a = GetNumerator(); int b = GetDenominator(); int c = r.GetNumerator(); int d = r.GetDenominator(); return Rational(a*c, b*d); } l Example t.Multiply(u);

Ch 9 / Foil 27 Insertion Facilitator void Rational::Insert(ostream &sout) const { sout << GetNumerator() << '/' << GetDenominator(); return; } l Example t.Insert(cout); Why is sout a reference parameter?

Ch 9 / Foil 28 Basic Extraction Facilitator void Rational::Extract(istream &sin) { int numer; int denom; char slash; sin >> numer >> slash >> denom; assert(slash == '/'); SetNumerator(numer); SetDenominator(denom); return; } l Example t.Extract(cin);

Ch 9 / Foil 29 Auxiliary Arithmetic Operators Rational operator+( const Rational &r, const Rational &s) { return r.Add(s); } Rational operator*( const Rational &r, const Rational &s) { return r.Multiply(s); } l Example cout << (t + t) * t;

Ch 9 / Foil 30 Auxiliary Insertion Operators ostream& operator<<( ostream &sout, const Rational &r) { r.Insert(sout); return sout; } l Why a reference return? l Note we can do either t.Insert(cout); cout << endl; // unnatural cout << t << endl; // natural

Ch 9 / Foil 31 Auxiliary Extraction Operator // extracting a Rational istream& operator>>(istream &sin, Rational &r) { r.Extract(sin); return sin; } l Why a reference return? l We can do either t.Extract(cin); // unnatural cin >> t; // natural