Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
lecture
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pt1_hs20
lecture
Commits
cd17fe3d
Commit
cd17fe3d
authored
Nov 05, 2020
by
Roger Kaeppeli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add week 08
parent
13cda327
Changes
16
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
557 additions
and
0 deletions
+557
-0
demos/week08/Inheritance/inheritance01.cpp
demos/week08/Inheritance/inheritance01.cpp
+35
-0
demos/week08/Inheritance/inheritance02.cpp
demos/week08/Inheritance/inheritance02.cpp
+27
-0
demos/week08/Inheritance/inheritance03.cpp
demos/week08/Inheritance/inheritance03.cpp
+50
-0
demos/week08/Inheritance/inheritance04.cpp
demos/week08/Inheritance/inheritance04.cpp
+53
-0
demos/week08/Inheritance/inheritance05.cpp
demos/week08/Inheritance/inheritance05.cpp
+51
-0
demos/week08/Inheritance/virtual_dtor.cpp
demos/week08/Inheritance/virtual_dtor.cpp
+30
-0
demos/week08/Integrate/CMakeLists.txt
demos/week08/Integrate/CMakeLists.txt
+16
-0
demos/week08/Integrate/integrate_generic.cpp
demos/week08/Integrate/integrate_generic.cpp
+27
-0
demos/week08/Integrate/integrate_oo.cpp
demos/week08/Integrate/integrate_oo.cpp
+43
-0
demos/week08/Integrate/integrate_procedural.cpp
demos/week08/Integrate/integrate_procedural.cpp
+26
-0
demos/week08/Stack/CMakeLists.txt
demos/week08/Stack/CMakeLists.txt
+17
-0
demos/week08/Stack/stack_generic.cpp
demos/week08/Stack/stack_generic.cpp
+52
-0
demos/week08/Stack/stack_modular.cpp
demos/week08/Stack/stack_modular.cpp
+59
-0
demos/week08/Stack/stack_oo.cpp
demos/week08/Stack/stack_oo.cpp
+47
-0
demos/week08/Stack/stack_procedural.cpp
demos/week08/Stack/stack_procedural.cpp
+24
-0
slides/week08.pdf
slides/week08.pdf
+0
-0
No files found.
demos/week08/Inheritance/inheritance01.cpp
0 → 100644
View file @
cd17fe3d
#include <iostream>
class
Polygon
{
public:
void
set_values
(
double
a
,
double
b
)
{
width_
=
a
;
height_
=
b
;
}
protected:
double
width_
,
height_
;
};
class
Rectangle
:
public
Polygon
{
public:
double
area
()
{
return
width_
*
height_
;
}
};
class
Triangle
:
public
Polygon
{
public:
double
area
()
{
return
0.5
*
width_
*
height_
;
}
};
int
main
()
{
Rectangle
rec
;
Triangle
tri
;
rec
.
set_values
(
4.
,
5.
);
tri
.
set_values
(
4.
,
5.
);
std
::
cout
<<
"Rectangle area: "
<<
rec
.
area
()
<<
'\n'
;
std
::
cout
<<
"Triangle area: "
<<
tri
.
area
()
<<
'\n'
;
}
demos/week08/Inheritance/inheritance02.cpp
0 → 100644
View file @
cd17fe3d
#include <iostream>
class
Mother
{
public:
Mother
()
{
std
::
cout
<<
"Mother::Mother()
\n
"
;
}
// default ctor
Mother
(
int
a
)
{
std
::
cout
<<
"Mother::Mother(int)
\n
"
;
}
// ctor
~
Mother
()
{
std
::
cout
<<
"Mother::~Mother()
\n
"
;
}
// dtor
};
class
Daughter
:
public
Mother
{
public:
Daughter
(
int
a
)
{
std
::
cout
<<
"Daughter::Daughter(int)
\n
"
;
}
// ctor
~
Daughter
()
{
std
::
cout
<<
"Daughter::~Daughter()
\n
"
;
}
// dtor
};
class
Son
:
public
Mother
{
public:
Son
(
int
a
)
:
Mother
(
a
)
{
std
::
cout
<<
"Son::Son(int)
\n
"
;
}
// ctor
~
Son
()
{
std
::
cout
<<
"Son::~Son()
\n
"
;
}
// dtor
};
int
main
()
{
Mother
peggy
;
Daughter
kelly
(
0
);
Son
bud
(
0
);
}
demos/week08/Inheritance/inheritance03.cpp
0 → 100644
View file @
cd17fe3d
#include <iostream>
class
Polygon
{
public:
void
set_values
(
double
a
,
double
b
)
{
width_
=
a
;
height_
=
b
;
}
protected:
double
width_
,
height_
;
};
class
Rectangle
:
public
Polygon
{
public:
double
area
()
{
return
width_
*
height_
;
}
};
class
Triangle
:
public
Polygon
{
public:
double
area
()
{
return
0.5
*
width_
*
height_
;
}
};
void
set_values
(
Polygon
&
poly
,
double
a
,
double
b
)
{
poly
.
set_values
(
a
,
b
);
}
int
main
()
{
Polygon
*
poly
;
Rectangle
rec
;
Triangle
tri
;
poly
=
&
rec
;
poly
->
set_values
(
4.
,
5.
);
std
::
cout
<<
"Rectangle area: "
<<
rec
.
area
()
<<
'\n'
;
poly
=
&
tri
;
poly
->
set_values
(
4.
,
5.
);
std
::
cout
<<
"Triangle area: "
<<
tri
.
area
()
<<
'\n'
;
set_values
(
rec
,
4.
,
5.
);
std
::
cout
<<
"Rectangle area: "
<<
rec
.
area
()
<<
'\n'
;
set_values
(
tri
,
4.
,
5.
);
std
::
cout
<<
"Triangle area: "
<<
tri
.
area
()
<<
'\n'
;
}
demos/week08/Inheritance/inheritance04.cpp
0 → 100644
View file @
cd17fe3d
#include <iostream>
class
Polygon
{
public:
void
set_values
(
double
a
,
double
b
)
{
width_
=
a
;
height_
=
b
;
}
virtual
double
area
()
{
return
0.
;
}
protected:
double
width_
,
height_
;
};
class
Rectangle
:
public
Polygon
{
public:
double
area
()
{
return
width_
*
height_
;
}
};
class
Triangle
:
public
Polygon
{
public:
double
area
()
{
return
0.5
*
width_
*
height_
;
}
};
void
set_values
(
Polygon
&
poly
,
double
a
,
double
b
)
{
poly
.
set_values
(
a
,
b
);
}
int
main
()
{
Polygon
*
poly
;
Rectangle
rec
;
Triangle
tri
;
poly
=
&
rec
;
poly
->
set_values
(
4.
,
5.
);
std
::
cout
<<
"Rectangle area: "
<<
poly
->
area
()
<<
'\n'
;
poly
=
&
tri
;
poly
->
set_values
(
4.
,
5.
);
std
::
cout
<<
"Triangle area: "
<<
poly
->
area
()
<<
'\n'
;
set_values
(
rec
,
4.
,
5.
);
std
::
cout
<<
"Rectangle area: "
<<
poly
->
area
()
<<
'\n'
;
set_values
(
tri
,
4.
,
5.
);
std
::
cout
<<
"Triangle area: "
<<
poly
->
area
()
<<
'\n'
;
}
demos/week08/Inheritance/inheritance05.cpp
0 → 100644
View file @
cd17fe3d
#include <iostream>
class
Polygon
{
public:
void
set_values
(
double
a
,
double
b
)
{
width_
=
a
;
height_
=
b
;
}
virtual
double
area
()
=
0
;
// pure virtual function!
protected:
double
width_
,
height_
;
};
class
Rectangle
:
public
Polygon
{
public:
double
area
()
override
{
return
width_
*
height_
;
}
};
class
Triangle
:
public
Polygon
{
public:
double
area
()
override
{
return
0.5
*
width_
*
height_
;
}
};
void
set_values
(
Polygon
&
poly
,
double
a
,
double
b
)
{
poly
.
set_values
(
a
,
b
);
}
int
main
()
{
Polygon
*
poly
;
Rectangle
rec
;
Triangle
tri
;
poly
=
&
rec
;
poly
->
set_values
(
4.
,
5.
);
std
::
cout
<<
"Rectangle area: "
<<
poly
->
area
()
<<
'\n'
;
poly
=
&
tri
;
poly
->
set_values
(
4.
,
5.
);
std
::
cout
<<
"Triangle area: "
<<
poly
->
area
()
<<
'\n'
;
set_values
(
rec
,
4.
,
5.
);
std
::
cout
<<
"Rectangle area: "
<<
poly
->
area
()
<<
'\n'
;
set_values
(
tri
,
4.
,
5.
);
std
::
cout
<<
"Triangle area: "
<<
poly
->
area
()
<<
'\n'
;
}
demos/week08/Inheritance/virtual_dtor.cpp
0 → 100644
View file @
cd17fe3d
#include <iostream>
class
Base
{
public:
Base
()
{
std
::
cout
<<
"Base::Base()
\n
"
;
}
~
Base
()
{
std
::
cout
<<
"Base::~Base()
\n
"
;
}
// virtual ~Base() { std::cout << "Base::~Base()\n"; }
};
class
Derived
:
public
Base
{
public:
Derived
()
{
std
::
cout
<<
"Derived::Derived()
\n
"
;
}
~
Derived
()
{
std
::
cout
<<
"Derived::~Derived()
\n
"
;
}
};
int
main
()
{
Base
*
b
=
new
Derived
();
// set a Base class pointer to newly created
// Derived object
// ... use b polymorphically ...
delete
b
;
// Should destruct the Derived object through the Base class
// pointer.
// However, if the Base class destructor is not virtual, the
// Derived class destructor is not called. According to the
// C++ standard, this leads to undedfined behavior!
}
demos/week08/Integrate/CMakeLists.txt
0 → 100644
View file @
cd17fe3d
cmake_minimum_required
(
VERSION 2.8
)
# C++11
set
(
CMAKE_CXX_STANDARD 11
)
# setting warning compiler flags
if
(
CMAKE_CXX_COMPILER_ID MATCHES
"(C|c?)lang"
)
add_compile_options
(
-Weverything
)
else
()
add_compile_options
(
-Wall -Wextra -Wpedantic
)
endif
()
add_executable
(
integrate_procedural integrate_procedural.cpp
)
add_executable
(
integrate_generic integrate_generic.cpp
)
add_executable
(
integrate_oo integrate_oo.cpp
)
demos/week08/Integrate/integrate_generic.cpp
0 → 100644
View file @
cd17fe3d
#include <iostream>
#include <cmath>
// trapezoidal rule
template
<
typename
T
,
typename
F
>
T
integrate
(
F
f
,
T
a
,
T
b
,
unsigned
int
N
)
{
T
dx
=
(
b
-
a
)
/
N
;
T
xi
=
a
;
T
I
=
0.5
*
f
(
xi
);
for
(
unsigned
int
i
=
1
;
i
<
N
;
++
i
)
{
xi
+=
dx
;
I
+=
f
(
xi
);
}
I
+=
0.5
*
f
(
b
);
return
I
*
dx
;
}
struct
func
{
double
operator
()(
double
x
)
{
return
x
*
std
::
sin
(
x
);
}
};
int
main
()
{
std
::
cout
<<
"I[x*sin(x)] = "
<<
integrate
(
func
(),
0.
,
1.
,
100
)
<<
'\n'
;
}
demos/week08/Integrate/integrate_oo.cpp
0 → 100644
View file @
cd17fe3d
#include <cmath>
#include <iostream>
// base class provides the function interface
struct
SimpleFunction
{
SimpleFunction
()
{}
virtual
double
operator
()(
double
)
const
=
0
;
};
// trapezoidal rule
double
integrate
(
SimpleFunction
const
&
f
,
double
a
,
double
b
,
unsigned
int
N
)
{
double
dx
=
(
b
-
a
)
/
N
;
double
xi
=
a
;
double
I
=
0.5
*
f
(
xi
);
for
(
unsigned
int
i
=
1
;
i
<
N
;
++
i
)
{
xi
+=
dx
;
I
+=
f
(
xi
);
}
I
+=
0.5
*
f
(
b
);
return
I
*
dx
;
}
// derived class (implements a function!)
struct
MyFunc1
:
SimpleFunction
{
MyFunc1
()
{}
double
operator
()(
double
x
)
const
{
return
x
*
std
::
sin
(
x
);
}
};
// derived class (implements another function!)
struct
MyFunc2
:
SimpleFunction
{
MyFunc2
()
{}
double
operator
()(
double
x
)
const
{
return
x
*
x
*
std
::
sin
(
x
);
}
};
int
main
()
{
MyFunc1
f1
;
MyFunc2
f2
;
std
::
cout
<<
"I[x*sin(x)] = "
<<
integrate
(
f1
,
0.
,
1.
,
100
)
<<
'\n'
;
}
demos/week08/Integrate/integrate_procedural.cpp
0 → 100644
View file @
cd17fe3d
#include <cmath>
#include <iostream>
// trapezoidal rule
double
integrate
(
double
(
*
f
)(
double
),
double
a
,
double
b
,
unsigned
int
N
)
{
double
dx
=
(
b
-
a
)
/
N
;
double
xi
=
a
;
double
I
=
0.5
*
f
(
xi
);
for
(
unsigned
int
i
=
1
;
i
<
N
;
++
i
)
{
xi
+=
dx
;
I
+=
f
(
xi
);
}
I
+=
0.5
*
f
(
b
);
return
I
*
dx
;
}
double
func
(
double
x
)
{
return
x
*
std
::
sin
(
x
);
}
int
main
()
{
std
::
cout
<<
"I[x*sin(x)] = "
<<
integrate
(
func
,
0.
,
1.
,
100
)
<<
'\n'
;
}
demos/week08/Stack/CMakeLists.txt
0 → 100644
View file @
cd17fe3d
cmake_minimum_required
(
VERSION 2.8
)
# C++11
set
(
CMAKE_CXX_STANDARD 11
)
# setting warning compiler flags
if
(
CMAKE_CXX_COMPILER_ID MATCHES
"(C|c?)lang"
)
add_compile_options
(
-Weverything
)
else
()
add_compile_options
(
-Wall -Wextra -Wpedantic
)
endif
()
add_executable
(
stack_procedural stack_procedural.cpp
)
add_executable
(
stack_modular stack_modular.cpp
)
add_executable
(
stack_oo stack_oo.cpp
)
add_executable
(
stack_generic stack_generic.cpp
)
demos/week08/Stack/stack_generic.cpp
0 → 100644
View file @
cd17fe3d
#include <iostream>
#include <stdexcept>
namespace
Stack
{
template
<
typename
T
>
class
stack
{
public:
stack
(
int
l
)
:
s
(
new
T
[
l
]),
p
(
s
),
n
(
l
)
{}
// ctor
~
stack
()
{
delete
[]
s
;
}
// dtor
void
push
(
T
v
)
{
if
(
p
==
s
+
n
-
1
)
{
throw
std
::
runtime_error
(
"stack overflow"
);
}
*
p
++
=
v
;
}
T
pop
()
{
if
(
p
==
s
)
{
throw
std
::
runtime_error
(
"stack underflow"
);
}
return
*--
p
;
}
private:
T
*
s
;
// pointer to allocated stack memory
T
*
p
;
// stack pointer
int
n
;
// stack size
};
}
int
main
()
{
try
{
Stack
::
stack
<
double
>
s
(
100
);
s
.
push
(
10.
);
std
::
cout
<<
s
.
pop
()
<<
'\n'
;
Stack
::
stack
<
int
>
si
(
100
);
si
.
push
(
10
);
std
::
cout
<<
si
.
pop
()
<<
'\n'
;
}
catch
(
std
::
exception
&
e
)
{
std
::
cerr
<<
"Exception occurred: "
<<
e
.
what
()
<<
'\n'
;
return
1
;
// return error, i.e. non-zero value!
}
return
0
;
// return 0, i.e. 0 for zero problems!
}
demos/week08/Stack/stack_modular.cpp
0 → 100644
View file @
cd17fe3d
#include <iostream>
#include <stdexcept>
namespace
Stack
{
struct
stack
{
double
*
s
;
// pointer to allocated stack memory
double
*
p
;
// stack pointer
int
n
;
// stack size
};
void
create
(
stack
&
s
,
int
l
)
{
s
.
s
=
new
double
[
l
];
s
.
p
=
s
.
s
;
s
.
n
=
l
;
}
void
destroy
(
stack
&
s
)
{
s
.
n
=
0
;
s
.
p
=
NULL
;
// or 0 or nullptr (C++11)
delete
[]
s
.
s
;
s
.
s
=
NULL
;
// or 0 or nullptr (C++11)
}
void
push
(
stack
&
s
,
double
v
)
{
if
(
s
.
p
==
s
.
s
+
s
.
n
-
1
)
{
throw
std
::
runtime_error
(
"stack overflow"
);
}
*
s
.
p
++
=
v
;
}
double
pop
(
stack
&
s
)
{
if
(
s
.
p
==
s
.
s
)
{
throw
std
::
runtime_error
(
"stack underflow"
);
}
return
*--
s
.
p
;
}
}
int
main
()
{
try
{
Stack
::
stack
s
;
Stack
::
create
(
s
,
100
);
// create stack (must be called!)
Stack
::
push
(
s
,
10.
);
std
::
cout
<<
Stack
::
pop
(
s
)
<<
'\n'
;
std
::
cout
<<
Stack
::
pop
(
s
)
<<
'\n'
;
// throws error
Stack
::
destroy
(
s
);
// destroy stack (must be called!)
}
catch
(
std
::
exception
&
e
)
{
std
::
cerr
<<
"Exception occurred: "
<<
e
.
what
()
<<
'\n'
;
return
1
;
// return error, i.e. non-zero value!
}
return
0
;
// return 0, i.e. 0 for zero problems!
}
demos/week08/Stack/stack_oo.cpp
0 → 100644
View file @
cd17fe3d
#include <iostream>
#include <stdexcept>
namespace
Stack
{
class
stack
{
public:
stack
(
int
l
)
:
s
(
new
double
[
l
]),
p
(
s
),
n
(
l
)
{}
// ctor
~
stack
()
{
delete
[]
s
;
}
// dtor
void
push
(
double
v
)
{
if
(
p
==
s
+
n
-
1
)
{
throw
std
::
runtime_error
(
"stack overflow"
);
}
*
p
++
=
v
;
}
double
pop
()
{
if
(
p
==
s
)
{
throw
std
::
runtime_error
(
"stack underflow"
);
}
return
*--
p
;
}
private:
double
*
s
;
// pointer to allocated stack memory
double
*
p
;
// stack pointer
int
n
;
// stack size
};
}
int
main
()
{
try
{
Stack
::
stack
s
(
100
);
s
.
push
(
10.
);
std
::
cout
<<
s
.
pop
()
<<
'\n'
;
std
::
cout
<<
s
.
pop
()
<<
'\n'
;
// throws error
}
catch
(
std
::
exception
&
e
)
{
std
::
cerr
<<
"Exception occurred: "
<<
e
.
what
()
<<
'\n'
;
return
1
;
// return error, i.e. non-zero value!
}
return
0
;
// return 0, i.e. 0 for zero problems!
}
demos/week08/Stack/stack_procedural.cpp
0 → 100644
View file @
cd17fe3d
#include <iostream>
void
push
(
double
*&
s
,
double
v
)
{
*
s
++
=
v
;
}
double
pop
(
double
*&
s
)
{
return
*--
s
;
}
int
main
()
{
double
stack
[
1000
];
double
*
p
=
stack
;
push
(
p
,
10
);
std
::
cout
<<
pop
(
p
)
<<
'\n'
;
std
::
cout
<<
pop
(
p
)
<<
'\n'
;
// ERROR of popping empty
// stack goes undetected!
}
slides/week08.pdf
0 → 100644
View file @
cd17fe3d
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment