Why the const Where are the following legal int a GetNumerator int b

# Why the const where are the following legal int a

This preview shows page 31 - 47 out of 49 pages.

Why the const? Where are the following legal? int a = GetNumerator(); int b = t.GetNumerator(); Numerator Mutator void Rational::SetNumerator( int numer) { NumeratorValue = numer; } Where are the following legal? SetNumerator(1); t.SetNumerator(2); Why no const? Denominator Mutator void Rational::SetDenominator(int denom) { if (denom != 0) { DenominatorValue = denom; } else { cerr << "Illegal denominator: " << denom << "using 1" << endl; DenominatorValue = 1; } } Example SetDenominator(5); 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); } Example cout << t.Add(u); 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); } Example t.Multiply(u); Insertion Facilitator void Rational::Insert(ostream &sout) const { sout << GetNumerator() << '/' << GetDenominator(); return ; } Example t.Insert(cout); Why is sout a reference parameter? 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 ; } Example t.Extract(cin); 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); } Example cout << (t + t) * t; Auxiliary Insertion Operator ostream& operator <<(ostream &sout, const Rational &r) { r.Insert(sout); return sout; } Why a reference return? Note we can do either t.Insert(cout); cout << endl; // unnatural cout << t << endl; // natural Auxiliary Extraction Operator // extracting a Rational istream& operator >>(istream &sin, Rational &r) { r.Extract(sin); return sin; } Why a reference return? We can do either t.Extract(cin); // unnatural cin >> t; // natural What’s Happening Here? Suppose the following definitions are in effect Rational a(2,3); Rational b(3,4); Rational c(1,2); Why do the following statements work Rational s(a); Rational t = b; c = a; C++ has automatically provided us a copy constructor and an assignment operator Copy Construction Default copy construction Copy of one object to another in a bit-wise manner The representation of the source is copied to the target in a bit-by-bit manner This type of copy is called shallow copying Class developers are free to implement their own copy constructor Rational does not need a special one, but we will define one for the experience A Rational Copy Constructor Rational::Rational( const Rational &r) { int a = r.GetNumerator(); int b = r.GetDenomiator(); SetNumerator(a); SetDenominator(b); } Rational a(1,2); Rational s(a); Rational t = b; A Rational Assignment Operator Rational& Rational:: operator =( const Rational &src) { if (&src != this ) { int a = src.GetNumerator(); int b = src.GetDenomiator(); SetNumerator(a); SetDenominator(b); } return * this ; } “this” is a pointer of the object itself. It is called “ this pointer “*this” is C++ syntax for the object itself Rational Destructor Rational::~Rational() { // nothing to do } Gang Of Three If it is appropriate to define a copy constructor then  #### You've reached the end of your free preview.

Want to read all 49 pages?

• • •  