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();
Image of page 31
Numerator Mutator void Rational::SetNumerator( int numer) { NumeratorValue = numer; } Where are the following legal? SetNumerator(1); t.SetNumerator(2); Why no const?
Image of page 32
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);
Image of page 33
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);
Image of page 34
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);
Image of page 35
Insertion Facilitator void Rational::Insert(ostream &sout) const { sout << GetNumerator() << '/' << GetDenominator(); return ; } Example t.Insert(cout); Why is sout a reference parameter?
Image of page 36
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);
Image of page 37
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;
Image of page 38
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
Image of page 39
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
Image of page 40
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
Image of page 41
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
Image of page 42
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;
Image of page 43
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
Image of page 44
Rational Destructor Rational::~Rational() { // nothing to do }
Image of page 45
Gang Of Three If it is appropriate to define a copy constructor then
Image of page 46
Image of page 47

You've reached the end of your free preview.

Want to read all 49 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes