Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are...

15
Object Oriented Programming Key Features of OO Approach Data encapsulation – data and methods are contained in a single unit, object – promotes internal integrity – simplifies coding, debugging and maintenance Data hiding – no need to know all the details of implementation

Transcript of Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are...

Page 1: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

Object Oriented Programming

Key Features of OO Approach

• Data encapsulation– data and methods are contained in a single

unit, object– promotes internal integrity– simplifies coding, debugging and

maintenance

• Data hiding– no need to know all the details of

implementation

Page 2: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

Object Oriented Programming

Key Features of OO Approach

• Inheritance– augment, restrict, specialize behavior– provides mechanism for reuse

• Polymorphism– different behavior of the same method or

operator depending on the class and/or parameters

Page 3: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

Object Oriented Programming

What Is Design?

• Reiss: “design in the most general sense is an abstract description of how something is built”

• Design involves finding the set of alternatives most relevant to the problem, analyzing that set, and then choosing a subset of the alternatives that cooperatively solves the problem at hand in the “best” manner (making tradeoffs)

Page 4: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

Object Oriented Programming

Quality of Design

• Level of detail of components should be the same

• Number of components should be reasonable

• The hierarchy of components should be terminated at an appropriate level

Page 5: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

Object Oriented Programming

Object Oriented Decomposition

• Well-defined interfaces

• Cluster related objects

Page 6: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

Object Oriented Programming

Object

• a tangible self-contained entity that exhibits some well-defined behavior

• has state, behavior, and identity; the structure and behavior of similar objects are defined in their common class

• exists at run-time

Page 7: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

Object Oriented Programming

Class

• description of a number of similar objects – custom data type

• objects are instances of the class

• consists of interface and implementation

• provides three levels of access control– public, protected, private

• classes are related in various ways

Page 8: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

#ifndef BANKACCOUNT_H#define BANKACCOUNT_H#include <string>class BankAccount{

private:string acctOwner;int acctNum;double curBalance;

public:BankAccount(string, int);BankAccount(string, int, double);~BankAccount() {};void setBalance(double);double getBalance() const;

};#endif

interface

constructors

destructor

modifier

selector

attributes

Page 9: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

#include “bankacct.h”

BankAccount::BankAccount(string name, int number) :acctOwner(name), acctNum(number),curBalance(0)

{ }

BankAccount::BankAccount(string name, int number, double balance) : acctOwner(name), acctNum(number), curBalance(balance) { }

void BankAccount::setBalance(double newBalance)

{curBalance = newBalance); }

double BankAccount::getBalance() const

{return curBalance;}

implementation

initializing

Page 10: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

using namespace std;

#include “bankacct.h”

void main()

{

BankAccount* ba = new BankAccount(“Jones”, 1234);

ba->setBalance(200.00);

cout<<“The balance is “<<ba->getBalance()<<endl;

delete ba;

}

use

pointer

deallocation

Page 11: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

Object Oriented Programming

Evaluation criteria for a design

• Correctness, completeness

• Simplicity

• Risk management

• Cohesion and coupling

• Information hiding

• Error handling

Page 12: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

Object Oriented Programming

Simplification

• Can operations be removed?• Can data elements be removed?• Can parameters be removed from

operations?• Can parameter and return types for

operations be simplified?• Are the operations and their parameters

logically consistent?

Page 13: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

Object Oriented Programming

Simplify by making methods short

• Pseudocoding/coding guideline: a method should fit onto one page (one screen).

• If the method is too long, split it into separate methods.

• If there are too many methods in a class, rewrite a method so that it invokes lower-level classes.

Page 14: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

Object Oriented Programming

Cohesion and Coupling

• A class is cohesive if everything is directed toward a central purpose.

• A class has good coupling if it has minimal dependency on other classes.

• A class should not need to know the implementation of another class.

Page 15: Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.

Object Oriented Programming

Implementing OO design

• Two steps:– Convert class design into class

declarations– Construct real code from pseudocode