Download - C++: Interface as Concept
![Page 1: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/1.jpg)
‐Recap:C++‐MemoryHandling–OO:interfaceandexamplesofOOLanguages
JussiPohjolainenTAMKUniversityofAppliedSciences
![Page 2: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/2.jpg)
StackBasedMemoryAllocaJon
• RegionofMemory• DataisaddedinLast‐In‐First‐Outmanner– StackallocaJonverysimple,fasterthanheapallocaJon
– ReleasingisautomaJc
– Stacksizeislimitedandusuallysmallerthanheap.• (InSymbianC++,stacksizeisonly8Kb)
![Page 3: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/3.jpg)
StackOverflow
• StackoverflowoccurswhentoomuchmemoryisusedontheStack– Itispossibletochangethestacksize(differsindifferentoperaJngsystems)
• StackoverflowhappensusuallywhenaXempJngtolocalarrayvariablewithverybigsize.
![Page 4: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/4.jpg)
DynamicMemoryAllocaJon
• DynamicmemoryallocaJonistheallocaJonofmemorystorageforuseinacomputerprogramduringtherunJmeofthatprogram.
• DynamicallyallocatedmemoryexistsunJlitisreleased– Stack:fixedduraJon
• HeapMemoryArea
![Page 5: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/5.jpg)
UseofHeap
• WhencontrollinglifeJmeofthevariable• "Big"variables• WhendecidingtheamountofallocatedmemoryinrunJme.
![Page 6: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/6.jpg)
Result?#include <iostream>
using namespace std;
class Cat {
};
int main() { cout << new Cat() << endl;
return 0; }
![Page 7: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/7.jpg)
Result?
int main() { // This is variable, which // is stored in stack-memory Cat* mirri; // Creates the Cat mirri = new Cat(); return 0; }
![Page 8: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/8.jpg)
HowmanyallocaJonstoStackandhowmanytoHeap?
int main() { Cat* mirri1 = new Cat();
Cat* mirri2 = mirri1; return 0; }
![Page 9: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/9.jpg)
Doesthiswork?#include <iostream>
using namespace std;
class Car { public: string brand_; public: Car(string brand) : brand_(brand) { } };
int main() { Car datsun1("datsun"); Car datsun2 = datsun1;
return 0; }
![Page 10: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/10.jpg)
It'sthesamethan...#include <iostream>
using namespace std;
class Car { public: string brand_; public: Car(string brand) : brand_(brand) { } };
int main() { Car datsun1("datsun"); Car datsun2(datsun1);
return 0; }
![Page 11: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/11.jpg)
AndC++makesDefaultCopyConstructor...
#include <iostream>
using namespace std;
class Car { public: string brand_; public: Car(string brand) : brand_(brand) { } Car(const Car& car) : brand_(car.brand_) { } };
int main() { Car datsun1("datsun"); Car datsun2(datsun1);
return 0; }
![Page 12: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/12.jpg)
Andthisisthesame..#include <iostream>
using namespace std;
class Car { public: string brand_; public: Car(string brand) : brand_(brand) { } Car(const Car& car) { brand_ = car.brand_; } };
int main() { Car datsun1("datsun"); Car datsun2 = datsun1;
return 0; }
![Page 13: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/13.jpg)
ComposiJonclass Motor { };
class Car { private: Motor* motor; public: Car() { motor = new Motor(); } ~Car() { delete motor; } };
int main() { Car datsun1();
return 0; }
![Page 14: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/14.jpg)
WithIniJalizaJonListclass Motor { };
class Car { private: Motor* motor; public: Car() : motor(new Motor()) { } ~Car() { delete motor; } };
int main() { Car datsun1();
return 0; }
![Page 15: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/15.jpg)
Whathappensnow?class Motor { };
class Car { private: Motor* motor; public: Car() : motor(new Motor()) { } ~Car() { delete motor; } };
int main() { Car datsun1; Car datsun2 = datsun1;
return 0; }
![Page 16: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/16.jpg)
DefaultCopyConstructorclass Motor { };
class Car { private: Motor* motor; public: Car() : motor(new Motor()) { } Car(const Car& car) { motor = car.motor; } ~Car() { delete motor; } };
int main() { Car datsun1; Car datsun2 = datsun1;
return 0; }
ThisisthesituaJonwhereyouhavetoimplementyourownversionof
thecopyconstructor!(and=operaJonoverload..)
![Page 17: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/17.jpg)
AnyProblemHere?class Motor { };
class Vehicle { };
class Car : public Vehicle {
private: Motor* motor; public: Car() : motor(new Motor()){} ~Car() { delete motor;
} };
int releaseVehicle(Vehicle* v) { delete v; }
int main() {
Car* mycar = new Car(); releaseVehicle(mycar); return 0; }
![Page 18: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/18.jpg)
SoluJonclass Motor { };
class Vehicle { public: virtual ~Vehicle() { } };
class Car : public Vehicle { private: Motor* motor; public:
Car() : motor(new Motor()){} ~Car() { delete motor; } };
int releaseVehicle(Vehicle* v) { delete v; }
int main() {
Car* mycar = new Car(); releaseVehicle(mycar); return 0; }
![Page 19: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/19.jpg)
INTERFACEASCONCEPT
![Page 20: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/20.jpg)
RemoteControl
• Project:RemoteControlledCarSystem• Projectgroup:Pete(RemoteControl)andJack(Car)
![Page 21: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/21.jpg)
InC++class RemoteControl { private: Car* car_; public: RemoteControl(Car* car) { car_ = car; } void keyVolumeUp() { car_->accelerate(); } void keyVolumeDown() { car_->decelerate(); } ... }
![Page 22: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/22.jpg)
WorkFlow
Pete'sresponsibility Jack'sresponsibility
Agreement?WhatarethemethodsinCarthattheRemotecancall?
![Page 23: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/23.jpg)
SoPeteImplementsthis..class RemoteControl { private: Car* car_; public: RemoteControl(Car* car) { car_ = car; } void keyVolumeUp() { car_->accelerate(); } void keyVolumeDown() { car_->decelerate(); } ... }
![Page 24: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/24.jpg)
AndJackImplementsthis..Wehaveaproblem.
class Car { public: void driveFaster() { ... }
void driveSlower() { ... } ... }
![Page 25: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/25.jpg)
MaintananceandReusability?
• PeteandJackmadesaagreementonmethodnamesandhowthetwoobjectsshouldworkwitheachother
• Theprojectwentsowell,thatthecompanygetsanotherorder:– RemotecontrolledAirplane
• Weknowthatwehaveplentyofcodefromthepreviousprojectbutthecodewasnotimplementedreusabilityinmind..
![Page 26: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/26.jpg)
CoulditbepossibletoimplementtheAirplanewithouttouchingtheRemote?
Pete'sresponsibility
Jack'sresponsibility
![Page 27: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/27.jpg)
Agreement,Interface?
• PeteandshouldimplementtheRemotesothatitisreusable.
• TheyshouldmakewithJackagreement(interface)definedalsointhecode.
• Thetargetobjectshouldfollowtheinterfaceandtheremotecontroliscontrollingthetargetobjectviatheinterface
interface
![Page 28: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/28.jpg)
Interface?
• Interface:abstractclasswithonlyabstractmethods.– Classdoesnothaveanyconcretemethods,onlyabstractmethods.
• PeteandJackagreesthattheremotecancontrolanymovableobject,thathasfollowingmethods:– start– accelerate– declerate– stop
![Page 29: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/29.jpg)
ImplemenJngtheInterface
class Movable { public: virtual void start() = 0; virtual void stop() = 0; virtual void accelerate() = 0; virtual void declerate() = 0; };
![Page 30: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/30.jpg)
CarandAirplaneclass Car : public Movable { public: void start() { ... } void stop() { ... } void accelerate() { ... } void declerate() { ... } }
class Airplane : public Movable { public: void start() { ... } void stop() { ... } void accelerate() { ... } void declerate() { ... } }
![Page 31: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/31.jpg)
Remoteclass RemoteControl { private: Movable* movable_; public: RemoteControl(Movable* movable_) { movable_ = movable_; } void keyVolumeUp() { movable_->accelerate(); } void keyVolumeDown() { movable_->decelerate(); } }
![Page 32: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/32.jpg)
Usage
int main() { Car* datsun = new Car(); RemoteControl rc = new RemoteControl(datsun); for(int i=0; i<10; i++) rc->keyVolumeUp(); ... }
![Page 33: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/33.jpg)
Usage
int main() { Airplane* ap = new Airplane(); RemoteControl rc = new RemoteControl(ap); for(int i=0; i<10; i++) rc->keyVolumeUp(); ... }
![Page 34: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/34.jpg)
MulJpleInheritance
class Car : public Movable, public Vehicle { public: void start() { ... } void stop() { ... } void accelerate() { ... } void declerate() { ... } }
![Page 35: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/35.jpg)
Thisdoesnotwork,why?class RemoteControl { private: Vehicle* vehicle_; public: RemoteControl(Vehicle* vehicle) { vehicle_= vehicle; } void keyVolumeUp() { vehicle_->accelerate(); } void keyVolumeDown() { vehicle_->decelerate(); } };
![Page 36: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/36.jpg)
Doesitworknow?
![Page 37: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/37.jpg)
RemoteControlledHuman?
• Wewouldlikethatthesameremotecontrolwouldworkonhumanstoo.
• Isthistheway?
![Page 38: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/38.jpg)
BeXerway
![Page 39: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/39.jpg)
InterfaceinC++
• InterfaceinC++isjustaclassthathasonlyabstractmethods– Couldleadtoproblems,howdoyouseeincodewhatisinterfaceandwhatisconcreteclass?• class Car : public Vehicle, public Movable
• SymbianC++soluJon:namingstandard:– class CCar: public CVehicle, public MMovable
• Java:– class Car extends Vehicle implements Movable
![Page 40: C++: Interface as Concept](https://reader033.vdocuments.site/reader033/viewer/2022052900/555e366fd8b42a63048b4990/html5/thumbnails/40.jpg)
Examples
• OOisusedalmostnowdaysalmostineveryProgramminglanguage!
• Java• PHP• SymbianC++
• ...