Download presentation
Presentation is loading. Please wait.
Published byNoah Lyons Modified over 8 years ago
1
Programming Abstract Data Types
2
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
3
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
4
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).
5
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
6
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
7
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
8
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);
9
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()
10
COMP104 Lecture 30 / Slide 10 Rational Overview class Rational { public: // for everybody (e.g. main()) private: // for Rational member functions only } ;
11
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();
12
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;
13
COMP104 Lecture 30 / Slide 13 const const Rational OneHalf(1,2); OneHalf.Display(); // no problem OneHalf.Get(); // illegal: OneHalf is a const
14
COMP104 Lecture 30 / Slide 14 Default Constructor // default constructor Rational::Rational() { SetNumerator(0); SetDenominator(1); } l Example Rational r; // r = 0/1
15
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
16
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?
17
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?
18
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);
19
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);
20
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);
21
COMP104 Lecture 30 / Slide 21 I/O Facilitators void Rational::Display() const { cout << Numerator() << '/' << Denominator(); } l Example t.Display();
22
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();
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.