classes - C+ Workshop Designing and Implementing Classes...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
C++ Workshop Designing and Implementing Classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
References C++ Programming Language, Bjarne Stroustrup, Addison-Wesley C++ and Object-Oriented Numeric Computing for Scientists and Engineers, Daoqi Yang, Springer Scientific and Engineering C++, John J. Barton and Lee R. Nackman, Addison-Wesley
Background image of page 2
Rational Class Mechanics Goal: Design and test a class, Rational, for manipulating rational numbers Components: rational.h – header file with definition of the class including member data and functions rational.cpp – implementation of member functions testrat.cpp – a small program to test the class definition
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Access Keywords: private (default), public, protected, friend private – members can accessed only by other member functions or operators of the class public – members can be used directly by client programs protected – members can be used only by other member functions or operators of the class or derived
Background image of page 4
Client Program Example - Revisited #include <iostream> #include <string> #include “rational.h” using namespace std; int main() { Rational r(3/4), s(2/3); Rational t(r); Rational Sum = r + s; Rational Product = r * s; cout << r << “+” << s << “=“ << Sum << endl; return(0);
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Statements not permissible due to access restrictions Rational r(3,4); r.ImaginaryValue = 6; //illegal – data is private int a = GetNumerator(r) //illegal – function is protected r.SetNumerator(b); //illegal – function is protected
Background image of page 6
Constructors Rational (); use in external program or function: Rational b; //data not initialized Rational (int numer, int denom=1); Use in external program or function: Rational b(3,4); //data is ¾ Rational c(4); //denominator defaults to 1
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
const qualifier Objects may also be declared as constants, e.g. const Rational OneHalf(1,2); const objects require const member functions const member functions can be used with both const and non-const objects Example: Rational Add(const Rational &r) const; Object is passed by reference (&r), const qualifier prevents modification const member function will not modify any of object's data members
Background image of page 8
Inspectors Note: protected not accessible to main or external functions Used to retrieve member data int GetNumerator() const; int GetDenominator() const; const qualifier permits use with const objects
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Mutators Modify member data Protected access void SetNumerator(int numer); void SetDenominator(int denom);
Background image of page 10
Arithmetic Functions Rational Add(const Rational &r) const; Public member functions – accessible in main and other functions Example usage: Rational r(3,4); Rational s(2,5); Rational t =r.Add(s); Explanation: r is a rational object invoking its public member function Add with the rational object s as an argument. The result is another Rational object which is assigned to t
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Stream Facilitators public access void Insert(ostream &sout) const;
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 43

classes - C+ Workshop Designing and Implementing Classes...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online