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.

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Operator overloading redefine the operations of operators
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Abstract Data Type Fraction Example
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.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Abstract Data Types Development and Implementation.
C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric.
If Statements & Relational Operators Programming.
Classes. COMP104 Lecture 25 / Slide 2 Motivation  Types such as int, double, and char are simple objects. * They can only answer one question: “What.
Classes. COMP104 Class / Slide 2 Motivation  Types such as int, double, and char are “stupid” objects. * They can only answer one question: “What value.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Classes and OOP. Basic, built-in, pre-defined types : char, int, double, … Variables + operations on them int a, b,c; c=a+b; c=a mod b; … More complicated,
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)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
CMSC 2021 Stream I/O Operators and Friend Functions.
Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Programmer-defined classes Part 3. A class for representing Fractions public class Fraction { private int numerator; private int denominator; public Fraction(int.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Object Oriented Programming in C++ Chapter5 Operator Overloading.
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.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Case Study - Fractions Timothy Budd Oregon State University.
Introduction to Classes in C++
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.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
CONTROLLING PROGRAM FLOW
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
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.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Object-Oriented Programming (OOP) Lecture No. 16
Andy Wang Object Oriented Programming in C++ COP 3330
CSCE 210 Data Structures and Algorithms
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Introduction to Programming
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Object-Oriented Programming (OOP) Lecture No. 16
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Fraction Abstract Data Type
Advanced Program Design with C++
Classes and OOP.
COMS 261 Computer Science I
COP 3330 Object-oriented Programming in C++
Development and Implementation
Class rational part2.
Chapter 11 Classes.
Presentation transcript:

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 functions" to read the values and "mutator functions" to modify values? Abstract (user-defined) data types Private data members

COMP104 Lecture 30 / Slide 3 Review: class l Is there a way to avoid all these hassles, for instance: void main() { Temperature temp; cout << temp.degree << ' ' << temp.scale << endl; // no inspectors temp.scale = 10; // no mutators temp.degree = 'c'; } l If yes to above, why we need all the hassles of inspector and mutator functions? Public data members! Information hiding

COMP104 Lecture 30 / Slide 4 Abstract Data Type l An Abstract Data Type is a class with some special restrictions. l These restrictions can make programming easier. l One of these restrictions is called information hiding, and it helps avoid common problems (e.g., a denominator of zero should not be allowed in rational numbers). l In information hiding, the user should not be allowed to access the data members directly (they should be private). l An Abstract Data Type is used in Object-Oriented Programming (COMP151).

COMP104 Lecture 30 / Slide 5 n Multiplication n Division Rational Review l Rational number Ratio of two integers: a/b –Numerator over the denominator l Standard operations n Addition n Subtraction

COMP104 Lecture 30 / Slide 6 Rational Representation Represent a numerator and denominator with two int data members n NumeratorValue and DenominatorValue n Data members private (information hiding) l Public arithmetic member functions n Rational addition, subtraction, multiplication and division l Public relational member functions n Equality and less than comparisons

COMP104 Lecture 30 / Slide 7 Rational Representation l Public functions n Construction –Default construction Rational r; –Specific numerator and denominator construction Rational r(3, 4); –Copy construction (provided automatically) Rational r(t);Rational r = t; n Assignment (provided automatically) r = t; n Inputting and displaying object l Private functions n Inspecting and mutating (changing) data members n Users should not inspect or change num/denom directly

a Values: NumeratorValue(1) DenominatorValue(2) b Values NumeratorValue(2) DenominatorValue(3) Class Rational Public interface: Add(), Subtract(), Multiply(),Divide(), Equal(), LessThan(), Display(), Get() Data members: NumeratorValue, DenominatorValue Private: Numerator(), Denominator(), SetNumerator(), SetDenominator(), Rational a(1,2); Rational b(2,3);

COMP104 Lecture 30 / Slide 9 void main(){ Rational r; Rational s; cout << "Enter two rationals(a/b): "; r.Get(); s.Get(); Rational t(r);// t = r Rational sum = r.Add(s);// sum = r + s; Rational product = r.Multiply(s);// product = r * s; r.Display(); cout << " + "; s.Display(); cout << " = "; sum.Display();cout << endl; r.Display(); cout << " * "; s.Display(); cout << " = "; product.Display();cout << endl; } main()

COMP104 Lecture 30 / Slide 10 Rational Overview class Rational { public: // for everybody (e.g. main()) private: // for Rational member functions only } ;

COMP104 Lecture 30 / Slide 11 Rational Public public: // default constructor Rational(); // explicit-value constructor Rational(int numer, int denom = 1); // arithmetic facilitators Rational Add(const Rational r) const; Rational Multiply(const Rational r) const; // i/o facilitators void Display() const; void Get();

COMP104 Lecture 30 / Slide 12 Rational Private private: // inspectors int Numerator() const; int Denominator() const; // mutators void SetNumerator(int numer); void SetDenominator(int denom); // data members int NumeratorValue; int DenominatorValue;

COMP104 Lecture 30 / Slide 13 const const Rational OneHalf(1,2); OneHalf.Display(); // no problem OneHalf.Get(); // illegal: OneHalf is a const

COMP104 Lecture 30 / Slide 14 Default Constructor // default constructor Rational::Rational() { SetNumerator(0); SetDenominator(1); } l Example Rational r; // r = 0/1

COMP104 Lecture 30 / Slide 15 Specific Constructor // specific constructor Rational::Rational(int numer, int denom) { SetNumerator(numer); SetDenominator(denom); } l Example Rational t(2,3); // t = 2/3

COMP104 Lecture 30 / Slide 16 Inspectors int Rational::Numerator() const { return NumeratorValue; } int Rational::Denominator() const { return DenominatorValue; } l Where is the following legal? int a = Numerator(); Answer: In a member function only, because Numerator() is private. Why the const?

COMP104 Lecture 30 / Slide 17 Mutators void Rational::SetNumerator(int numer) { NumeratorValue = numer; } l Where is the following legal? SetNumerator(1); Answer: In a member function only, because SetNumerator() is private. Why no const?

COMP104 Lecture 30 / Slide 18 Mutators void Rational::SetDenominator(int denom) { if (denom != 0) DenominatorValue = denom; else { cout << "Illegal denominator: " << denom << "using 1" << endl; DenominatorValue = 1; } l Example SetDenominator(5);

COMP104 Lecture 30 / Slide 19 Facilitators Rational Rational::Add(const Rational r) const { int a = Numerator(); int b = Denominator(); int c = r.Numerator(); int d = r.Denominator(); Rational result(a*d + b*c, b*d); return result; } l Example Rational r = t.Add(u);

COMP104 Lecture 30 / Slide 20 Facilitators Rational Rational::Multiply(const Rational r) const { int a = Numerator(); int b = Denominator(); int c = r.Numerator(); int d = r.Denominator(); Rational result(a*c, b*d); return result; } l Example Rational r = t.Multiply(u);

COMP104 Lecture 30 / Slide 21 I/O Facilitators void Rational::Display() const { cout << Numerator() << '/' << Denominator(); } l Example t.Display();

COMP104 Lecture 30 / Slide 22 I/O Facilitators void Rational::Get() { int numer; int denom; char slash; cin >> numer >> slash >> denom; SetNumerator(numer); SetDenominator(denom); } l Example t.Get();