Commit cd17fe3d authored by Roger Kaeppeli's avatar Roger Kaeppeli

Add week 08

parent 13cda327
#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';
}
#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);
}
#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';
}
#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';
}
#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';
}
#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!
}
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)
#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';
}
#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';
}
#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';
}
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)
#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!
}
#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!
}
#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!
}
#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!
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment