Sei sulla pagina 1di 11

Protection Levels and Constructors Introduction to Object-

Oriented Programming

Structural programing and object-oriented programming


q

Structural (procedural) programming


v

Programming using well defined control structures


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

/n object consists of0

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

Similar to building multiple $ouses from one set of blueprints%

6ow to define and use a class in a program

""7 1 "eclare, "efine, 7se

"eclare a class

C$oose w$at objects of t$is class will store (member variables), and $ow objects will be$ave (member functions)%

"efine member functions

Provide an implementation for t$e member functions in t$e class%

7se class to create objects

&ou can declare an new object instance of #our class just li'e declaring an# ot$er variable (eg% int !)%

-!ample Class "eclaration


class Circle 8 public0 49 interface, we will cover later 94
void Set:adius(double r); 49 sets member variable radius to r 94 double /reaOf(); 49 returns area of circle as a double 94 double radius; 49 radius of circle stored as double 94

<; 49 don5t forget 5;5 94

"efine =ember 2unctions

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)

:efer to a member function0 className::memberFuntionName

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

-!ample member function definition


44"eclaration0 class Circle 8 public0 void Set:adius(double r); 49sets member variable radius to r 94 double /reaOf(); 49 returns area of circle as a double 94 private0 double radius; 49 radius of circle 94 <; 49 "efinition (Implementation) 94 void Circle00Set:adius(double r) 8 radius > r; 49 radius refers to t$is object?s member variable 94 < double Circle00/reaOf() 8 return ((%*+9radius9radius); <

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

Putting it /ll 3oget$er


q

See sample*%cpp 3o recap, t$is program0


v

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

Potrebbero piacerti anche