Forms with mutable Area
main.cpp
Go to the documentation of this file.
1 // Simple class demo
2 // g++ -std=c++11 -Wall -W -Wfloat-equal -Wshadow -Wredundant-decls -Weffc++ -pedantic main.cpp form.cpp polygon.cpp
3 #include "form.h"
4 #include "polygon.h"
5 #include <algorithm> // sort
6 #include <iostream>
7 #include <vector> // vector
8 using namespace std;
9 
10 //---------- Hauptprogramm (nicht veraendern!!) ----------------------
11 int main()
12 {
13  const Kreis a( 0.0F, 1.0F, 2.1F);
14  Quadrat b(-1.0F, 0.5F, 2.0F);
15 
16  cout << endl << a << endl << b << endl; // function operator<<() uses polymorphism
17 
18  if (a<b)
19  {
20  cout << "Flaeche von " << a.classname() << " < als von " << b.classname() << endl;
21  }
22  else
23  {
24  cout << "Flaeche von " << b.classname() << " <= als von " << a.classname() << endl;
25  }
26  cout << endl;
27 
28  // dynamic memory allocation
29  vector<Form*> v{new Kreis(a), new Quadrat(b), new Kreis(2.0F, 1.0F, 1.1F), new Quadrat(-1.0F, 1.5F, 1.9F),
30  new Polygon(127)};
31 
32  // Sort ascending wrt. area
33  cout << " SORT\n";
34  sort(v.begin(),v.end(),kleiner);
35  for (const auto pi : v) { cout << *pi << endl; } cout << "-------------\n";
36 
37  // Sort descending wrt. area; lambda function
38  sort(v.begin(),v.end(),
39  [](const Form *aa, const Form *bb) -> bool
40  {
41  return aa->area() > bb->area();
42  }
43  );
44  for (auto *const pi : v) { cout << *pi << endl; } cout << "-------------\n";
45 
46  // distance matrix
47  vector<vector<float>> distmatrix(v.size()); // #rows
48  for (auto &di : distmatrix ) di.resize(v.size()); // #cols per row (Reference !!)
49 // for (vector<float> &di : distmatrix ) di.resize(v.size()); // #cols per row (Reference !!)
50 // for (size_t i=0; i<distmatrix.size(); ++i) distmatrix[i].resize(v.size()); // #cols per row
51 
52  // calculate distances between objects
53  for (size_t i=0; i<v.size(); ++i)
54  {
55  for (size_t j=0; j<v.size(); ++j)
56  {
57  distmatrix[i][j] = euclid_dist( v[i], v[j] );
58  cout << i << " " << j << " " << distmatrix[i][j] << endl;
59  }
60  }
61 
62  // Output distances
63  cout.precision(6);
64  cout.setf(ios::fixed, ios::floatfield); // arranges output in columns
65  for (const auto &vi: distmatrix) // Reference avoids copying, const ensures no changes
66  {
67  for (const auto vj: vi)
68  {
69  cout << vj << " ";
70  }
71  cout << endl;
72  }
73 
74 
75 
76  // deallocate dynamic memory
77  for (auto &pi : v) { delete pi;}
78 
79  return 0;
80 }
Definition: form.h:73
Definition: form.h:126
std::string classname() const override
Definition: form.h:132
Contains the description of a polygon, now with mutable. The traverse is stored.
Definition: polygon.h:28
Definition: form.h:104
std::string classname() const override
Definition: form.h:109
bool kleiner(const Form *a, const Form *b)
Definition: form.cpp:15
float euclid_dist(const Form &a, const Form &b)
Definition: form.h:152
int main()
Definition: main.cpp:11