chapter 7 implementing abstract data types 抽象数据类型实现 introduction a data abstraction...

49
CHAPTER 7 Implementing abstract data ty pes 抽抽抽抽抽抽抽抽 Introduction A data abstraction is a representation of informat ion and the operations to be performed on the info rmation. An abstract data type, or ADT, is a well- defined and complete data abstraction that uses th e information-hiding principle. ADTs make it possi ble to create and manipulate objects in a natural manner. 抽抽抽抽抽抽抽抽抽抽抽抽抽抽抽抽抽抽抽抽 抽抽抽抽抽抽 (ADT) 抽抽抽抽抽抽抽抽抽 抽抽抽抽抽抽 抽抽抽抽抽抽抽抽抽ADT 抽抽抽抽抽抽抽抽抽抽抽抽抽抽抽

Upload: colleen-harvey

Post on 02-Jan-2016

271 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

CHAPTER 7Implementing abstract data types

抽象数据类型实现

IntroductionA data abstraction is a representation of information and the operations to

be performed on the information. An abstract data type, or ADT, is a well-

defined and complete data abstraction that uses the information-hiding prin

ciple. ADTs make it possible to create and manipulate objects in a natural

manner.

数据抽象就是对信息表达并对信息执行的操作。抽象数据类型 (ADT)

按照信息屏蔽的原则,对数据抽象作意义明确完整的定义。 ADT 允许用自然的方法创建和管理对象。

Page 2: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

• Fundamental-type 基本数据类型• Data abstraction 数据抽象• Information-hiding 信息隐蔽• Abstract data type(ADT) 抽象数据类型

7.1 INTRODUCING ABSTRACT DATA TYPES

抽象数据类型简介

Example, an Rational ADT :

Rational a ( 1, 2 ) ; // a = 1 / 2

Rational b ( 2, 3 ) ; // b = 2 / 3

cout << a << “+” << b << “=” << ( a + b) << endl ;

Page 3: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

Our goal in developing Rational is to create a type whose objects are as

natural to use as objects defined using the fundamental types.

7.2 RATIONAL ADT BASICS

有理数 Rational ADT 的基础

The basic arithmetic operations :

bd

bcad

d

c

b

a • Addition:

bd

bcad

d

c

b

a • Subtraction:

bd

ac

d

c

b

a• Mulitiplication:

bd

ad

c/b

a/b• Division:

Page 4: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

Data members:

• numerator int object

• denominator int object

Member functions:

• Construct the rational number with default or particular attributes.

• Add, subtract, multiply, and divide the rational number to another rational

number.

• Copy the value of the rational number to another rational number.

• Compare the rational number to another rational number.

• Display the value of the rational number.

• Extract the value of the rational number.

• Inspect the values of the numerator and the denominator.

• Set the values of the numerator and the denominator.

Class Rational

Page 5: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

• This rule states that unless a behavior is needed, it should not be part of

the ADT.

• A corollary to the rule of minimality is the class minimality principle.

Rule minimality ( 最小化规则 )

Page 6: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// Program 8.1 Demonstrate Rational ADT

#include <iostream>

#include <string>

#include "rational.h"

using namespace std ;

int main() {

Rational r , s ;

cout << "Enter rational number (a/b): " ; cin >> r ;

cout << "Enter rational number (a/b): " ; cin >> s ;

Rational t ( r ) ;

Rational Sum = r + s ;

Rational Product = r * s ;

cout << r << " + " << s << " = " << Sum << endl ;

cout << r << " * " << s << " = " << Product << endl ;

return 0;

}

7.2.1 A client program using the rational library

Page 7: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// Program 8.1 Demonstrate Rational ADT #include <iostream>

#include <string>

#include "rational.h"

using namespace std ;

int main() {

Rational r , s ;

cout << "Enter rational number (a/b): " ; cin >> r ;

cout << "Enter rational number (a/b): " ; cin >> s ;

Rational t ( r ) ;

Rational Sum = r + s ;

Rational Product = r * s ;

cout << r << " + " << s << " = " << Sum << endl ;

cout << r << " * " << s << " = " << Product << endl ;

return 0;

}

7.2.1 A client program using the rational library

Use the default constructor for the class

Rational

Page 8: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// Program 8.1 Demonstrate Rational ADT

#include <iostream>

#include <string>

#include "rational.h"

using namespace std ;

int main() {

Rational r , s ;

cout << "Enter rational number (a/b): " ; cin >> r ;

cout << "Enter rational number (a/b): " ; cin >> s ;

Rational t ( r ) ;

Rational Sum = r + s ;

Rational Product = r * s ;

cout << r << " + " << s << " = " << Sum << endl ;

cout << r << " * " << s << " = " << Product << endl ;

return 0;

}

7.2.1 A client program using the rational library

Use the copy constructor

to create a new object.

Page 9: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// Program 8.1 Demonstrate Rational ADT

#include <iostream>

#include <string>

#include "rational.h"

using namespace std ;

int main() {

Rational r , s ;

cout << "Enter rational number (a/b): " ; cin >> r ;

cout << "Enter rational number (a/b): " ; cin >> s ;

Rational t ( r ) ;

Rational Sum = r + s ;

Rational Product = r * s ;

cout << r << " + " << s << " = " << Sum << endl ;

cout << r << " * " << s << " = " << Product << endl ;

return 0;

}

7.2.1 A client program using the rational library

copy constructor

• The copy constructor has a single constant

reference parameter.

• The copy constructor is always present :

It can be specified by designer

or is automatically supplied by the

compiler.

copy constructor

• The copy constructor has a single constant

reference parameter.

• The copy constructor is always present :

It can be specified by designer

or is automatically supplied by the

compiler.

Page 10: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// Program 8.1 Demonstrate Rational ADT

#include <iostream>

#include <string>

#include "rational.h"

using namespace std ;

int main() {

Rational r , s ;

cout << "Enter rational number (a/b): " ; cin >> r ;

cout << "Enter rational number (a/b): " ; cin >> s ;

Rational t ( r ) ;

Rational Sum = r + s ;

Rational Product = r * s ;

cout << r << " + " << s << " = " << Sum << endl ;

cout << r << " * " << s << " = " << Product << endl ;

return 0;

}

7.2.1 A client program using the rational library

They use the

copy constructor too.

Page 11: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// Program 8.1 Demonstrate Rational ADT

#include <iostream>

#include <string>

#include "rational.h"

using namespace std ;

int main() {

Rational r , s ;

cout << "Enter rational number (a/b): " ; cin >> r ;

cout << "Enter rational number (a/b): " ; cin >> s ;

Rational t ( r ) ;

Rational Sum = r + s ;

Rational Product = r * s ;

cout << r << " + " << s << " = " << Sum << endl ;

cout << r << " * " << s << " = " << Product << endl ;

return 0;

}

7.2.1 A client program using the rational library

Supply initialization parameters:

• The parameter list.

• The initialization expression.

Supply initialization parameters:

• The parameter list.

• The initialization expression.

Page 12: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// Program 8.1 Demonstrate Rational ADT

#include <iostream>

#include <string>

#include "rational.h"

using namespace std ;

int main() {

Rational r , s ;

cout << "Enter rational number (a/b): " ; cin >> r ;

cout << "Enter rational number (a/b): " ; cin >> s ;

Rational t ( r ) ;

Rational Sum = r + s ;

Rational Product = r * s ;

cout << r << " + " << s << " = " << Sum << endl ;

cout << r << " * " << s << " = " << Product << endl ;

return 0;

}

7.2.1 A client program using the rational library

Overloaded operators

Page 13: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

Assignment operator and copy constructionAssignment operator and copy construction Rational v ( 6, 21 ) ; // v is 6 / 21

Rational w ; // w is 0 / 1

w = v ; // w is 6 / 21 by assignment

Rational x = v ; // x is 6 / 21 by copy construction

7.2.1 A client program using the rational library

Overloaded constructionOverloaded construction Rational x ( 3, 4 ) ; // v is 3 / 4

Rational y ( 5 ) ; // y is 5 / 1

Page 14: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

const objectconst object // the data members can not be modified

const Rational OneHalf ( 1, 2 ) ;

7.2.1 A client program using the rational library

Overloaded operators >> and <<Overloaded operators >> and << Rational r ;

cout << "Enter rational number (a/b): " ;

cin >> r ;

cout << r ;

Page 15: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

const objectconst object // the data members can not be modified

const Rational OneHalf ( 1, 2 ) ;

7.2.1 A client program using the rational library

Overloaded operators >> and <<Overloaded operators >> and << Rational r ;

cout << "Enter rational number (a/b): " ;

cin >> r ;

cout << r ;

• If operator >> (or <<) is overloaded to be a member function,

left operand must be an object of Rational type :

Rational :: istream & operator >>operator >> ( istream &sin ) ;

Rational :: ostream & operator <<operator << ( ostream &sout ) ;

r >> cin ;

r << cout ;

• If they overloaded to be common function or friend function,

we are free to define their left and right operands.

istream & operator >>operator >> ( istream &sin, const Rational &r ) ;

ostream & operator <<operator << ( ostream &sout, const Rational &r ) ;

cin >> r ;

cout << r ;

• If operator >> (or <<) is overloaded to be a member function,

left operand must be an object of Rational type :

Rational :: istream & operator >>operator >> ( istream &sin ) ;

Rational :: ostream & operator <<operator << ( ostream &sout ) ;

r >> cin ;

r << cout ;

• If they overloaded to be common function or friend function,

we are free to define their left and right operands.

istream & operator >>operator >> ( istream &sin, const Rational &r ) ;

ostream & operator <<operator << ( ostream &sout, const Rational &r ) ;

cin >> r ;

cout << r ;

Page 16: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

7.2.1 A client program using the rational library

Operators +, * are overloaded to be Operators +, * are overloaded to be commoncommon function or function or friendfriend function function

• Member function

Rational :: Rational operator +operator + ( const Rational &r ) ;

Rational u ( 3, 4 ) , t ;

t = u + 2 ; // OK

t = 2 + u ;t = 2 + u ; // Error

• Common function or friend function

Rational operator +operator + ( const Rational &r , const Rational &s ) ;

t = u + 2 ; // OK

t = 2 + u ; // OK

Function name

Page 17: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// rational.h: declaration of Rational ADT

#ifndef RATIONAL_H

#define RATIONAL_H

#include <iostream>

#include <string>

using namespace std;

// …

#endif

7.3 RATIONAL INTERFACE DESCRIPTION

Page 18: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// Rational ADT: class descriptionclass Rational { public: Rational(); // member functions default constructor Rational(int numer, int denom = 1); // a second constructor // some arithmetic and stream facilitators

Rational Add(const Rational &r) const;Rational Multiply(const Rational &r) const;void Insert(ostream &sout) const;void Extract(istream &sin);

protected: // inspectorsint GetNumerator() const;int GetDenominator() const;

// mutatorsvoid SetNumerator(int numer);void SetDenominator(int denom);

private: // data membersint NumeratorValue;int DenominatorValue;

};

Page 19: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// Rational ADT: auxiliary operator description

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);

#endif

Page 20: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

public can be directly used

protected can be use by other member functions of the class and

by

member functions from a derived class.

private can be use only by other member functions of the class .

7.3.1 Access restrictions 访问权限

Page 21: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

The prototypes of the two Rational constructors

Rational();

Rational(int numer, int denom = 1);

7.3.2 Constructor member functions

A copy constructor and a member assignment operator are defaulted, the

Rational class uses the definitions provided by the compiler.

Page 22: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

Member functions:

• Inspector functions (检查器)

provide methods to access representations of the date member.

• Mutator functions (转变器)

provide methods to modify the representations of the data members.

• Facilitator functions (简化器)

provide methods for achieving the operations intended with the object.

7.3.3 Facilitator member functions 简化器

Page 23: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

Rational Add ( const Rational &r ) const ;

Rational Multiply ( const Rational &r ) const ;

void Insert ( ostream &sout ) const ;

void Extract ( istream &sin ) ;

7.3.3 Facilitator member functions 简化器

Page 24: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

Rational Add ( const Rational &r ) constconst ;

Rational Multiply ( const Rational &r ) constconst ;

void Insert ( ostream &sout ) constconst ;

void Extract ( istream &sin ) ;

7.3.3 Facilitator member functions 简化器

• Const member functions can not change any of the invoking object’s

data member.

• Const member functions and operators can be used by const and non-

const objects.

• Non-const member functions con not be used by const objects.

Page 25: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

Rational Add ( const Rational &r ) constconst ;

Rational Multiply ( const Rational &r ) constconst ;

void Insert ( ostream &sout ) constconst ;

void Extract ( istream &sin ) ;

7.3.3 Facilitator member functions 简化器

For example

const Rational OneHalf ( 1, 2 ) ;

OneHalf . Insert ( cout ) ; // legal

OneHalf . Extract ( cin ) ;OneHalf . Extract ( cin ) ; // illegal, Extract() is not const member function

Page 26: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

Use the four Rational facilitators

Rational r ;

Rational s ;

cout << "Enter rational number (a/b): " ;

r.Extract ( cin ) ;

cout << "Enter rational number (a/b): " ;

s.Extract ( cin ) ;

Rational t ( r ) ;

Rational Sum = r.Add (s) ;

Rational Product = r.Multiply (s) ;

r.Inseral ( cout ) ; cout << " + " ; s.Inseral ( cout ) ;

cout << " = " ; s.Inseral ( cout ) ; cout << endl ;

r.Inseral ( cout ) ; cout << " * " ; s.Inseral ( cout ) ;

cout << " = " ; Product.Inseral ( cout ) ; cout << endl ;

Page 27: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

Use the four Rational facilitators

Rational r ;

Rational s ;

cout << "Enter rational number (a/b): " ;

r.Extract ( cin ) ;r.Extract ( cin ) ;

cout << "Enter rational number (a/b): " ;

s.Extract ( cin ) ;s.Extract ( cin ) ;

Rational t ( r ) ;

Rational Sum = r.Add (s) ;Rational Sum = r.Add (s) ;

Rational Product = r.Multiply (s) ;Rational Product = r.Multiply (s) ;

r.Inseral ( cout ) ; cout << " + " ; s.Inseral ( cout ) ; r.Inseral ( cout ) ; cout << " + " ; s.Inseral ( cout ) ;

cout << " = " ; s.Inseral ( cout ) ; cout << endl ;cout << " = " ; s.Inseral ( cout ) ; cout << endl ;

r.Inseral ( cout ) ; cout << " * " ; s.Inseral ( cout ) ; r.Inseral ( cout ) ; cout << " * " ; s.Inseral ( cout ) ;

cout << " = " ; Product.Inseral ( cout ) ; cout << endl ;cout << " = " ; Product.Inseral ( cout ) ; cout << endl ;

useuseOverloaded operatorsOverloaded operators

useuseOverloaded operatorsOverloaded operators

Page 28: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

Use the four Rational facilitators

Rational r ;

Rational s ;

cout << "Enter rational number (a/b): " ;

r.Extract ( cin ) ;r.Extract ( cin ) ;

cout << "Enter rational number (a/b): " ;

s.Extract ( cin ) ;s.Extract ( cin ) ;

Rational t ( r ) ;

Rational Sum = r.Add (s) ;Rational Sum = r.Add (s) ;

Rational Product = r.Multiply (s) ;Rational Product = r.Multiply (s) ;

r.Inseral ( cout ) ; cout << " + " ; s.Inseral ( cout ) ; r.Inseral ( cout ) ; cout << " + " ; s.Inseral ( cout ) ;

cout << " = " ; s.Inseral ( cout ) ; cout << endl ;cout << " = " ; s.Inseral ( cout ) ; cout << endl ;

r.Inseral ( cout ) ; cout << " * " ; s.Inseral ( cout ) ; r.Inseral ( cout ) ; cout << " * " ; s.Inseral ( cout ) ;

cout << " = " ; Product.Inseral ( cout ) ; cout << endl ;cout << " = " ; Product.Inseral ( cout ) ; cout << endl ;

cin >> r ;cin >> r ;

cin >> s ;cin >> s ;

Rational Sum = r + s;Rational Sum = r + s;

Rational Product = r * s;Rational Product = r * s;

cout << r << " + " << s << " = " << Sum << endl;cout << r << " + " << s << " = " << Sum << endl;

cout << r << " * " << s << " = " << Product << endl;

Page 29: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

Use the four Rational facilitators

Rational r ;

Rational s ;

cout << "Enter rational number (a/b): " ;

r.Extract ( cin ) ;r.Extract ( cin ) ; // cin >> r ;// cin >> r ;

cout << "Enter rational number (a/b): " ;

s.Extract ( cin ) ; // cin >> s ;// cin >> s ;

Rational t ( r ) ;

Rational Sum = r.Add (s) ;Rational Sum = r.Add (s) ; // Rational Sum = r + s;// Rational Sum = r + s;

Rational Product = r.Multiply (s) ;Rational Product = r.Multiply (s) ; // Rational Product = r * s;// Rational Product = r * s;

r.Inseral ( cout ) ; cout << " + " ; s.Inseral ( cout ) ; r.Inseral ( cout ) ; cout << " + " ; s.Inseral ( cout ) ;

cout << " = " ; s.Inseral ( cout ) ; cout << endl ;cout << " = " ; s.Inseral ( cout ) ; cout << endl ;

// cout << r << " + " << s << " = " << Sum << endl;// cout << r << " + " << s << " = " << Sum << endl;

r.Inseral ( cout ) ; cout << " * " ; s.Inseral ( cout ) ; r.Inseral ( cout ) ; cout << " * " ; s.Inseral ( cout ) ;

cout << " = " ; Product.Inseral ( cout ) ; cout << endl ;cout << " = " ; Product.Inseral ( cout ) ; cout << endl ;

// cout << r << " * " << s << " = " << Product << endl;

Page 30: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

protected:

int GetNumerator() const;

int GetDenominator() const;

7.3.4 Inspector member function 检查器

Page 31: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

protected:

void SetNumerator(int numer);

void SetDenominator(int denom);

7.3.5 Mutator member function 转变器

Page 32: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

private:

int NumeratorValue ;

int DenominatorValue ;

7.3.6 Data member 数据成员

Page 33: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

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);

7.3.6 Overloaded operators 运算符重载

Overloaded operators are the same as their

conventional precedence

and calling form .

Exercise P423-12

Page 34: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

SCOPE OPERATOR ( 作用域运算符 )

void Rational :: SetNumerator(int numer) {

NumeratorValue = numer;

}

7.4 IMPLEMENTING THE RATIONAL CLASS

Page 35: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// default constructor

Rational::Rational()

{ SetNumerator(0);

SetDenominator(1);

}

// (numer, denom) constructor

Rational::Rational(int numer, int denom)

{ SetNumerator(numer);

SetDenominator(denom);

}

7.4.1 Constructor definitions

Page 36: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// default constructor

Rational::Rational()

{ SetNumerator(0);

SetDenominator(1);

}

// (numer, denom) constructor

Rational::Rational(int numer, int denom)

{ SetNumerator(numer);

SetDenominator(denom);

}

Use member functions

7.4.1 Constructor definitions

Page 37: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

7.4.1 Constructor definitions

// default constructor

Rational::Rational()

{ SetNumerator(0);

SetDenominator(1);

}

// (numer, denom) constructor

Rational::Rational(int numer, int denom)

{ SetNumerator(numer);

SetDenominator(denom);

}

// default constructor

Rational::Rational()

{ NumeratorValue = 0;

DenominatorValue = 1;

// (numer, denom) constructor

Rational::Rational(int numer, int deno

m) { NumeratorValue = numer;

if (denom != 0) Denominat

orValue = denom;

else

DenominatorValue = 1;

}

Page 38: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// get the numerator

int Rational::GetNumerator() const {

return NumeratorValue;

}

// get the denominator

int Rational::GetDenominator() const {

return DenominatorValue;

}

7.4.2 Inspector definitions

Page 39: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// set the numeratorvoid Rational :: SetNumerator(int numer) {

NumeratorValue = numer;

}

// set the denominatorvoid Rational :: SetDenominator(int denom) {

if (denom != 0) {

DenominatorValue = denom;

}

else {

cerr << "Illegal denominator: " << denom<< "using 1" << endl;

DenominatorValue = 1;

}

}

7.4.3 Mutator definitions

Exception handling

Page 40: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// adding RationalsRational 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);

}

// multiplying RationalsRational 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);

}

7.4.4 Arithmetic facilitator definitions

Rational x ( 1, 2 ) ;

Rational y ( 1, 3 ) ;

Rational z = x . Add ( y ) ; // 1/2 + 1/3 is 5/6

cout << z <<endl;

Rational x ( 1, 2 ) ;

Rational y ( 1, 3 ) ;

Rational z = x . Add ( y ) ; // 1/2 + 1/3 is 5/6

cout << z <<endl;

Page 41: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// inserting a Rationalvoid Rational::Insert(ostream &sout) const { sout << GetNumerator() << '/' << GetDenominator(); return;}

// extracting a Rationalvoid Rational::Extract(istream &sin) { int numer; int denom; char slash; sin >> numer >> slash >> denom; SetNumerator(numer); SetDenominator(denom); return;}

7.4.5 Insertion and extraction definitions

Rational x ;

x . Extract ( cin ) ;

Rational y ( 1, 3 ) ;

Rational z = x . Add ( y ) ;

z . Insert ( cout ) ;

Rational x ;

x . Extract ( cin ) ;

Rational y ( 1, 3 ) ;

Rational z = x . Add ( y ) ;

z . Insert ( cout ) ;

Can youOutput the value of

x+y directly ?

Can youOutput the value of

x+y directly ?

Page 42: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// adding RationalsRational operator+(const Rational &r, const Rational &s) {

return r . Add ( sr . Add ( s ) ; // is r + s}

// multiplying RationalsRational operator*(const Rational &r, const Rational &s) {

return r . Multiply ( s )r . Multiply ( s ) ; // is r * s}

7.4.5 Auxiliary arithmetic operator definitions

Rational x ;

x . Extract ( cin ) ;

Rational y ( 1, 3 ) ;

Rational z = x + yx + y ;

z . Insert ( cout ) ;

Rational x ;

x . Extract ( cin ) ;

Rational y ( 1, 3 ) ;

Rational z = x + yx + y ;

z . Insert ( cout ) ;

Page 43: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// inserting a Rationalostream& operator<<(ostream &sout, const Rational &r) {

r.Insert(sout);r.Insert(sout);return sout;

}

// extracting a Rationalistream& operator>>(istream &sin, Rational &r) {

r.Extract(sin);r.Extract(sin);return sin;

}

7.4.6 Auxiliary stream operator definitions

Rational x ;

cin >> xcin >> x ;

Rational y ( 1, 3 ) ;

Rational z = x + yx + y ;

cout << zcout << z << endl ;

Rational x ;

cin >> xcin >> x ;

Rational y ( 1, 3 ) ;

Rational z = x + yx + y ;

cout << zcout << z << endl ;

Page 44: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// Rational : copy constructor

Rational :: Rational ( const Rational &r ) {

int a = r . GetNumerator() ;

inr b = r . GetDenominator() ;

SetNumerator(a) ;

SetDenominator(b) ;

}

// Rational destructor

Rational :: ~~Rational ( ) { /* no body needed */ }

7.5 COPY CONSTRUCTION, MEMBER ASSIGNMENT, AND DESTRUCTION

Shallow copying (浅拷贝 )

The source’s data members ar

e copied bit by bit to the corre

sponding data members of the

target.

Shallow copying (浅拷贝 )

The source’s data members ar

e copied bit by bit to the corre

sponding data members of the

target.

Page 45: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// Rational : copy constructor

Rational :: Rational ( const Rational &r ) {

int a = r . GetNumerator() ;

inr b = r . GetDenominator() ;

SetNumerator(a) ;

SetDenominator(b) ;

}

// Rational destructor

Rational :: ~~Rational ( ) { /* no body needed */ }

7.5 COPY CONSTRUCTION, MEMBER ASSIGNMENT, AND DESTRUCTION

DestructorDestructor• Cannot take a parameter.

• Cannot produce a return value.

• A class to a single destructor.

Page 46: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

// Rational : assignment operator

Rational & Rational :: operator= ( const Rational &r ) {

int a = r . GetNumerator() ;

inr b = r . GetDenominator() ;

SetNumerator(a) ;

SetDenominator(b) ;

return *this*this ;

}

7.5 COPY CONSTRUCTION, MEMBER ASSIGNMENT, AND DESTRUCTION

Page 47: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

About thisthis pointer

• this is a key word

• this is a address of the object whose member function is being invoked.

• this is imparted to the member function when is being invoked by

system. Object . mf actual parameter

ClassType :: mf formal parameterthis

Page 48: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

About thisthis pointer

• this is a key word

• this is a address of the object whose member function is being invoked.

• this is imparted to the member function when is being invoked by

system. Object . mf actual parameter

ClassType :: mf formal parameterthis &Object

DM:

FM:

DM:

FM:Object

&Object

this * this Outside Inside

Object *this

Object.dm (*this).dm this->dm

Object.fm (*this).fm this->fm

Page 49: CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed

#include<iostream>#include<string>using namespace std;string s="w";class C{ public: C() //defual constructor { name=s; cout<<name<<": default constructed"<<endl; } C(const C &c) //copy constructor { name=s; cout<<name<<": copy constructor using "<<c.name<<endl; } ~C() // destructor { cout<<name<<": destructed "<<endl; } C & operator=(const C &c) // assignment { cout<<name<<": assigned using "<<c.name<<endl; return *this; } private: string name; //data member};int main(){ C w; s="x"; C x; { s = "y" ; C y ; } s = "z" ; C z(w) ; x = w = z ; return 0 ;}

w: default constructed

x: default constructed

y: default constructed

y: destructed

z : copy constructor using w

w: assigned using z

x : assigned using w

z: destructed

x: destructed

w: destructed