You are on page 1of 13

Old Programming Paradigm

• Computer system consists of data and


programs.
Object Oriented Programming • Programs manipulate data.
• Programs organized by
• functional decomposition
Based on Levent Akın’s CmpE160 • dataflow
Lecture Slides
• modules

1 2

New Programing Paradigm Object Oriented Programming

• Computer system consists of a set of • consists of the following major concepts:


objects. • Abstraction
• Objects are responsible for knowing and • Encapsulation
doing certain things. • Polymorphism
• Objects collaborate to carry out their • Inheritance
responsibilities.
• Programs organized by classes,
inheritance hierarchies and subsystems
3 4
Abstraction Abstraction and Information Hiding
• separation of a data type’s logical
properties from its implementation
• the process of representing data about real-
• Problem solvers have traditionally used
world things in software abstraction to manage system complexity.
• examples of abstractions:
• The focus in using abstraction is to define
Thing Abstraction Examples a part or component of a system so that
Person academic records this component can be viewed as a black
medical information
job history
box.
• The component can be understood without
Car garage records knowing its details or specifics.
official records
• There is no need to know how this component
is to be used at a higher level.
5 6

Procedural Abstraction Data Abstraction

• All programming languages support • Several programming languages, such


abstraction through subprogram as Eiffel, Java, and C++, support data
constructs (e.g., procedures and abstraction.
functions). This type of abstraction is • The purpose of data abstraction is to
usually called procedural abstraction. view each data structure or object as
consisting of data values and operations
on these values.

7 8
Information Hiding Encapsulation

• The purpose of information hiding is to • association (bundling) of data and the


make inaccessible certain details that do operations that can be performed on that data
not affect the other modules in the • separates the representation of data from the
system. applications that use that data by “hiding” the
data; i.e. not allowing the programmer to
• Hence, the data type and its actions manipulate the data except by the associated
constitute a closed system, the details of operations
which are hidden from the other • enforces information hiding
modules.

9 10

Polymorphism Binding Time

• ability to determine which of several • time at which a name or symbol is


operations with the same name should bound to the appropriate code or value
be performed • static binding – compile-time binding;
• from one, many forms example: function overloading
• example: function overloading • dynamic binding – run-time binding;
• in programming, allow the compiler or example: virtual function overloading
run-time circumstances to decide which • polymorphism is a combination of both
form to use static and dynamic binding

11 12
Overloading and Binding Time Inheritance

• overloading functions allows compiler to • a mechanism used with a hierarchy of


decide what to call or create classes by which each descendant class
inherits the properties (data and
• usually done at static binding time
operations) of its ancestor class
• creation of new encapsulated types from
existing encapsulated types
• allows incorporation of existing features
with additions and replacements

13 14

Inheritance Detailed Program Specification

• derived class – class that inherits


• Tells what the program
• base class – class that is inherited
must do, but not how it
does it.

• Is written documentation
about the program.

15
Heart of Object-Oriented Programming Modeling

• Don't make a new language, extend • All phases of software life-cycle are
your old one. modeling
• analysis - modeling of problem
• Objects should be abstractions of • design - modeling of solution
problem domain. • implementation - making model run on a
computer
• maintenance - fixing/extending your model

17 18

Assumption about Modeling Modeling

• Basing system design on structure of • Claim: people model the world with
problem makes system "objects"
• more reusable • objects
• more extensible • classes
• relationships between objects
• relationships between classes

19 20
Modeling Objects and Relationships

• Advantages of object-oriented software • Ahmet is Ece's father. Ece is Ahmet's


development daughter.
• more natural - matches the way people • Kurt is Kemal's dog. Kemal is Kurt's
think owner.
• single notation - makes it easy to move
• Fatma is Cemal's employer. Cemal is
between software phases
Fatma's employee.

21 22

Objects and Attributes Objects and Behavior

• Ahmet's name is “Ahmet Kara". • Ahmet goes on a trip.


• Ahmet's age is 27. • Ahmet makes reservations.
• Ahmet's address is Nispetiye Cad. No • Ahmet buys tickets.
15/1, Etiler • Ahmet travels by airplane.
• What about Ahmet's job? Ahmet's wife? • Ahmet checks into hotel.

• What is an attribute, and what is a


relationship?

23 24
What Really is an Object? What Really is an Object?

• Reservation -- a promise to give service • Anything we can talk about can be an


to a customer object, including relationships ("the
• Ticket -- proof that customer has paid for husband of the first party", "first-born
service in advance son").
• Flight • What are we trying to model?
• Payment -- an event (transaction?) in • Models should be as simple as possible,
which money is exchanged but no simpler.

25 26

Classification Classification

• We naturally put objects into classes • Ahmet is an employee.


that have similar characteristics. • Ahmet is a father.
• Hasan is a man. Mammals • Ahmet is a sky-diver.
• Ayşe is a woman. • Ahmet is under 30.
Dogs People
• Kurt is a dog.
Women Men
• All women are people. Kurt • Is Ahmet an instance of Employee,
• All people are mammals. Ayşe Hasan Father, Sky-diver, and Under30?

27 28
C++ class data type C++ Classes

• primary instrument of object oriented


• A class is an unstructured type that programming in C++
encapsulates a fixed number of data • allows the user to design true types by
components (data members) with the • abstraction of data about the thing being
functions (called member functions) that modeled and
manipulate them. • encapsulate the results of that abstraction with
the operations that will work on that type
• The predefined operations on an instance • the parts of a class are called its members
of a class are whole assignment and • members can be data objects (variables)
component access. and functions to manipulate the data
objects

29 30

C++ Classes Class Specification (Definition) Statement Syntax

• a class must be designed (naming of class className


all members and the properties of {
those members) and then … member-list …
implemented (statements written)
};
• an instance of a class is created by
declaring an object variable of that where member-list is a listing of the
class functions and data objects of the
class

31 32
Member Access Modifiers Member Access Modifiers

• access modifiers set how a member may be • protected – like private, but can be
accessed
• all members in a class are given an access inherited
modifier • all members of a class are private by
• public – indicates that the member may be default
accessed by code outside of the class and
can be inherited • access is always explicitly stated by
• private – indicates that the member may convention
only be accessed by code inside of the
class and cannot be inherited

33 34

Accessor and Mutator Functions class DateType Specification

• Accessor function member: function that only // SPECIFICATION FILE ( datetype.h )

accesses (does not modify) the value(s) of the class DateType // declares a class data type
{
data member(s)
public : // 4 public member functions
• Mutator function member: function that
void Initialize (int newMonth, int newDay, int newYear ) ;
modifies the value(s) of the data member(s) int YearIs( ) const ; // returns year
int MonthIs( ) const ; // returns month
• Constant function member: function that int DayIs( ) const ; // returns day
cannot modify data members and includes the private : // 3 private data members
reserved word const at the end of the
int year ;
function heading int month ;
int day ;
} ;
35 36
Use of C++ data type class Client Code Using DateType
#include <iostream>
• Variables of a class type are called objects #include “datetype.h” // includes specification of the class
(or instances) of that particular class. using namespace std;
int main ( void )
{
• Software that declares and uses objects of DateType startDate ; // declares 2 objects of DateType
the class is called a client. DateType endDate ;
bool retired = false ;
startDate.Initialize ( 6, 30, 1998 ) ;
• Client code uses public member functions endDate.Initialize ( 10, 31, 2002 ) ;
(called methods in OOP) to handle its class cout << startDate.MonthIs( ) << “/” << startDate.DayIs( )
objects. << “/” << startDate.YearIs( ) << endl;
while ( ! retired )
{ finishSomeTask( ) ;
• Sending a message means calling a public . . .
member function. }
}

37 38

2 separate files generally


used for class type DateType Class Instance Diagrams

// SPECIFICATION FILE ( datetype .h )


// Specifies the data and function members.
class DateType
{
startDate endDate
public:
. . .

private:
. . . Private data: Private data:
} ; Initialize Initialize
year 1998 year 2002
YearIs YearIs
// IMPLEMENTATION FILE ( datetype.cpp ) month 6 month 10
MonthIs MonthIs
day 30 day 31
// Implements the DateType member functions.
DayIs DayIs
. . .

39 40
Implementation of DateType
member functions
int DateType :: MonthIs ( ) const
// IMPLEMENTATION FILE (datetype.cpp) // Accessor function for data member month
#include “datetype.h” // also must appear in client code {
return month ;
void DateType :: Initialize ( int newMonth, int newDay, }
int newYear )
// Post: year is set to newYear. int DateType :: YearIs ( ) const
// month is set to newMonth. // Accessor function for data member year
// day is set to newDay. {
{ return year ;
year = newYear ; }
month = newMonth ;
day = newDay ; int DateType :: DayIs ( ) const
} // Accessor function for data member day
{
return day ;
41 42
42
}

Familiar Class Instances


and Member Functions Scope Resolution Operator ( :: )

• The member selection operator ( . ) selects either data • C++ programs typically use several class types.
members or member functions.
• Different classes can have member functions with the
• Header files iostream and fstream declare the istream, same identifer, like Write( ).
ostream,and ifstream, ofstream I/O classes.
• Member selection operator is used to determine the
• Both cin and cout are class objects and get and ignore class whose member function Write( ) is invoked.
are member functions. currentDate .Write( ) ; // class DateType
cin.get (someChar) ; numberZ .Write( ) ; // class ComplexNumberType
cin.ignore (100, ‘\n’) ;
• In the implementation file, the scope resolution
• These statements declare myInfile as an instance of operator is used in the heading before the member
class ifstream and invoke member function open. function’s name to specify its class.
ifstream myInfile ; void DateType :: Write ( ) const
myInfile.open ( “A:\\mydata.dat” ) ; { . . .

43 } 44
Three Ways to Access Members
Namespace
within a Namespace

namespace mySpace
• Qualify each reference:
{
mySpace::name with every reference.
// All variables and functions within this
• Using declaration:
// block must be accessed using scope using mySpace::name;
// resolution operator (::). All future references to name refer to mySpace::name.
} • Using directive:
Purpose: Avoid namespace pollution. using namespace mySpace;
All members of mySpace can be referenced without
qualification.
45 46

Rules for Use of Namespace std


(within text) Exceptions

• Qualify names in prototypes and/or • An exception is an unusual situation that


function definitions. occurs when the program is running.
• If name used more than once in a • Exception Management
function block, use a using declaration. • Define the error condition
• If more than one name is used from a • Enclose code containing possible error (try).
namespace, use a using directive. • Alert the system if error occurs (throw).
• Handle error if it is thrown (catch).

47 48
try, catch, and throw Exception Example

try #include <iostream>


{ #include <fstream>
// code that contains a possible error #include <string>
… throw string(“An error has occurred in using namespace std;
function …”);
int main()
{
}
catch (string message)
double d, e;
{ ifstream inFile;
std::cout << message << std::endl; inFile.open("data.dat");
return 1;
}
49 50

Exception Example

try
{
inFile >> d;
if (d == 0)
throw string(“Not acceptable");
e = 1.0/d;
}
catch (string message)
{
cout << "Bad Data! Program aborted!" << endl;
exit(1);
}
inFile.close();
cout << e << endl;
return 0;
}
51

You might also like