-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
session6 added beoordeling pdf, inhertiance instrument assignment exa…
…mples and extra polymorphism code
- Loading branch information
1 parent
f927226
commit 7cd4578
Showing
24 changed files
with
494 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
class Instrument { | ||
public: | ||
Instrument() : name("default name"), sound("default sound") { | ||
std::cout << "Constructor - Instrument\n"; | ||
} | ||
void play() { | ||
std::cout << "\n" << name << ": " << sound << "\n"; | ||
} | ||
protected: | ||
std::string name; | ||
std::string sound; | ||
}; | ||
|
||
class Trumpet : public Instrument { | ||
public: | ||
Trumpet() : Instrument() { | ||
std::cout << "Constructor - Trumpet\n"; | ||
name = "Trumpet"; | ||
sound = "Tetete"; | ||
} | ||
}; | ||
|
||
|
||
|
||
int main() { | ||
Trumpet aTrumpet; | ||
aTrumpet.play(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
class Instrument { | ||
public: | ||
Instrument() { | ||
std::cout << "Constructor - Instrument\n"; | ||
} | ||
virtual void play() { | ||
std::cout << "\nAn instrument is playing, no idea how it sounds...\n"; | ||
} | ||
}; | ||
|
||
class Trumpet : public Instrument { | ||
public: | ||
Trumpet() : Instrument() { | ||
std::cout << "Constructor - Trumpet\n"; | ||
} | ||
void play() { | ||
std::cout << "\nA trumpet is playing: Teteetestete\n"; | ||
} | ||
}; | ||
|
||
|
||
|
||
int main() { | ||
Trumpet aTrumpet; | ||
aTrumpet.play(); | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CXX=g++ | ||
CXXFLAGS=-Wall -std=c++14 | ||
LDFLAGS= | ||
SOURCE= main.o | ||
TARGET=example | ||
|
||
$(TARGET): $(SOURCE) | ||
$(CXX) -o $@ $(SOURCE) $(LDFLAGS) | ||
|
||
.cpp.o: | ||
$(CXX) -c $(CXXFLAGS) $< | ||
clean: | ||
rm -f $(TARGET) *.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "saw.h" | ||
#include "sine.h" | ||
|
||
/* | ||
* Simple example of the need for virtual methods | ||
*/ | ||
|
||
int main() | ||
{ | ||
/* | ||
* Create the sine and saw objects | ||
*/ | ||
Sine sine; | ||
Saw saw; | ||
|
||
/* | ||
* because the base class calculate method is overridden in both subclasses, | ||
* the calculate method of the subclass is called, instead of the calculate | ||
* method of the baseclass. | ||
*/ | ||
sine.calculate(); | ||
saw.calculate(); | ||
|
||
|
||
/* | ||
* Now let's store the address of the sine object in an Oscillator pointer! | ||
* Oscillator* --> Oscillator pointer | ||
* &sine --> with & you retrieve the address of an object | ||
* both the usage of the * and & symbols are simply convention | ||
*/ | ||
Oscillator* osc = &sine; | ||
/* | ||
* OOF....! - when we call the calculate method of the sine object with an | ||
* oscillator pointer ... it will go 'terribly wrong' | ||
* the calculate function of the baseclass is called, not the subclass method. | ||
* FIXME add the virtual keyword to the calculate method | ||
*/ | ||
|
||
osc->calculate(); | ||
/* | ||
* We can also store the address of the saw in the same oscillator pointer | ||
*/ | ||
osc = &saw; | ||
osc->calculate(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
|
||
class Oscillator | ||
{ | ||
public: | ||
Oscillator() {std::cout << "Oscillator::Oscillator -- constructor\n";} | ||
~Oscillator() {std::cout << "Oscillator::~Oscillator -- destructor\n";} | ||
|
||
void calculate() { std::cout << "Oscillator::calculate\n";} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include "oscillator.h" | ||
|
||
class Saw : public Oscillator | ||
{ | ||
public: | ||
Saw() {std::cout << "Saw::Saw -- constructor \n";} | ||
~Saw() {std::cout << "Saw::~Saw -- destructor \n";} | ||
|
||
//override calculate from base class | ||
void calculate() {std::cout << "Saw::calculate\n";} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include "oscillator.h" | ||
|
||
class Sine : public Oscillator | ||
{ | ||
public: | ||
Sine() {std::cout << "Sine::Sine -- constructor\n";} | ||
~Sine() {std::cout << "Sine::~Sine -- destructor\n";} | ||
|
||
//override calculate from base class | ||
void calculate() {std::cout << "Sine::calculate\n";} | ||
}; |
14 changes: 14 additions & 0 deletions
14
blok2b/session6/03_polymorphism_dynamicAllocation/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CXX=g++ | ||
CXXFLAGS=-Wall -std=c++14 | ||
LDFLAGS= | ||
SOURCE= main.o | ||
TARGET=example | ||
|
||
$(TARGET) : $(SOURCE) | ||
$(CXX) -o $@ $(SOURCE) $(LDFLAGS) | ||
|
||
.cpp.o: | ||
$(CXX) -c $< $(CXXFLAGS) | ||
|
||
clean: | ||
rm -f $(TARGET) *.o |
65 changes: 65 additions & 0 deletions
65
blok2b/session6/03_polymorphism_dynamicAllocation/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "saw.h" | ||
#include "sine.h" | ||
|
||
/* | ||
* Simple example of the need for virtual methods | ||
* We allocate a sine and square object dynamically | ||
*/ | ||
|
||
int main() | ||
{ | ||
#if 0 // build example 1 | ||
|
||
/* | ||
* Create the sine and saw objects dynamically with the keyword 'new' | ||
* This returns a pointer to the sine object, which we can store in | ||
* a 'class pointer', e.g. a Sine pointer or a Saw pointer | ||
* a pointer type is indicated with a *, which is simply a convention. | ||
*/ | ||
Sine* sine = new Sine(); | ||
Saw* saw = new Saw(); | ||
|
||
/* | ||
* because the base class calculate method is overridden in both subclasses, | ||
* the calculate method of the subclass is called, instead of the calculate | ||
* method of the baseclass. | ||
*/ | ||
sine->calculate(); | ||
saw->calculate(); | ||
|
||
/* | ||
* Now let's store the pointers to the sine and saw objects in an Oscillator | ||
* pointer! | ||
*/ | ||
Oscillator* sineOscillator = sine; | ||
Oscillator* sawOscillator = saw; | ||
|
||
/* | ||
* OOF....! - when we call the calculate method of the sine object with an | ||
* oscillator pointer ... it will go 'terribly wrong' | ||
* the calculate function of the baseclass is called, not the subclass method. | ||
* FIXME add the virtual keyword to the calculate method | ||
*/ | ||
sineOscillator->calculate(); | ||
sawOscillator->calculate(); | ||
|
||
// delete dynamically allocated objects | ||
delete sine; | ||
sine = nullptr; | ||
delete saw; | ||
saw = nullptr; | ||
|
||
|
||
#else //build example | ||
Oscillator* osc = new Sine(); | ||
osc->calculate(); | ||
/* | ||
* OOF#2....! - We are allowed to store a pointer to a Sine object in a pointer of the type Oscilllator. | ||
* however ... when we delete the object with the delete keyword, the destructor of the subclass is not called | ||
* FIXME add the virtual keyword to the destructor method in the base class | ||
*/ | ||
|
||
delete osc; | ||
osc = nullptr; | ||
#endif | ||
} |
12 changes: 12 additions & 0 deletions
12
blok2b/session6/03_polymorphism_dynamicAllocation/oscillator.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
|
||
class Oscillator | ||
{ | ||
public: | ||
Oscillator() {std::cout << "Oscillator::Oscillator -- constructor\n";} | ||
~Oscillator() {std::cout << "Oscillator::~Oscillator -- destructor\n";} | ||
|
||
void calculate() { std::cout << "Oscillator::calculate\n";} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include "oscillator.h" | ||
|
||
class Saw : public Oscillator | ||
{ | ||
public: | ||
Saw() {std::cout << "Saw::Saw -- constructor \n";} | ||
~Saw() {std::cout << "Saw::~Saw -- destructor \n";} | ||
|
||
//override calculate from base class | ||
void calculate() {std::cout << "Saw::calculate\n";} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include "oscillator.h" | ||
|
||
class Sine : public Oscillator | ||
{ | ||
public: | ||
Sine() {std::cout << "Sine::Sine -- constructor\n";} | ||
~Sine() {std::cout << "Sine::~Sine -- destructor\n";} | ||
|
||
//override calculate from base class | ||
void calculate() {std::cout << "Sine::calculate\n";} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CXX=g++ | ||
CXXFLAGS=-Wall -std=c++14 | ||
LDFLAGS= | ||
SOURCE= main.o | ||
TARGET=example | ||
|
||
$(TARGET): $(SOURCE) | ||
$(CXX) -o $@ $(SOURCE) $(LDFLAGS) | ||
|
||
.cpp.o: | ||
$(CXX) -c $(CXXFLAGS) $< | ||
clean: | ||
rm -f $(TARGET) *.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "saw.h" | ||
#include "sine.h" | ||
|
||
/* | ||
* Simple example of the need for virtual methods | ||
*/ | ||
|
||
int main() | ||
{ | ||
/* | ||
* Create the sine and saw objects | ||
*/ | ||
Sine sine; | ||
Saw saw; | ||
|
||
/* | ||
* because the base class calculate method is overridden in both subclasses, | ||
* the calculate method of the subclass is called, instead of the calculate | ||
* method of the baseclass. | ||
*/ | ||
sine.calculate(); | ||
saw.calculate(); | ||
|
||
|
||
/* | ||
* Now let's store the address of the sine object in an Oscillator pointer! | ||
* Oscillator* --> Oscillator pointer | ||
* &sine --> with & you retrieve the address of an object | ||
* both the usage of the * and & symbols are simply convention | ||
*/ | ||
Oscillator* osc = &sine; | ||
/* | ||
* OOF....! - when we call the calculate method of the sine object with an | ||
* oscillator pointer ... it will go 'terribly wrong' | ||
* the calculate function of the baseclass is called, not the subclass method. | ||
* FIXME add the virtual keyword to the calculate method | ||
*/ | ||
|
||
osc->calculate(); | ||
/* | ||
* We can also store the address of the saw in the same oscillator pointer | ||
*/ | ||
osc = &saw; | ||
osc->calculate(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
|
||
class Oscillator | ||
{ | ||
public: | ||
Oscillator() {std::cout << "Oscillator::Oscillator -- constructor\n";} | ||
~Oscillator() {std::cout << "Oscillator::~Oscillator -- destructor\n";} | ||
|
||
void calculate() { std::cout << "Oscillator::calculate\n";} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include "oscillator.h" | ||
|
||
class Saw : public Oscillator | ||
{ | ||
public: | ||
Saw() {std::cout << "Saw::Saw -- constructor \n";} | ||
~Saw() {std::cout << "Saw::~Saw -- destructor \n";} | ||
|
||
//override calculate from base class | ||
void calculate() {std::cout << "Saw::calculate\n";} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include "oscillator.h" | ||
|
||
class Sine : public Oscillator | ||
{ | ||
public: | ||
Sine() {std::cout << "Sine::Sine -- constructor\n";} | ||
~Sine() {std::cout << "Sine::~Sine -- destructor\n";} | ||
|
||
//override calculate from base class | ||
void calculate() {std::cout << "Sine::calculate\n";} | ||
}; |
Oops, something went wrong.