0% found this document useful (0 votes)
88 views

Object Oriented Programming in C++

Inheritance, encapsulation, and polymorphism are the three primary features of object-oriented programming (OOP). Inheritance allows code to be reused between related classes by defining common functionality in a base class that derived classes inherit from. Encapsulation refers to grouping related data and functions together into classes, hiding implementation details. Polymorphism allows values to be treated as a base type and call the appropriate function at runtime based on the actual derived type through the use of virtual functions.

Uploaded by

xob97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Object Oriented Programming in C++

Inheritance, encapsulation, and polymorphism are the three primary features of object-oriented programming (OOP). Inheritance allows code to be reused between related classes by defining common functionality in a base class that derived classes inherit from. Encapsulation refers to grouping related data and functions together into classes, hiding implementation details. Polymorphism allows values to be treated as a base type and call the appropriate function at runtime based on the actual derived type through the use of virtual functions.

Uploaded by

xob97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

6.

096
IntroductiontoC++ January19,2011
MassachusettsInstituteofTechnology
Lecture 7 Notes: Object-Oriented Programming
(OOP) and Inheritance
Wevealreadyseenhowtodenecompositedatatypesusingclasses.Nowwelltakeastep
backandconsider the programming philosophyunderlyingclasses, knownasobject-oriented
programming (OOP).
1 The Basic Ideas of OOP
Classicproceduralprogramming languages before C++ (suchas C)often focusedon the
questionWhatshouldtheprogramdonext?Thewayyoustructureaprograminthese
languages is:
1. Splititupintoasetoftasksandsubtasks
2. Makefunctionsforthetasks
3. Instructthecomputertoperformtheminsequence
Withlargeamountsofdataand/orlargenumbersoftasks,thismakesforcomplexand
unmaintainable programs.
Considerthetaskofmodelingtheoperationofacar.Suchaprogramwouldhavelotsof
separatevariablesstoringinformationonvariouscarparts,andtheredbenowaytogroup
togetherallthecodethatrelatesto,say,thewheels.Itshardtokeepallthesevariables
andtheconnectionsbetweenallthefunctionsinmind.
Tomanagethiscomplexity,itsnicertopackageupself-sucient,modularpiecesofcode.
People think of the world in terms of interactingobjects:wed talkabout interactions between
the steeringwheel, the pedals, the wheels, etc.OOP allows programmers to packaway details
into neat, self-contained boxes (objects) so that theycan thinkofthe objects more abstractly
andfocusontheinteractionsbetweenthem.
TherearelotsofdenitionsforOOP,but3primaryfeaturesofitare:
Encapsulation: groupingrelated data and functions together as objects and dening
aninterface tothoseobjects
Inheritance: allowingcodetobereusedbetweenrelatedtypes
Polymorphism: allowingavaluetobeoneofseveraltypes,anddeterminingat
runtimewhichfunctionstocallonitbasedonitstype
LetsseehoweachoftheseplaysoutinC++.
2 Encapsulation
Encapsulationjustreferstopackagingrelatedstutogether. Wevealreadyseenhowto
packageupdataandtheoperationsitsupportsinC++:withclasses.
Ifsomeonehandsusaclass,wedonotneedtoknowhowitactuallyworkstouseit;allwe
needtoknowaboutisitspublicmethods/dataitsinterface.Thisisoftencomparedto
operatingacar:whenyoudrive,youdontcarehowthesteeringwheelmakesthewheels
turn;youjustcarethattheinterfacethecarpresents(thesteeringwheel)allowsyouto
accomplishyourgoal. IfyouremembertheanalogyfromLecture6aboutobjectsbeing
boxeswithbuttonsyoucanpush,youcanalsothinkoftheinterfaceofaclassastheset
ofbuttonseachinstanceofthatclassmakesavailable.Interfacesabstractawaythedetails
ofhowalltheoperationsareactuallyperformed,allowingtheprogrammertofocusonhow
objectswilluseeachothersinterfaceshowtheyinteract.
ThisiswhyC++makesyouspecifypublic andprivate accessspeciers:bydefault,it
assumesthatthethingsyoudeneinaclassareinternaldetailswhichsomeoneusingyour
codeshouldnothavetoworryabout.Thepracticeofhidingawaythesedetailsfromclient
codeiscalleddatahiding,ormakingyourclassablackbox.
Onewayto thinkaboutwhat happens in anobject-oriented program is thatwe denewhat
objectsexistandwhateachoneknows,andthentheobjectssendmessagestoeachother
(by callingeachothersmethods)toexchangeinformationandtelleachotherwhattodo.
3 Inheritance
Inheritance allowsustodenehierarchiesofrelatedclasses.
Imagine were writing an inventory program for vehicles, includingcars andtrucks.We could
writeoneclassforrepresentingcarsandanunrelatedoneforrepresentingtrucks,butwed
havetoduplicatethefunctionalitythatallvehicleshaveincommon.Instead,C++allows
ustospecifythecommoncodeinaVehicleclass,andthenspecifythattheCarandTruck
classessharethiscode.
TheVehicleclasswillbemuchthesameaswhatweveseenbefore:
1
2
3
4
class Vehicle {
protected:
string license;
int year;
2
5
6 public:
7 Vehicle(const string &myLicense , const int myYear)
8 : license(myLicense), year(myYear) {}
9 const string getDesc () const
10 {return license + " from " + stringify(year);}
11 const string &getLicense () const {return license ;}
12 const int getYear () const {return year;}
13 };
Afewnotesonthiscode,byline:
2.Thestandardstringclass is described in Section 1of PS3;see there for details.Recall
thatstringscanbeappendedtoeachotherwiththe+ operator.
3.protectedislargelyequivalenttoprivate.Welldiscussthedierencesshortly.
8.Thislinedemonstratesmember initializer syntax.Whendeningaconstructor,you
sometimes want to initialize certain members, particularlyconstmembers, even before
theconstructorbody.Yousimplyputacolonbeforethefunctionbody,followedbya
comma-separatedlistofitemsoftheformdataMember(initialValue).
10.Thislineassumestheexistenceofsomefunctionstringifyfor convertingnumbers to
strings.
NowwewanttospecifythatCar willinherittheVehicle code,butwithsomeadditions.
Thisisaccomplishedinline1below:
1 class Car : public Vehicle { // Makes Car inherit from Vehicle
2 string style;
3
4 public:
5 Car(const string &myLicense , const int myYear , const string
&myStyle)
6 : Vehicle(myLicense , myYear), style(myStyle) {}
7 const string &getStyle () {return style;}
8 };
NowclassCar hasallthedatamembersandmethodsofVehicle,aswellasastyle data
memberandagetStylemethod.
ClassCar inherits from classVehicle. ThisisequivalenttosayingthatCar isaderived
class,whileVehicle is itsbase class.Youmayalsohearthetermssubclass andsuperclass
instead.
Notesonthecode:
3
1.Dontworryfornowaboutwhywestuckthepublic keyword in there.
6.Notehowweusememberinitializersyntaxtocallthebase-classconstructor.Weneed
tohaveacompleteVehicle objectconstructedbeforeweconstructthecomponents
added in theCar. If you do not explicitlycalla base-class constructor using this syntax,
thedefaultbase-classconstructorwillbecalled.
Similarly,wecouldmakeaTruckclass that inherits fromVehicleandshares its code.This
wouldgiveaclass hierarchy like the following:
Vehicle
Truck Car
Classhierarchiesaregenerallydrawnwitharrowspointingfromderivedclassestobase
classes.
3.1 Is-a vs. Has-a
TherearetwowayswecoulddescribesomeclassA asdependingonsomeotherclassB:
1.EveryAobjecthas a Bobject.For instance, everyVehiclehas a stringobject (called
license).
2.EveryinstanceofA is a B instance.Forinstance,everyCar is a Vehicle,aswell.
Inheritanceallowsustodeneis-arelationships,butitshouldnotbeusedtoimplement
has-arelationships. ItwouldbeadesignerrortomakeVehicle inheritfromstring
becauseeveryVehicle hasalicense;aVehicle isnotastring. Has-arelationships
shouldbeimplementedbydeclaringdatamembers,notbyinheritance.
3.2 Overriding Methods
We mightwant to generate the description forCars in a dierentway from genericVehicles.
Toaccomplishthis,wecansimplyredenethegetDesc method inCar,asbelow. Then,
whenwecallgetDesconaCarobject,itwillusetheredenedfunction.Redeninginthis
manneriscalledoverriding the function.
1
2
3
class Car : public
string style;
Vehicle { // Makes Car inherit from Vehicle
4
4 public:
5 Car(const string &myLicense , const int myYear , const string
&myStyle)
6 : Vehicle(myLicense , myYear), style(myStyle) {}
7 const string getDesc () // Overriding this member function
8 {return stringify(year) + + style + ": " + license
;}
9 const string &getStyle () {return style;}
10 };
3.2.1 Programming by Dierence
Indeningderivedclasses,weonlyneedtospecifywhatsdierentaboutthemfromtheir
baseclasses.Thispowerfultechniqueiscalledprogramming by dierence.
Inheritanceallowsonlyoverridingmethodsandaddingnewmembersandmethods. We
cannotremovefunctionalitythatwaspresentinthebaseclass.
3.3 Access Modiers and Inheritance
Ifweddeclaredyear andlicense asprivate inVehicle,wewouldntbeabletoaccess
themevenfromaderivedclasslikeCar.Toallowderivedclassesbutnotoutsidecodeto
accessdatamembersandmemberfunctions,wemustdeclarethemasprotected.
Thepublic keywordused inspecifyinga baseclass (e.g.,class Car : public Vehicle
{...}) gives a limit for the visibility ofthe inheritedmethods in the derivedclass.Normally
youshould justusepublic here,whichmeansthatinheritedmethodsdeclaredaspublic
arestillpublic inthederivedclass.Specifyingprotected wouldmakeinheritedmethods,
even those declaredpublic, have atmostprotectedvisibility.For a fulltable ofthe eects
ofdierentinheritanceaccessspeciers,see
http://en.wikibooks.org/wiki/C++Programming/Classes/Inheritance.
4 Polymorphism
Polymorphism meansmanyshapes. Itreferstotheabilityofoneobjecttohavemany
types. IfwehaveafunctionthatexpectsaVehicle object,wecansafelypassitaCar
object,becauseeveryCarisalsoaVehicle. Likewise for references and pointers:anywhere
youcanuseaVehicle *,youcanuseaCar *.
5
4.1 virtual Functions
Thereisstillaproblem.Takethefollowingexample:
1 Car c("VANITY", 2003);
2 Vehicle *vPtr = &c;
3 cout << vPtr->getDesc();
(The ->notationon line 3 just dereferencesand getsamember.ptr->memberisequivalent
to(*ptr).member.)
BecausevPtris declaredas aVehicle *, this willcalltheVehicleversion ofgetDesc, even
thoughtheobject pointedto isactuallyaCar.Usuallywedwant the program to select the
correctfunctionatruntimebasedonwhichkindofobjectispointedto.Wecangetthis
behaviorbyaddingthekeywordvirtual beforethemethoddenition:
1 class Vehicle {
2 ...
3 virtual const string getDesc () {...}
4 };
Withthisdenition,thecodeabovewouldcorrectlyselecttheCar versionofgetDesc.
Selectingthe correct function atruntime is calleddynamic dispatch. This matches the whole
OOPideaweresendingamessagetotheobjectandlettingitgureoutforitselfwhat
actionsthatmessageactuallymeansitshouldtake.
Becausereferencesareimplicitlyusingpointers,thesameissuesapplytoreferences:
1 Car c("VANITY", 2003);
2 Vehicle &v = c;
3 cout << v.getDesc();
ThiswillonlycalltheCar versionofgetDescifgetDescisdeclaredasvirtual.
OnceamethodisdeclaredvirtualinsomeclassC,itisvirtualineveryderivedclassofC,
evenifnotexplicitlydeclaredassuch.However,itisagoodideatodeclareitasvirtual
inthederivedclassesanywayforclarity.
4.2 Pure virtual Functions
Arguably, there is no reasonable wayto denegetDescfor a genericVehicle only derived
classesreallyneedadenitionofit,sincethereisnosuchthingasagenericvehiclethat
isntalso a car, truck, or the like.Still,we do want to require every derivedclass ofVehicle
to have this function.
6
WecanomitthedenitionofgetDesc fromVehicle bymakingthefunctionpure virtual
viathefollowingoddsyntax:
1 class Vehicle {
2 ...
3 virtual const string getDesc () = 0; // Pure virtual
4 };
The= 0indicates thatno denition will be given.This implies thatone can no longer create
aninstanceofVehicle;onecanonlycreateinstancesofCars,Trucks,andotherderived
classeswhichdoimplementthegetDesc method.Vehicle isthenanabstract class one
whichdenesonlyaninterface,butdoesntactuallyimplementit,andthereforecannotbe
instantiated.
5 Multiple Inheritance
Unlikemanyobject-orientedlanguages,C++allowsaclasstohavemultiplebaseclasses:
1 class Car : public Vehicle, public InsuredItem {
2 ...
3 };
This species that Carshould have all the members of both theVehicleand theInsuredItem
classes.
Multipleinheritanceistrickyandpotentiallydangerous:
If bothVehicle andInsuredItem deneamemberx,youmustremembertodisam-
biguatewhichoneyourereferringtobysayingVehicle::xorInsuredItem::x.
If bothVehicle andInsuredItem inheritedfromthesamebaseclass,youdendup
withtwoinstancesofthebaseclasswithineachCar(adreadeddiamond classhier-
archy).Therearewaystosolvethisproblem,butitcangetmessy.
Ingeneral,avoidmultipleinheritanceunlessyouknowexactlywhatyouredoing.
7
MIT OpenCourseWare
http://ocw.mit.edu
6.096 Introduction to C++
January (IAP) 2011
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

You might also like