simple base and derived class example [member function override pure virtual function]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 14 Drill 7
Using std_lib_facilities.h by Bjarne Stroustrup.

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 14 Drill 7

#include "std_lib_facilities.h"
using namespace std;

class B2
{
public:
	virtual void pvf() = 0;
};

class D21 : public B2
{
public:
	void pvf() { cout << data << endl; }
	string data;
};

class D22 : public B2
{
public:
	void pvf() { cout << val << endl; }
	int val;
};

void f(B2& b2_ref) {
	b2_ref.pvf();
}

int main()
try
{
	D21 d21;
	d21.data = "hello";
	D22 d22;
	d22.val = 5;

	f(d21);
	f(d22);

	keep_window_open();
}
catch (exception& e) {
	cout << e.what() << endl;
	return 1;
}
catch (...) {
	cout << "Exiting" << endl;
	return 2;
}
Output:
hello
5
Please enter a character to exit

simple base and derived class example [override pure virtual function]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 14 Drill 6
Using std_lib_facilities.h by Bjarne Stroustrup.

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 14 Drill 6

#include "std_lib_facilities.h"
using namespace std;

class B1
{
public:
	virtual void vf() { cout << "B1::vf" << endl; }
	void f() { cout << "B1::f" << endl; }
	virtual void pvf() = 0;
};

class D1 : public B1
{
public:
	void vf() { cout << "D1::vf" << endl; }
	void f() { cout << "D1::f" << endl; }
	void pvf() { cout << "D1::pvf" << endl; }
};

class D2 : public D1
{
public:
	void pvf() { cout << "D2::pvf" << endl; }
};

int main()
try
{
	D2 d2_0;
	d2_0.f();
	d2_0.vf();
	d2_0.pvf();

	keep_window_open();
}
catch (exception& e) {
	cout << e.what() << endl;
	return 1;
}
catch (...) {
	cout << "Exiting" << endl;
	return 2;
}
Output:
D1::f
D1::vf
D2::pvf
Please enter a character to exit

simple base and derived class example [adding a pure virtual function]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 14 Drill 5
Using std_lib_facilities.h by Bjarne Stroustrup.

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 14 Drill 5

#include "std_lib_facilities.h"
using namespace std;

class B1
{
public:
	virtual void vf() { cout << "B1::vf" << endl; }
	void f() { cout << "B1::f" << endl; }
	virtual void pvf() = 0;
};

class D1 : public B1
{
public:
	void vf() { cout << "D1::vf" << endl; }
	void f() { cout << "D1::f" << endl; }
	void pvf() { cout << "D1::pvf" << endl; }
};

void call(B1& b1_ref) {
	b1_ref.f();
	b1_ref.vf();
	b1_ref.pvf();
}

int main()
try
{
	cout << "drill 1" << endl;
	//B1 b1;
	//b1.f();
	//b1.vf();
	cout << "not allowed, B1::pvf is a pure virtual function" << endl;

	cout << "drill 2" << endl;
	D1 d2;
	d2.f();
	d2.vf();
	d2.pvf();

	cout << "drill 3" << endl;
	D1 d3;
	call(d3);

	keep_window_open();
}
catch (exception& e) {
	cout << e.what() << endl;
	return 1;
}
catch (...) {
	cout << "Exiting" << endl;
	return 2;
}
Output:
drill 1
not allowed, B1::pvf is a pure virtual function
drill 2
D1::f
D1::vf
D1::pvf
drill 3
B1::f
D1::vf
D1::pvf
Please enter a character to exit

simple base and derived class example [function call]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 14 Drill 4
Using std_lib_facilities.h by Bjarne Stroustrup.

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 14 Drill 4

#include "std_lib_facilities.h"
using namespace std;

class B1
{
public:
	virtual void vf() { cout << "B1::vf" << endl; }
	void f() { cout << "B1::f" << endl; }
};

class D1 : public B1
{
public:
	void vf() { cout << "D1::vf" << endl; }
	void f() { cout << "D1::f" << endl; }
};

void call(B1& b1_ref) {	
	b1_ref.f();
	b1_ref.vf();
}

int main()
try
{
	cout << "drill 1" << endl;
	B1 b1;
	b1.f();
	b1.vf();

	cout << "drill 2" << endl;
	D1 d2;
	d2.f();
	d2.vf();

	cout << "drill 3" << endl;
	D1 d3;
	call(d3);

	keep_window_open();
}
catch (exception& e) {
	cout << e.what() << endl;
	return 1;
}
catch (...) {
	cout << "Exiting" << endl;
	return 2;
}
Output:
drill 1
B1::f
B1::vf
drill 2
D1::f
D1::vf
drill 3
B1::f
D1::vf
Please enter a character to exit

simple base and derived class example [using a reference]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 14 Drill 3
Using std_lib_facilities.h by Bjarne Stroustrup.

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 14 Drill 3

#include "std_lib_facilities.h"
using namespace std;

class B1
{
public:
	virtual void vf() { cout << "B1::vf" << endl; }
	void f() { cout << "B1::f" << endl; }
};

class D1 : public B1
{
public:
	void vf() { cout << "D1::vf" << endl; }
};

void call(B1& b1_ref) {
	b1_ref.vf();
	b1_ref.f();	
}

int main()
try
{
	D1 d1;
	call(d1);
	
	keep_window_open();
}
catch (exception& e) {
	cout << e.what() << endl;
	return 1;
}
catch (...) {
	cout << "Exiting" << endl;
	return 2;
}
Output:
D1::vf
B1::f
Please enter a character to exit

simple base and derived class example

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 14 Drill 2
Using std_lib_facilities.h by Bjarne Stroustrup.

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 14 Drill 2

#include "std_lib_facilities.h"
using namespace std;

class B1
{
public:
	virtual void vf() { cout << "B1::vf" << endl; }
	void f() { cout << "B1::f" << endl; }
};

class D1 : public B1
{
public:
	void vf() { cout << "D1::vf" << endl; }
};

int main()
try
{
	D1 d1;
	d1.f();
	d1.vf();

	keep_window_open();
}
catch (exception& e) {
	cout << e.what() << endl;
	return 1;
}
catch (...) {
	cout << "Exiting" << endl;
	return 2;
}
Output:
B1::f
D1::vf
Please enter a character to exit

simple virtual function example

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 14 Drill 1
Using std_lib_facilities.h by Bjarne Stroustrup.

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 14 Drill 1

#include "std_lib_facilities.h"
using namespace std;

class B1
{
public:
	virtual void vf() { cout << "B1::vf" << endl; }
	void f() { cout << "B1::f" << endl; }
};

int main()
try
{
	B1 b1;
	b1.f();
	b1.vf();

	keep_window_open();
}
catch (exception& e) {
	cout << e.what() << endl;
	return 1;
}
catch (...) {
	cout << "Exiting" << endl;
	return 2;
}
Output:
B1::f
B1::vf
Please enter a character to exit

Bjarne Stroustrup – Programming Principles and Practice using C++ [WIP]

Get the book Programming Principles and Practice using C++ by the inventor of C++ Bjarne Stroustrup here and join me on my journey.

MENU
Chapter 02 – Hello, World!
Chapter 03 – Objects, Types and Values
Chapter 04 – Computation
Chapter 05 – Errors
Chapter 06 – Writing a Program
Chapter 07 – Completing a Program
Chapter 08 – Technicalities: Functions, etc.
Chapter 09 – Technicalities: Classes, etc.
Chapter 10 – Input and Output Streams
Chapter 11 – Customizing Input and Output
Chapter 12 – A Display Model [using FLTK]
Chapter 13 – Graphics Classes [using FLTK]
Chapter 14 – Graphics Class Design [using FLTK]
Chapter 15 – Graphing Functions and Data [using FLTK]
Chapter 16 – Graphical User Interface [using FLTK]

How to compile FLTK – [following the instruction of fltk.org] This approach apparently prevents cross-contamination. Instead of copying files and folders from fltk-1.3.4 to the Visual Studio Community 2017 folder structure, the include and library paths are propperly (I hope so!) added to the project.
How to compile FLTK – [following the instruction of Principles and Practice using C++]

Linear Programming using GLPK (GNU Linear Programming Kit)

null

Simple Examples – Linear Programming

I started to develop an interest in Linear Programming problem solving. But what is Linear Programming and what sort of problems can be solved? Quick wikipedia search: “Linear programming (LP, also called linear optimization) is a method to achieve the best outcome (such as maximum profit or lowest cost) in a mathematical model whose requirements are represented by linear relationships. Linear programming is a special case of mathematical programming (mathematical optimization).”

Well I discovered a light weight library for C++ called GNU Linear Programming Kit, short GLPK, including the following main components:

– primal and dual simplex methods
– primal-dual interior-point method
– branch-and-cut method
– translator for GNU MathProg
– application program interface (API)
– stand-alone LP/MIP solver

And a set of real life example problems on slideshare, right here, which I will solve using GLPK:

1. Minimize cost for small and large vans
2. Maximize servings of pancakes and waffles recipes
3. Maximize earnings
4. Optimize protein consumption

Have a look and let me know what you think!

simple linear programming example 4 – protein consumption [GLPK]

This example was found on linkedIn SlideShare Page 9:

As part of your weight training regimen, you want to
consume lean sources of protein. You want to consume
at least 300 Calories a day from at least 48
grams of protein
. One ounce of chicken provides 35 Calories
and 8.5 g of protein
. One ounce of tofu provides 20 Calories
and 2.5 g protein
. Your local supermarket charges $5 a pound
for chicken and $2.5 a pound for tofu. How much of each food
should you eat each day if you want to meet your requierments
with the lowest cost?
What is this daily cost?

c = number of lbs of chicken
t = number of lbs of tofu

optimisation equation: P(Price Paid)
P = 5 * c + 2.5 * t

subject to: (16 ounces = 1 lb)
c >= 0
t >= 0
560 * c + 320 * t >= 300 //1 lb chicken = 16 * 35 = 560 calories
//1 lb tofu = 16 * 20 = 320 calories
136 * c + 40 * t >= 48 //1 lb chicken = 16 * 8.5 = 136 g of protein
//1 lb chicken = 16 * 2.5 = 40 g of protein

Also written as:
p = 560 * c + 320 * t
q = 136 * c + 40 * t

300 <= p <= +inf
0 <= x1 < +inf

48 <= q <= +inf
0 <= x2 < +inf

//  Philipp Siedler
//  Linear Programming
//  Real Life Example 4

#include <stdio.h>
#include <stdlib.h>
#include <glpk.h>
#include <std_lib_facilities.h>

int main(void)
{
	glp_prob *lp;
	int row_index[1 + 1000];					//Row indices of each element
	int	col_index[1 + 1000];					//column indices of each element
	double value[1 + 1000];						//numerical values of corresponding elements
	double z, x1, x2, x3;
	lp = glp_create_prob();						//creates a problem object
	glp_set_prob_name(lp, "sample");			//assigns a symbolic name to the problem object
	glp_set_obj_dir(lp, GLP_MIN);				//calls the routine glp_set_obj_dir to set the
												//omptimization direction flag,
												//where GLP_MAX means maximization

	//ROWS
	glp_add_rows(lp, 2);						//adds three rows to the problem object
	//row 1
	glp_set_row_name(lp, 1, "p");				//assigns name p to first row
	glp_set_row_bnds(lp, 1, GLP_LO, 300.0, INFINITY);	//sets the type and bounds of the first row,
												//where GLP_LO means that the row has an lower bound.
												//300 <= p <= +inf	
	//row 2
	glp_set_row_name(lp, 2, "q");				//assigns name q to second row
	glp_set_row_bnds(lp, 2, GLP_LO, 48.0, INFINITY);//48 <= q <= +inf	

	//COLUMNS
	glp_add_cols(lp, 2);						//adds three columns to the problem object
	//column 1
	glp_set_col_name(lp, 1, "x1");				//assigns name x1 to first column
	glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 0.0);	//sets the type and bounds to the first column,
												//where GLP_LO means that the column has an lower bound
	glp_set_obj_coef(lp, 1, 5.0);				//sets the objective coefficient for thr first column
												//P = 5 * c + 2.5 * t
	//column 2
	glp_set_col_name(lp, 2, "x2");				//assigns name x2 to first column
	glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 0.0);	//sets the type and bounds to the second column
	glp_set_obj_coef(lp, 2, 2.5);				//sets the objective coefficient for thr second column

	/*
	p = 560 * c + 320 * t
	q = 136 * c + 40 * t
	*/

	row_index[1] = 1, col_index[1] = 1, value[1] = 560.0;	// a[1,1] = 560.0
	row_index[2] = 1, col_index[2] = 2, value[2] = 320.0;	// a[1,2] = 320.0
	row_index[3] = 2, col_index[3] = 1, value[3] = 136.0;	// a[2,1] = 136.0
	row_index[4] = 2, col_index[4] = 2, value[4] = 40.0;	// a[2,2] = 40.0
	
	for (int i = 1; i < 5; i++) {
		cout << value[i];
		cout << ((i % 2 == 0) ? "\n" : "\t");
	}

	glp_load_matrix(lp, 4, row_index, col_index, value);	//calls the routine glp_load_matrix
															//loads information from three arrays
															//into the problem object
	glp_simplex(lp, NULL);						//calls the routine glp_simplex
												//to solve LP problem
	z = glp_get_obj_val(lp);					//obtains a computed value of the objective function
	x1 = glp_get_col_prim(lp, 1);				//obtain computed values of structural variables (columns)
	x2 = glp_get_col_prim(lp, 2);				//obtain computed values of structural variables (columns)

	printf("\nPrize(z) = %g; chicken(x1) = %g; tofu(x2) = %g;\n", z, x1, x2); //writes out the optimal solution
	glp_delete_prob(lp);						//calls the routine glp_delete_prob, which frees all the memory
	keep_window_open();							//leave this line out and run program with ctrl + F5 to keep window open
												//that means you won't need std_lib_facilities.h
}
Output:
560     320
136     40
GLPK Simplex Optimizer, v4.63
2 rows, 2 columns, 4 non-zeros
      0: obj =   0.000000000e+00 inf =   3.480e+02 (2)
      2: obj =   2.678571429e+00 inf =   0.000e+00 (0)
*     3: obj =   2.443181818e+00 inf =   0.000e+00 (0)
OPTIMAL LP SOLUTION FOUND

Prize(z) = 2.44318; chicken(x1) = 0.159091; tofu(x2) = 0.659091;
Please enter a character to exit