Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Oriented Programming
Conditionals, loops, se uence, e!pression and assignments "ata (variables, arra#s, structures) are separated from t$eir operations It provides an abstraction of t$e $ardware% &ou 'now t$is from COP()*+
Object-oriented programming
v
,uilt on top of structural (procedural) programming Programming based on t$e concept of object%
Objects bundle data wit$ t$eir operations% -nables information $iding, w$ic$ allow us to organi.e t$e program in a more manageable wa#%
Object-Oriented basics
/ fundamental concept in an object-oriented language is t$e encapsulation of data and procedures (functions) toget$er into units called objects%
q
Name 1 a wa# of referring to an object inside a program (eg% / 2raction object mig$t be called 2*)% Member Data 1 data contained wit$in an object (eg% 2raction $as an integer numerator and denominator)% Member Functions 1 routines t$at act upon t$e data wit$in an object (eg% 3$e fraction object mig$t $ave a function t$at returns t$e decimal representation of t$e fraction stored)% Interface 1 defines t$e wa#s a programmer ma# directl# access t$e member data4functions of an object (more on t$is ne!t lecture)%
Classes
q
/ class is anot$er fundamental concept in an object-oriented language t$at provides a blueprint for a new t#pe (5classification5) of object%
v
/ class outlines t$e data, functions and t$e interface objects of t$at class will receive% / class also defines $ow objects of t$at class be$ave b# providing code t$at implements t$e functions associated wit$ t$e class% / programmer can create one or more objects from a class
"eclare a class
C$oose w$at objects of t$is class will store (member variables), and $ow objects will be$ave (member functions)%
&ou can declare an new object instance of #our class just li'e declaring an# ot$er variable (eg% int !)%
3$ere are two wa#s to provide t$e member function definitions for a class0
Inside t$e class declaration using 8< (we will not use) /fter t$e class declaration (t$is is t$e met$od we c$oose)
3$is identifier refers to t$e member function memberFunctionName of class className (e%g% Circle00Set:adius) 3$e double colon 00 is called t$e scope resolution operator
/fter t$e class declaration, member functions are defined just li'e an# ot$er function
Object 7se
/fter a class $as been declared and defined, an object of t$at class can be declared (also 'nown as creation or instantiation) and used, a class is just li'e anot$er t#pe (int, c$ar, etc)% / programmer can declare an object wit$ t$e following format0
ClassName ObjectName;
3$is statement creates an object based on t$e blueprint of class @ClassName? and t$e object can be referred to b# t$e identifier (variable name) @ObjectName? 3$e @ % ? (dot) operator can be used to access an object?s public members 3$e format for referring to an object?s member is0 ObjectName.MemberFunction() O: ObjectName.MemberVariable
declares t$e class Circle and outlines its members and interface defines t$e implementation for t$e member functions of t$e Circle class declares two objects of t$e class Circle, referred to as C* and CA uses t$e interfaces of C* and CA to store t$e radius of two circles and later to calculate t$e area of t$ose circles
Summar#
q
/n object is a unit t$at encapsulates data and functions% It $as four elements0 a name, data members, function members, and an interface% / class specifies t$e (user-defined) form of objects% 3$e use of an object in a CBB program follows t$e declare, define, and use se uence% C$at does scope resolution operator (00) doD C$at does t$e dot operator (%) doD