data structures and algorithms in c++  michael t. goodrich roberto tamassia david m. mount

28
Data Structures and Algorithms in C++ Michael T. Goodrich Roberto Tamassia David M. Mount Chapter 2 Object-Oriented Design

Upload: thane-glenn

Post on 02-Jan-2016

151 views

Category:

Documents


12 download

DESCRIPTION

Data Structures and Algorithms in C++  Michael T. Goodrich Roberto Tamassia David M. Mount. Chapter 2 Object-Oriented Design. Contents. 2.1 Goals and Principles 2.2 Inheritance and Polymorphism 2.3 Templates 2.4 Exceptions 2.5 Recursion and Other Design Patterns 2.6 Exercises. - PowerPoint PPT Presentation

TRANSCRIPT

Data Structures and Algorithms in C++

Michael T. Goodrich Roberto Tamassia David M. Mount

Chapter 2Object-Oriented Design

Contents

• 2.1 Goals and Principles• 2.2 Inheritance and Polymorphism• 2.3 Templates• 2.4 Exceptions• 2.5 Recursion and Other Design Patterns• 2.6 Exercises

2.1 Goals and Principles

• GoalsRobustnessAdaptabilityReusability

• PrinciplesAbstractionEncapsulationModularity

2.2 Inheritance and Polymorphism

2.2.1 Inheritance in C++class Person {private:

string name;string ssn;

public://…void print();string getName();

};

2.2 Inheritance and Polymorphism

class Student : public Person {private:

string major;int gradYear;

public://…void print();void changeMajor( string newMajor);

};

2.2 Inheritance and Polymorphism

Member Functions

void Person::print() {cout << “Name “ << name << ‘\n’;cout << “SSN “ << ssn << ‘\n’;

}void Student::print() {

Person::print();cout << “Major “ << major << ‘\n’;cout << “Year “ << gradYear << ‘\n’;

}

2.2 Inheritance and Polymorphism

Protected Members

class <class_name> {private:

//…protected:

//…public:

//…};

2.2 Inheritance and PolymorphismConstructors and Destructors

Person::Person(const string &nm, const string &ss): name(nm), // initialize name ssn(ss) { } // initialize ssn

Student::Student(const string &nm, const string &ss, const string &maj, int year)

: Person(nm, ss), // initialize Person membersmajor(maj), // initialize membergradYear(year) { } // initialize graduation year

Student* s = new Student(“John Smith”,”123-45-6789”,”Physics”,2010);Person::~Person() //Person destructor

{…} Student::~Student() //Student destructor

{…}delete s; //calls ~Student() then ~Person()

2.2 Inheritance and Polymorphism

Static BindingPerson* pp[100];pp[0] = new Person(…);pp[1] = new Student(…);

cout << pp[1] ->getName() <<‘\n’; //okaypp[0] ->print();pp[1] ->print();pp[1] ->changeMajor(“English”); //ERROR!

2.2 Inheritance and Polymorphism

Dynamic Binding and Virtual Functionsclass Person {

virtual void print() {….}//…

};class Student : public Person {

virtual void print() {….}//…

};

2.2 Inheritance and Polymorphism2.2.3 Examples of Inheritance in C++

2.2 Inheritance and PolymorphismArithmetic and Geometric Progression Classes

2.2 Inheritance and PolymorphismA Fibonacci Progression Class

2.2 Inheritance and Polymorphism

2.2 Inheritance and Polymorphism

2.2.4 Multiple Inheritance and Class Castingclass Base {protected: int foo;public: int bar;};class Derive1 : public Base {

//foo is protected and bar is public};class Derive2 : protected Base {

//both foo and bar are protected };class Derive3 : private Base {

// both foo and bar are private };

2.2 Inheritance and Polymorphism

2.2.5 Interfaces and Abstract Classes

class Stack {public:

bool isEmpty( ) const;void push(int x);int pop( );

};

2.2 Inheritance and Polymorphism

Interfaces and Abstract Base Classesclass Stack {public:

virtual bool isEmpty( ) const = 0;virtual void push(int x) = 0;virtual int pop( ) = 0;

};class ConcreteStack : public Stack {pribate: //….public:

virtual bool isEmpty( ) {…}virtual void push(int x) {…}virtual int pop( ) {…}

};

2.3 Templates

2.3.1 Function Templatesint min(int a, int b)

{ return (a < b ? a : b); }

template <typename T>T min( T a, T b)

{ return (a < b ? a : b); }

2.3 Templates2.3.2 Class Templates

template <typename Object>class BasicVector {

Object* a;int capacity;

public: BasicVector(int capac = 10) {

capacity = capac;a = new Object[ capacity ];

}Object& elemAtRank(int r)

{ return a[r]; }//…};

2.3 Templates

2.3.2 Class Templates BasicVector<int> iv(5); BasicVector<double> dv(20); BasicVector<string> sv(10);//…iv. elemAtRank(3) = 8;dv. elemAtRank(14) = 2.5;sv. elemAtRank(7) = “hello”;

2.3 Templates

Templated ArgumentsBasicVector<BasicVector<int> > xv(5);//…xv. elemAtRank(2). elemAtRank(8) = 15;

Templated Memberstemplate <typename Object>Object& BasicVector<Object>::elemAtRank(int r) {

return a[r];}

2.4 Exceptions

2.4.1 Exceptions Objectclass MathException {private:

string errMsg;public:

MathException(const string& err){ errMsg = err; }

};

2.4 Exceptions

Using Inheritance to Define New Exception Typesclass ZeroDivideException : public MathException {public:

ZeroDivideException(const string& err): MathException(err) { }

};class NegativeRootException : public MathException {public:

NegativeRootException(const string& err): MathException(err) { }

};

2.4 Exceptions

2.4.2 Throwing and Catching Exceptionstry {

if (divisor == 0 ) throw ZeroDivideException(“Divide by zero in

Module X”);}catch (ZeroDivideException& zde) {

//…}catch (MathException& me) {

//…}

2.4 Exceptions

2.4.3 Exceptions Specification

void calculator( ) throw(ZeroDivideException, NegativeRootException ) {

//function body…}

2.4 ExceptionsGeneric Exception Class

class RuntimeException {private:

string errorMsg;public:

RuntimeException( const string& err){errorMsg = err; }

string getMessage() const { return errorMsh; }};inline std::ostream& operator <<(std::ostream& out,

const RuntimeException& e){ return out << e.getMessage(); }

2.5 Recursion and Other Design Patterns

int recursiveFactorial(int n) {if (n==0) return 1;else return n*recursiveFactorial(n-1);

}

2.6 Exercises

• R-2.11• R-2.12