c++ workshop designing and implementing classes. references ● c++ programming language, bjarne...

43
C++ Workshop Designing and Implementing Classes

Post on 18-Dec-2015

223 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

C++ Workshop

Designing and Implementing Classes

Page 2: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

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

Page 3: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

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

Page 4: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

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 classes

● friend – complete access to all class members, use with caution

Page 5: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

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

Page 6: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Statements not permissible due to access restrictions

Rational r(3,4);

r.ImaginaryValue = 6; //illegal – data is privateint a = GetNumerator(r) //illegal – function is protectedr.SetNumerator(b); //illegal – function is protected

Page 7: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

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

Page 8: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

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

Page 9: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

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

Page 10: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Mutators

● Modify member data● Protected access ● void SetNumerator(int numer);● void SetDenominator(int denom);

Page 11: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

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

Page 12: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Stream Facilitators

● public access● void Insert(ostream &sout) const;

– inserts a representation of the object into the output stream referenced by &sout

● void Extract(istream &sin) const;– extracts a representation of the object

from the input stream referenced by &sin

Page 13: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Stream use examples

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.Add(s);t.Insert(cout);cout << endl;

//note: illustrations of use are based only on the //definitions. These member functions still have to be//implemented

Page 14: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Overloaded Operators

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

● reserved word operator indicates operator overloading

● returns a Rational object

Page 15: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Implementation

● use a .cc or .cpp file● Syntax for member function

implementation header:● <return-type>

<classname>::<functionname>(<parameters>)

● Example:– void Rational::SetNumerator(int numer) {– NumeratorValue=numer;– }

Page 16: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Constructors - Implementation

//default constructorRational::Rational() { SetNumerator(0); SetDenominator(1);}

//numer, denom constructorRational::Rational(int numer, int denom) { SetNumerator(numer); SetDenominator(denom);}

//Notice: both constructors are invoking protectedmember functions SetNumerator and SetDenominator

Page 17: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Inspector Implementations

//get numeratorint Rational::GetNumerator() const { return NumeratorValue;}

//get denominatorint Rational::GetDenominator() const { return DenominatorValue;}

Page 18: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Mutator Implementation//set numeratorvoid Rational::SetNumerator(int numer) { NumeratorValue = numer;}//set denominatorvoid Rational::SetDenominator(int denom) { if (denom != 0) { DenominatorValue = denom; } else { cerr << “Illegal denominator: “ << denom << “using 1” << endl; DenominatorValue = 1; }}

Page 19: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Arithmetic Function Implementation

One example will suffice:

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);}//Note that the return invokes the constructor

Page 20: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Stream Insertion and Extraction - Implement

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

//Extracting a Rationalvoid Rational::Extract(istream &sin) { //input a/b int numer; int denom; char slash; sin >> numer >> slash >> denom; SetNumerator(numerb); SetDenominator(denom);}

Page 21: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Operator implementation

Again, one example:

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

return r.Add(s);}

Page 22: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Automatic member functionsC++ automatically makes copy constructor available:e.g.Rational r(1,2);Rational s(r); //s is copy constructed from r

C++ also automatically makes assignment available,e.g.

Rational t = r;

C++ automatically makes destructor member functionavailable, ~Rational() -Restrictions: no parameters, no return value

Page 23: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Explicit Implementation //Rational copy constructorRational::Rational(const Rational &r) { int a = r.getDenominator(); int b = r.getNumerator(); SetNumerator(b); SetDenominator(a);}//Rational: destructorRational::~Rational(){ }//Rational: assignment operatorRational& Rational::operator=(const Rational &r) { int a = r.GetDenominator(); int b = r.GetNumerator(); SetNumerator(b); SetDenominator(a); return *this;}

Page 24: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

What is *this ?

this is a keyword for the address of the object whosemember function is being invoked

* is the dereferencing operator

*this is the value of the object at that address

Page 25: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Putting it all together1. Create rational.h file with class definitions

2. Create rational.cpp file with implementations for member functions

3. Compile: g++ -c rational.cpp

4. Create a simple driver program using the class, e.g. call example program, testrat.cpp

5. Compile the driver g++ -c testrat.cpp

5. Link the driver to the class module: g++ -o testrat testrat.o rational.o

Page 26: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Other possible member functions for Rational class

● operator += (and other op=)● operator == ● ++, --

Page 27: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

MyComplex Class

● Using example of Rational Class, walk through construction of user-defined Complex Class

Page 28: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Point Class

● 2 dimensional point● attributes: x,y● methods:

– Constructor (s)– move –

parameters1 : x,y coordinates,

parameters 2: another point– draw – for now, just print out– ?? Other functions

Page 29: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Integral Class

● ref: Yang● evaluate definite integrals ● attributes:

– lower and upper bounds– pointer to a function

Page 30: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Integral Classtypedef double (*pfn)(double); //defining a function ptr

class integral { double lower; double upper; pfn integrand; //integrand functionpublic: integral(double a, double b, pfn f) { lower = a; upper = b; integrand = f; } double lowbd() const {return lower;}; double upbd() const {return upper;} void changebd(double, double); double trapezoidal(int) const; friend double simpson(integral, int);};

Page 31: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Integral Class Use Example

int main() {integral di(0,5, sin);

double result = di.trapezoidal(100); cout << result << endl; //change bounds di.changebd(3,7); result = di.trapezoidal(100); cout << result << endl; result = simpson(di, 200); cout << result << endl;}

Page 32: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Class MyVector

Class MyVector: a vector

Data: plain array

Functions: subscripting, change length, assignment to anothervector, inner product with another vector, ...

This example is for illustration only. A vector class is defined in the Standard Template Library

Page 33: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

MyVector - functionalityCreate vectors of a specified length:MyVector v(n);

Create a vector with zero length:MyVector v;

Redimension a vector to length n:v.redim(n);

Create a vector as a copy of another vector w:MyVector v(w);

Extract the length of the vector:const int n = v.size();

Page 34: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

More functionalityExtract an entry:double e = v(i);

Assign a number to an entry:v(j) = e;

Set two vectors equal to each other:w = v;

Take the inner product of two vectors:double a = w.inner(v);or alternativelya = inner(w,v);

Page 35: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

More functionalityWrite a vector to the screen:v.print(…);

Arithmetic operations with vectors…

It is assumed that the example syntax would be defined in theMyVector class.

MyVector consists of the data in the vector, the length of the vector, as well as a set of member functions for operating on the vector data

Users can only operate on the vector data using the memberFunctions

Page 36: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

MyVector class possible definitionclass MyVector{

private:double* A; // vector entries (C-array)int length;void allocate (int n); // allocate memory, length=nvoid deallocate(); // free memorypublic:MyVector (); // MyVector v;MyVector (int n); // MyVector v(n);MyVector (const MyVector& w); // MyVector v(w);~MyVector (); // clean up dynamic memorybool redim (int n); // v.redim(m);MyVector& operator= (const MyVector& w);// v = w;double operator() (int i) const; // a = v(i);double& operator() (int i); // v(i) = a;void print (ostream& o) const; // v.print(cout);double inner (const MyVector& w) const; // a = v.inner(w);int size () const { return length; } // n = v.size();/ operators:MyVector operator* (double a, const MyVector& v); // u = a*v;

MyVector operator* (const MyVector& v, double a); // u = v*a;MyVector operator+ (const MyVector& a, const MyVector& b); // u = a+b;

Page 37: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Constructors - I

MyVector v; // declare a vector of length 0// this actually means calling the function

MyVector::MyVector (){ A = NULL; length = 0; }

Page 38: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Constructors II

MyVector v(n); // declare a vector of length n

// means calling the functionMyVector::MyVector (int n){ allocate(n); }

void MyVector::allocate (int n){ length = n;A = new double[n]; // create n doubles in memory}

Page 39: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

DestructorA MyVector object is created (dynamically) at run time, but mustalso be destroyed when it is no longer in use. The destructorspecifies how to destroy the object:

MyVector::~MyVector (){deallocate();}// free dynamic memory:void MyVector::deallocate (){delete [] A;}

Page 40: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Assignment Operator

Set a vector equal to another vector:// v and w are MyVector objectsv = w;means calling: MyVector& MyVector::operator= (const MyVector& w)// for setting v = w;{ redim (w.size()); // make v as long as w int i; for (i = 0; i < length; i++) { // (C++ arrays start at 0) A[i] = w.A[i];

return *this;}// return of *this, i.e. a MyVector&, allows nested// assignments:u = v = u_vec = v_vec;

Page 41: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Redimensioning LengthChange the length of an already allocated MyVector object:v.redim(n); // make a v of length nImplementation:bool MyVector::redim (int n){

if (length == n)return false; // no need to allocate anythingelse {

if (A != NULL) {/ "this" object has already allocated memorydeallocate(); } //end ifallocate(n);

return true; // the length was changed } //end else}

Page 42: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Copy Constructor

Create a new vector as a copy of an existing one:MyVector v(w); // take a copy of w

MyVector::MyVector (const MyVector& w){allocate (w.size()); // "this" object gets w’s length*this = w; // call operator=}

this is a pointer to the current (“this”) object, *this is the objectitself

Page 43: C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric

Subscripting

// a and v are MyVector objects; want to set a(j) = v(i+1);// the meaning of a(j) is defined by inline double& MyVector::operator() (int i){return A[i-1];// base index is 1 (not 0 as in C/C++)}Inline functions: function body is copied to calling code, no overheadof function call!Note: inline is just a hint to the compiler; there is no guarantee thatthe compiler really inlines the functionWhy return a double reference?double& MyVector::operator() (int i) { return A[i-1]; }// returns a reference (‘‘pointer’’) directly to A[i-1]// such that the calling code can change A[i-1]