Professional Documents
Culture Documents
The Object-Oriented Approach: Junaed Sattar September 24, 2008
The Object-Oriented Approach: Junaed Sattar September 24, 2008
The Object-Oriented Approach: Junaed Sattar September 24, 2008
Today
Adaptation to change
non OO approach can be robust, structured but lacks flexibility maintainability (a.k.a. service packs) extensibility (newer releases of software) distributed design and development
C++ as an OO language?
has a large community of users multiparadigm (remember?) performance legacy code access and reuse
thriving community of developers high quality compilers, development aids documentation, forums, organizations
Multiparadigm
unlike Java
fast code, small memory footprint OO design leads to easy performance tuning makes it easy to port C code to C++
OO Fundamentals
Aren't they the same thing? Classes are like blueprints; they are the specifications, not instantiations Objects are like houses built from blueprints. It really exists.
Classes
The packaging unit in OO programming localizes (i.e. packages) data and member functions
Classes
imagine a software specification of a car service accelerate should only affect state speed
OO and N-OO
C:
floatspeed=0.0; //thisisaglobal ... voidAccelerate() { speed+=0.1; }
C++
classCar{ floatspeed; ... ... voidAccelerate() { speed+=0.1; } }
Objects
Qualities of an object?
not just a package for data and methods fundamental purpose is to provide services hence should be alive, responsible, intelligent
Alive?
contrast with dead data in procedural languages dead data are declared but lie in memory, not used most of the time data must not be garbage i.e. uninitialized dynamic memory
Responsible?
objects are self contained inner workings are protected should perform task in a reliable, consistent manner
Intelligent?
Intelligent?
provides services to other users removes complexity from many to few o.e. why not airplane rentals?
First steps
roles and responsibilities what services to provide? why does it exist? is it required?
Public elements
accessible from outside the class NOT accessible from outside the class we'll see soon.
Private elements
Protected?
Synopsis
specifying private at the start of the declaration is optional can have many more, just an example for now two special ones
More Synopsis
always called during instantiation compiler provides default (invisible) dummy constructor provide our own when data initialization is required compiler provides default as well perform data clean up in here
Note
Providing our own constructor (and/or destructor) will disable the default constructor given by the compiler.
constant member functions, or methods cannot change member variables C++'s way of enforcing read-only access to member variables will generate compiler error if changes to class member variables are made in constant methods
An alternative declaration/definition
classVehicle{ private: intnPass; doublegasCapacity; boolHybrid; floatfSpeed; public: Vehicle(){ nPass=5; gasCapacity=100; Hybrid=true; fSpeed=0.0f; } ~Vehicle(){};//Destructor boolLoadPassengers(intnp) { if(nPass<=np)nPass=np; } ...
...Continued
voidAccelerate(){ fSpeed+=5.0; } boolisHybrid()const{returnHybrid;} floatGetSpeed()const{returnfSpeed;} };//classdeclarationends
Inline functions
expanded by the preprocessor like a macro in C equivalent to a C define preprocessor statement also can use the inline keyword
Alternate syntax
classVehicle{ ... boolLoadPassengers(int np) { if(nPass<=np) nPass=np; } classVehicle{ ... boolLoadPassengers(int np); }; inlinebool Vehicle::LoadPassengers( intnp) { if(nPass<=np) nPass=np; }
Inline functions
Use inline for short, frequently-used methods Functions that contain static data, loops, switch statements, or recursive calls cannot be inlined
Instantiation
Creating an object
Can only access public methods or data Privates are hidden (i.e. encapsulated)
Next class
More about access protection encapsulation object pointers and dynamic objects