21
Object-Oriented Design

Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Object-Oriented Design

Page 2: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Object-Oriented Design

• Method for designing computer programs

• Consider “objects” interacting in the program– Example: a zoo

Page 3: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

OOD Goals

• Robustness– Gracefully handle failures

• Adaptability– Evolve as necessary

• Reusability– Many programs use same piece of code

Page 4: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

OOD Principles

• Abstraction– Abstract Data Types (ADTs)– Interfaces

• Encapsulation– Information Hiding

• Modularity– Easily plug together components

Page 5: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Inheritance

• Many objects have a hierarchical relationship– Examples: zoo

• Inheritance allows software design to take advantage of relationships

Page 6: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Terminology

• Base class/Parent class/Superclass– defines generic functionality

• Derived class/Child class/Subclass– extends or specializes base class – inherits members of parent– may implement new members– may override members of parent

Page 7: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Examples

• Card Game

• Airline Reservation System

Page 8: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Syntax

class Student : public Person {…}

class Derived : public Base{…}

• Derived class may override or reimplement a function implemented by base class

class Person { class Student:public Person{

… …

void print(); void print();

} }

Page 9: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Function Invocation

• Person – print, getName• Student – print, changeMajor

Person p(…);

Student s(…);

s.getName();

p.print();

s.print();

p.changeMajor();

s.changeMajor();

Page 10: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Function Invocation

• Person – print, getName• Student – print, changeMajor

Person p(…);

Student s(…);

s.getName(); //Person::getName

p.print(); //Person::print

s.print(); //Student::print

p.changeMajor(); //ERROR!!!!!!!!

s.changeMajor(); //Student::changeMajor

Page 11: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

More Syntax

void Person::print() {…}

void Student::print() {Person::print();

//Superclass::function();

}

Page 12: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Protected

• private members of parent not accessible to child class

• protected members accessible only to derived classes

• examples

class classname {private:protected:public:

}

Page 13: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Child Class Constructors

• Subclass must create superclass– invoke superclass constructor from subclass

constructor– use initializer list

Student::Student(string newname, string newmajor)

:Person(newname), major(newmajor) {…}

Student::Student(string newname, string newmajor)

:Person(newname) {major = newmajor;}

Page 14: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Static Binding

Person* p = new Person(…);Student* s = new Student(…);p->print(); //calls Person::print()p = s; //OKAYp->print(); //calls Person::print()p->changeMajor(); //ERROR

• Function called depends on declared type

Page 15: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Dynamic Binding

• May want to determine which function to call based on object contents

• Use virtual functions

class Person {virtual void print();

}class Student : public Person {

virtual void print();}

Page 16: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Dyanmic Binding

Person* p = new Person(…);

Student* s = new Student(…);

p->print(); //calls Person::print()

p = s; //OKAY

p->print(); //calls Student::print()

p->changeMajor(); //ERROR

Page 17: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Polymorphism

• Many forms

• A variable is polymorphic if it points to an object with 1 or more virtual functions

Page 18: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Casting

Person* p;

p = new Student(…);

Student* s = dynamic_cast<Student*>(p);

• Create new pointer of subclass type to point to object

• Pointer (s in this case) will be NULL if cast was NOT successful

• Can also use C-style cast

Page 19: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Abstract Classes

• May want to defined base class which cannot be instantiated– Examples – bank account, car

• Declare one or more functions as pure virtual

virtual void print() = 0;

virtual void func_name(…) = 0;

Page 20: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Templates

template <typename Object>

class BasicVector {}

BasicVector<int> iv(5);

BasicVector<string> sv(100);

• Define classes which can hold any type of object

Page 21: Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo

Exceptions

• If error occurs during runtime, report and continue execution– Example: array index, divide by 0

• Exceptions are also classes– programmer defines

try {//call func that throws exception

} catch(ExceptionType& et) {//e.g., print error

} catch(…) { //catch all exceptions }

void func() throw(ExceptionType) {…}