40
‐ Recap: C++ ‐ Memory Handling – OO: interface and examples of OO Languages Jussi Pohjolainen TAMK University of Applied Sciences

C++: Interface as Concept

Embed Size (px)

Citation preview

Page 1: C++: Interface as Concept

‐Recap:C++‐MemoryHandling–OO:interfaceandexamplesofOOLanguages

JussiPohjolainenTAMKUniversityofAppliedSciences

Page 2: C++: Interface as Concept

StackBasedMemoryAllocaJon

•  RegionofMemory•  DataisaddedinLast‐In‐First‐Outmanner–  StackallocaJonverysimple,fasterthanheapallocaJon

–  ReleasingisautomaJc

–  Stacksizeislimitedandusuallysmallerthanheap.•  (InSymbianC++,stacksizeisonly8Kb)

Page 3: C++: Interface as Concept

StackOverflow

•  StackoverflowoccurswhentoomuchmemoryisusedontheStack–  Itispossibletochangethestacksize(differsindifferentoperaJngsystems)

•  StackoverflowhappensusuallywhenaXempJngtolocalarrayvariablewithverybigsize.

Page 4: C++: Interface as Concept

DynamicMemoryAllocaJon

•  DynamicmemoryallocaJonistheallocaJonofmemorystorageforuseinacomputerprogramduringtherunJmeofthatprogram.

•  DynamicallyallocatedmemoryexistsunJlitisreleased– Stack:fixedduraJon

•  HeapMemoryArea

Page 5: C++: Interface as Concept

UseofHeap

•  WhencontrollinglifeJmeofthevariable•  "Big"variables•  WhendecidingtheamountofallocatedmemoryinrunJme.

Page 6: C++: Interface as Concept

Result?#include <iostream>

using namespace std;

class Cat {

};

int main() { cout << new Cat() << endl;

return 0; }

Page 7: C++: Interface as Concept

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

HowmanyallocaJonstoStackandhowmanytoHeap?

int main() { Cat* mirri1 = new Cat();

Cat* mirri2 = mirri1; return 0; }

Page 9: C++: Interface as Concept

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

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

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

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

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

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

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

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

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

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

INTERFACEASCONCEPT

Page 20: C++: Interface as Concept

RemoteControl

•  Project:RemoteControlledCarSystem•  Projectgroup:Pete(RemoteControl)andJack(Car)

Page 21: C++: Interface as Concept

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

WorkFlow

Pete'sresponsibility Jack'sresponsibility

Agreement?WhatarethemethodsinCarthattheRemotecancall?

Page 23: C++: Interface as Concept

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

AndJackImplementsthis..Wehaveaproblem.

class Car { public: void driveFaster() { ... }

void driveSlower() { ... } ... }

Page 25: C++: Interface as Concept

MaintananceandReusability?

•  PeteandJackmadesaagreementonmethodnamesandhowthetwoobjectsshouldworkwitheachother

•  Theprojectwentsowell,thatthecompanygetsanotherorder:– RemotecontrolledAirplane

•  Weknowthatwehaveplentyofcodefromthepreviousprojectbutthecodewasnotimplementedreusabilityinmind..

Page 26: C++: Interface as Concept

CoulditbepossibletoimplementtheAirplanewithouttouchingtheRemote?

Pete'sresponsibility

Jack'sresponsibility

Page 27: C++: Interface as Concept

Agreement,Interface?

•  PeteandshouldimplementtheRemotesothatitisreusable.

•  TheyshouldmakewithJackagreement(interface)definedalsointhecode.

•  Thetargetobjectshouldfollowtheinterfaceandtheremotecontroliscontrollingthetargetobjectviatheinterface

interface

Page 28: C++: Interface as Concept

Interface?

•  Interface:abstractclasswithonlyabstractmethods.–  Classdoesnothaveanyconcretemethods,onlyabstractmethods.

•  PeteandJackagreesthattheremotecancontrolanymovableobject,thathasfollowingmethods:–  start–  accelerate–  declerate–  stop

Page 29: C++: Interface as Concept

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

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

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

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

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

MulJpleInheritance

class Car : public Movable, public Vehicle { public: void start() { ... } void stop() { ... } void accelerate() { ... } void declerate() { ... } }

Page 35: C++: Interface as Concept

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

Doesitworknow?

Page 37: C++: Interface as Concept

RemoteControlledHuman?

•  Wewouldlikethatthesameremotecontrolwouldworkonhumanstoo.

•  Isthistheway?

Page 38: C++: Interface as Concept

BeXerway

Page 39: C++: Interface as Concept

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

Examples

•  OOisusedalmostnowdaysalmostineveryProgramminglanguage!

•  Java•  PHP•  SymbianC++

•  ...