Skip to content

Commit

Permalink
session6 added beoordeling pdf, inhertiance instrument assignment exa…
Browse files Browse the repository at this point in the history
…mples and extra polymorphism code
  • Loading branch information
ciskavriezenga committed Dec 17, 2024
1 parent f927226 commit 7cd4578
Show file tree
Hide file tree
Showing 24 changed files with 494 additions and 0 deletions.
Binary file added blok2b/Beoordeling - Blueprint.pdf
Binary file not shown.
31 changes: 31 additions & 0 deletions blok2b/extra/midas/main.cpp
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();
}
29 changes: 29 additions & 0 deletions blok2b/extra/midas/main2.cpp
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.
13 changes: 13 additions & 0 deletions blok2b/session6/02_polymorphism/Makefile
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
45 changes: 45 additions & 0 deletions blok2b/session6/02_polymorphism/main.cpp
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();
}
12 changes: 12 additions & 0 deletions blok2b/session6/02_polymorphism/oscillator.h
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";}
};
14 changes: 14 additions & 0 deletions blok2b/session6/02_polymorphism/saw.h
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";}
};
14 changes: 14 additions & 0 deletions blok2b/session6/02_polymorphism/sine.h
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 blok2b/session6/03_polymorphism_dynamicAllocation/Makefile
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 blok2b/session6/03_polymorphism_dynamicAllocation/main.cpp
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 blok2b/session6/03_polymorphism_dynamicAllocation/oscillator.h
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";}
};
14 changes: 14 additions & 0 deletions blok2b/session6/03_polymorphism_dynamicAllocation/saw.h
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";}
};
14 changes: 14 additions & 0 deletions blok2b/session6/03_polymorphism_dynamicAllocation/sine.h
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";}
};
13 changes: 13 additions & 0 deletions blok2b/session6/prep/02_polymorphism/Makefile
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
45 changes: 45 additions & 0 deletions blok2b/session6/prep/02_polymorphism/main.cpp
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();
}
12 changes: 12 additions & 0 deletions blok2b/session6/prep/02_polymorphism/oscillator.h
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";}
};
14 changes: 14 additions & 0 deletions blok2b/session6/prep/02_polymorphism/saw.h
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";}
};
14 changes: 14 additions & 0 deletions blok2b/session6/prep/02_polymorphism/sine.h
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";}
};
Loading

0 comments on commit 7cd4578

Please sign in to comment.