function class object with reset

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 15 Exercise 2

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 15 Exercise 2

#define _USE_MATH_DEFINES
#include "Simple_window.h"
#include "Graph.h"
#include <cmath>

typedef double Fct(double);

double one(double x) { return 1; }
double slope(double x) { return x / 2; }
double square(double x) { return x * x; }
double sloping_cos(double x) { return cos(x) + slope(x); }
double sum_sin_cos(double x) { return sin(x) + cos(x); }
double sum_sin_cos_sq(double x) { return sin(x) * sin(x) + cos(x) * cos(x); }

struct myFct : Shape {
	myFct(Fct* f, double r1, double r2, Point orig, int count = 100, double xscale = 25, double yscale = 25);
	~myFct() {}

	void set_f(Fct* p) { f = p; }
	void set_r1(double r) { r1 = r; }
	void set_r2(double r) { r2 = r; }
	void set_orig(Point o) { orig = o; }
	void set_count(int c) { count = c; }
	void set_xscale(double x) { xscale = x; }
	void set_yscale(double y) { yscale = y; }

	double get_r1() { return r1; }
	double get_r2() { return r2; }
	Point get_orig() { return orig; }
	int get_count() { return count; }
	double get_xscale() { return xscale; }
	double get_yscale() { return yscale; }

	void calc();
	void reset(Fct* f, double r1, double r2, Point orig, int count = 100, double xscale = 25, double yscale = 25);
	
private:
	Fct* f;
	double r1;
	double r2;
	Point orig;
	int count;
	double xscale;
	double yscale;
};

myFct::myFct(Fct* f, double r1, double r2, Point orig, int count, double xscale, double yscale)
	:f(f), r1(r1), r2(r2), orig(orig), count(count), xscale(xscale), yscale(yscale)
{
	calc();
}

void myFct::calc()
{
	if (r2 - r1 <= 0) error("bad graphing range");
	if (count <= 0) error("non-positive graphing count");
	double dist = (r2 - r1) / count;
	double r = r1;
	for (int i = 0; i < count; ++i) {
		cout << f(r) << endl;
		add(Point(orig.x + int(r * xscale), orig.y - int(f(r) * yscale)));
		r += dist;
	}
}

void myFct::reset(Fct* f, double r1, double r2, Point orig, int count, double xscale, double yscale)
{
	set_f(f);
	set_r1(r1);
	set_r2(r2);
	set_orig(orig);
	set_count(count);
	set_xscale(xscale);
	set_yscale(yscale);
	calc();
}

int main()
try
{	
	int min = 0;
	int max = 5;
	Point origin(0, 0);
	int count = 5;

	myFct MF(one, min, max, origin, count);
	cout << "---" << endl;
	MF.reset(slope, min, max, origin, count);

	keep_window_open();
}
catch (exception& e) {
	cerr << "error: " << e.what() << '\n';
	keep_window_open();
	return 1;
}

catch (...) {
	cerr << "Unknown exception!\n";
	keep_window_open();
	return 2;
}
Output:
1
1
1
1
1
---
0
0.5
1
1.5
2
Please enter a character to exit

factorial – recursive and iterative methods

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 15 Exercise 1

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 15 Exercise 1

#include "std_lib_facilities.h"

int fac_recursive(int n) { return n > 1 ? n * fac_recursive(n - 1) : 1; } // factorial n!

int fac_iterative(int n)
{
	int r = 1;
	while (n > 1) {
		r *= n;
		--n;
	}
	return r;
}

int main()
try
{
	for (int i = 0; i < 21; i++) {
		cout << fac_recursive(i) << endl;
	}

	for (int i = 0; i < 21; i++) {
		cout << fac_iterative(i) << endl;
	}

	keep_window_open();
}
catch (exception& e) {
	cerr << "error: " << e.what() << '\n';
	keep_window_open();
	return 1;
}

catch (...) {
	cerr << "Unknown exception!\n";
	keep_window_open();
	return 2;
}
Output:
1
1
2
6
24
120
720
5040
40320
362880
3628800
39916800
479001600
1932053504
1278945280
2004310016
2004189184
-288522240
-898433024
109641728
-2102132736
1
1
2
6
24
120
720
5040
40320
362880
3628800
39916800
479001600
1932053504
1278945280
2004310016
2004189184
-288522240
-898433024
109641728
-2102132736
Please enter a character to exit

simple structure – adding complex objects by input – class definition recap 9/9

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 15 Class Definition Drill 9

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 15 Class Definition Drill 9

#include "std_lib_facilities.h"

struct Person
{
	Person(string first_name, string second_name, int age);
	Person() {}

	const string first_name() { return first_n; }
	const string second_name() { return second_n; }
	const int age() { return a; }

	void set_first_name(string first_name);
	void set_second_name(string second_name);
	void set_age(int age);

private:
	string first_n;
	string second_n;
	int a;
};

Person::Person(string first_name, string second_name, int age)
{
	for (char l : first_name) {
		if (!isalpha(l)) { error("first_name contains invalid character: ", first_name); }
	}
	for (char l : second_name) {
		if (!isalpha(l)) { error("second_name contains invalid character: ", second_name); }
	}
	if (age > 150 || age < 0) { error("age is not valid", age); }

	first_n = first_name;
	second_n = second_name;
	a = age;
}

void Person::set_first_name(string first_name)
{
	for (char l : first_name) {
		if (!isalpha(l)) { error("name contains invalid character: ", first_name); }
	}
	first_n = first_name;
}

void Person::set_second_name(string second_name)
{
	for (char l : second_name) {
		if (!isalpha(l)) { error("name contains invalid character: ", second_name); }
	}
	second_n = second_name;
}

void Person::set_age(int age)
{
	if (age > 150 || age < 0) { error("age is not valid", age); }
	a = age;
}

istream& operator>>(istream& is, Person& p) {
	string first_name;
	string second_name;
	int second_name_check;
	int age;

	is >> first_name;
	is >> second_name;
	for (char l : second_name) {
		if (!isalpha(l)) { error("there was no second name provided: ", second_name); }
	}
	is >> age;
	
	for (char l : first_name) {
		if (!isalpha(l)) { error("first_name contains invalid character: ", first_name); }
	}
	for (char l : second_name) {
		if (!isalpha(l)) { error("second_name contains invalid character: ", second_name); }
	}
	if (age > 150 || age < 0) { error("age is not valid", age); }

	p.set_first_name(first_name);
	p.set_second_name(second_name);
	p.set_age(age);

	return is;
}

ostream& operator<<(ostream& os, Person& p) {
	os << "Name: " << p.first_name() << ", " << p.second_name() << " Age: " << p.age();
	return os;
}

int main()
try
{
	Person p1;
	vector<Person> ppl;

	char check;

	while (true) {
		cin >> check;
		if (check == 'x') {
			break;
		}
		else {
			cin.putback(check);
			cin >> p1;
			ppl.push_back(p1);
		}
	}

	for (Person p : ppl) {
		cout << p << endl;
	}

	keep_window_open();
}
catch (exception& e) {
	cerr << "error: " << e.what() << '\n';
	keep_window_open();
	return 1;
}

catch (...) {
	cerr << "Unknown exception!\n";
	keep_window_open();
	return 2;
}
Input:
Hans Peter 23
Phil Schneider 34
x
Output:
Name: Hans, Peter Age: 23
Name: Phil, Schneider Age: 34
Please enter a character to exit

simple structure – adding objects by input – class definition recap 8/9

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 15 Class Definition Drill 8

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 15 Class Definition Drill 8

#include "std_lib_facilities.h"

struct Person
{
	Person(string name, int age);
	Person() {}

	const string name() { return n; }
	const int age() { return a; }

	void set_name(string name);
	void set_age(int age);

private:
	string n;
	int a;
};

Person::Person(string name, int age)
{
	for (char l : name) {
		if (!isalpha(l)) { error("name contains invalid character: ", name); }
	}
	if (age > 150 || age < 0) { error("age is not valid", age); }

	n = name;
	a = age;
}

void Person::set_name(string name)
{
	for (char l : name) {
		if (!isalpha(l)) { error("name contains invalid character: ", name); }
	}
	n = name;
}

void Person::set_age(int age)
{
	if (age > 150 || age < 0) { error("age is not valid", age); }
	a = age;
}

istream& operator>>(istream& is, Person& p) {
	string name;
	int age;

	is >> name >> age;

	p.set_name(name);
	p.set_age(age);

	return is;
}

ostream& operator<<(ostream& os, Person& p) {
	os << "Name: " << p.name() << " Age: " << p.age();
	return os;
}

int main()
try
{
	Person p1;
	vector<Person> ppl;

	char check;

	while (true) {
		cin >> check;
		if (check == 'x') {
			break;
		}
		else {
			cin.putback(check);
			cin >> p1;
			ppl.push_back(p1);
		}		
	}

	for (Person p : ppl) {
		cout << p << endl;
	}

	keep_window_open();
}
catch (exception& e) {
	cerr << "error: " << e.what() << '\n';
	keep_window_open();
	return 1;
}

catch (...) {
	cerr << "Unknown exception!\n";
	keep_window_open();
	return 2;
}
Input:
Phil 28
Jasmin 30
Benjamin 22
x
Output:
Name: Phil Age: 28
Name: Jasmin Age: 30
Name: Benjamin Age: 22
Please enter a character to exit

simple structure – adding set methods – class definition recap 7/9

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 15 Class Definition Drill 7

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 15 Class Definition Drill 7

#include "std_lib_facilities.h"

struct Person
{
	Person(string name, int age);
	Person() {}

	const string name() { return n; }
	const int age() { return a; }

	void set_name(string name);
	void set_age(int age);

private:
	string n;
	int a;
};

Person::Person(string name, int age)
{
	for (char l : name) {
		if (!isalpha(l)) { error("name contains invalid character: ", name); }
	}
	if (age > 150 || age < 0) { error("age is not valid", age); }

	n = name;
	a = age;
}

void Person::set_name(string name)
{
	for (char l : name) {
		if (!isalpha(l)) { error("name contains invalid character: ", name); }
	}
	n = name;
}

void Person::set_age(int age)
{
	if (age > 150 || age < 0) { error("age is not valid", age); }
	a = age;
}

istream& operator>>(istream& is, Person& p) {
	string name;
	int age;

	is >> name >> age;

	p.set_name(name);
	p.set_age(age);

	return is;
}

ostream& operator<<(ostream& os, Person& p) {
	os << "Name: " << p.name() << " Age: " << p.age();
	return os;
}

int main()
try
{
	Person p1;
	cin >> p1;
	cout << p1 << endl;

	keep_window_open();
}
catch (exception& e) {
	cerr << "error: " << e.what() << '\n';
	keep_window_open();
	return 1;
}

catch (...) {
	cerr << "Unknown exception!\n";
	keep_window_open();
	return 2;
}
Input:
Input:
Goofy 63
Output:
Name: Goofy Age: 63
Please enter a character to exit

simple structure – adding set methods – class definition recap 6/9

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 15 Class Definition Drill 6

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 15 Class Definition Drill 6

#include "std_lib_facilities.h"

struct Person
{
	Person(string name, int age)
		:n(name), a(age) {}
	Person() {}

	const string name() { return n; }
	const int age() { return a; }

	void set_name(string name) { n = name; }
	void set_age(int age) { a = age; }

private:
	string n;
	int a;
};

istream& operator>>(istream& is, Person& p) {
	string name;
	int age;

	is >> name >> age;

	p.set_name(name);
	p.set_age(age);

	return is;
}

ostream& operator<<(ostream& os, Person& p) {
	os << "Name: " << p.name() << " Age: " << p.age();
	return os;
}

int main()
try
{
	Person p1;
	cin >> p1;
	cout << p1 << endl;

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

simple structure – adding get methods – class definition recap 5/9

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 15 Class Definition Drill 5

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 15 Class Definition Drill 5

#include "std_lib_facilities.h"

struct Person
{
	Person(string name, int age)
		:n(name), a(age) {}
	Person() {}

	string name() { return n; }
	int age() { return a; }

private:
	string n;
	int a;
};

istream& operator>>(istream& is, Person& p) {
	is >> p.name >> p.age;
	return is;
}

ostream& operator<<(ostream& os, Person& p) {
	os << "Name: " << p.name << " Age: " << p.age;
	return os;
}

int main()
try
{
	Person p1;
	cin >> p1;
	cout << p1 << endl;

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

simple structure – constructor variation – class definition recap 4/9

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 15 Class Definition Drill 4

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 15 Class Definition Drill 4

#include "std_lib_facilities.h"

struct Person
{
	Person(string name, int age)
		:name(name), age(age) {}
	Person() {}

	string name;
	int age;
};

istream& operator>>(istream& is, Person& p) {
	is >> p.name >> p.age;
	return is;
}

ostream& operator<<(ostream& os, Person& p) {
	os << "Name: " << p.name << " Age: " << p.age;
	return os;
}

int main()
try
{
	Person p1;
	cin >> p1;
	cout << p1 << endl;

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

simple structure – i/o-stream overloading – class definition recap 3/9

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 15 Class Definition Drill 3

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 15 Class Definition Drill 3

#include "std_lib_facilities.h"

struct Person
{
	string name;
	int age;
};

istream& operator>>(istream& is, Person& p) {
	is >> p.name >> p.age;
	return is;
}

ostream& operator<<(ostream& os, Person& p) {
	os << "Name: " << p.name << " Age: " << p.age; return os; } int main() try { Person p1; cin >> p1;
	cout << p1 << endl;

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

simple structure – class definition recap 2/9

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 15 Class Definition Drill 2

//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 15 Class Definition Drill 2

#include "std_lib_facilities.h"


struct Person
{
	string name;
	int age;	
};

int main()
try
{
	Person p1;
	p1.name = "Goofy";
	p1.age = 63;

	cout << "Name: " << p1.name << " Age: " << p1.age << endl;

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