Fahrzeuge 3: list
main.cpp
Go to the documentation of this file.
1 // C++ Vorlesung 2. Juni 2017
2 // Klassenhierarche mit virtuellen Methoden
3 // Nutzung des Polymorphismus
4 // STL: sorting and acuumulate as examples
5 // !! now we have a
6 
7 // v_10b --> v_10c_list
8 // fahrzeug.cpp, fahrzeug.h remain unchanged
9 
10 /*
11  g++ -pedantic -std=c++14 -Weffc++ -Wall -Wextra -pedantic -Wswitch-default -Wfloat-equal -Wundef -Wredundant-decls -Winit-self -Wshadow -Wparentheses -Wshadow -Wunreachable-code -Wuninitialized -Wmaybe-uninitialized *.cpp
12  cppcheck --enable=all --inconclusive --std=c++11 --std=posix --suppress=missingIncludeSystem *.cpp
13  clang++ -std=c++14 -fsyntax-only -Wdocumentation -Wconversion -Wshadow -Wfloat-conversion -pedantic *.cpp
14  clang++ -std=c++14 -Weverything -Wno-c++98-compat -Wno-padded *.cpp
15  clang++ -cc1 --help
16  icpc -std=c++14 -Wall -Wextra -pedantic *.cpp
17 */
18 
19 #include "fahrzeug.h"
20 #include <algorithm>
21 #include <iostream>
22 #include <list>
23 #include <numeric> // accumulate
24 #include <typeinfo>
25 using namespace std;
26 
30 // |--- parameter passing by reference allows polymorphism
31 void PrintInfo(const Fahrzeug& p);
32 
33 // |--- parameter passing by pointer allows polymorphism
34 //void PrintInfo(const Fahrzeug* p);
35 
36 // |--- n o polymorphism if parameter is passed by v a l u e
37 //void PrintInfo(const Fahrzeug p);
38 
44 ostream& operator<<(ostream &s, const Fahrzeug& p);
45 
53 bool fuel_consumption(const Fahrzeug* a, const Fahrzeug* b);
54 
55 bool fuel_consumption(const Fahrzeug* a, const Fahrzeug* b)
56 {
57  return a->verbrauch() < b->verbrauch();
58 }
59 
67 float add_fuel(float x, const Fahrzeug* y);
68 
69 float add_fuel(float x, const Fahrzeug* y)
70 {
71  return x + y->verbrauch();
72 }
73 
74 int main()
75 {
76  cout << "Hello world!" << endl;
77 
78  MAN l1(7500, 10100);
79  Raba l2 (3500, 4000);
80  Opel p1(1450);
81  Smart p2 (1100);
82 
83  //
84  PrintInfo(p1); // derived class ==> correct information is printed, because of virtual methods!!
85  PrintInfo(l2); // derived class ==> correct information is printed, because of virtual methods!!
86 
87 // ####################################################################################################
88  cout << " -------- New in v_10c --------------\n";
89  // new in v_10c
90  // container of base class pointers
91  list<Fahrzeug*> v;
92  // |-- we need a pointer to an instance
93  v.push_back( new Raba(3600, 4000) );
94  v.push_back( new Opel(1450) );
95  //
96  v.push_back( new MAN(1200, 12000) );
97  v.push_back( new Smart(950) );
98  v.push_back( new Smart(1100));
99 
100  for (auto it: v) { PrintInfo(*it); }
101 
102  cout << " -------- after sort (standard --> incorrect) -------------\n";
103 // sort(v.begin(),v.end()); // not with list
104  v.sort();
105  for (auto it: v) { PrintInfo(*it); }
106  cout << " -------- only the pointers have been compared -------------\n";
107 
108  cout << " -------- after sort (compare regarding fuel consumption) -------------\n";
109 // sort(v.begin(),v.end(), fuel_consumption ); // not with list
110  v.sort(fuel_consumption );
111  for (auto it: v) { PrintInfo(*it); }
112  cout << " now, using cout " << endl;
113  for (auto it: v) { cout << *it; }
114  cout << " -------- correct------------\n";
115 
116  // Berechne Flottenverbrauch (gleichgewichtet)
117  // konventionell
118  float sum=0.0;
119 // for (unsigned int i=0; i<v.size(); ++i) // not with list
120 // {
121 // sum += v[i]->verbrauch();
122 // }
123  for (auto pv: v)
124  {
125  sum += pv->verbrauch();
126  }
127  cout << "konv: durchschnittlicher Verbrauch: " << sum/v.size() << endl;
128 
129  // via STL
130  float sum2 =accumulate(v.begin(), v.end(), 0.0f, add_fuel);
131  cout << "accu: durchschnittlicher Verbrauch: " << sum2/v.size() << endl;
132 
133 
134  cout << " -------- sort using lambda function -------------\n";
135 // Sorting by using lambda-functions (ascending order wrt. fuel)
136 // http://stackoverflow.com/questions/5122804/sorting-with-lambda
137 // sort(v.begin(),v.end(), // not with list
138  v.sort(
139  [](const Fahrzeug* const aa, const Fahrzeug* const cc) -> bool
140  {
141  return aa->verbrauch() > cc->verbrauch();
142  }
143  );
144  for (auto it: v) { cout << *it; }
145 
146 // Free those element that have been allocated by new
147  for (auto it: v) { delete it; }
148 
149 
150  return 0;
151 }
152 
153 // |--- parameter passing by reference allows polymorphism
154 void PrintInfo(const Fahrzeug& p)
155 {
156 // Virtual Method Table
157 // |-- VMT at run time ==> method from derived class
158 // | |--- always non-virtual method from base class
159  cout << p.classname() << " : " << p.Get_kg() << " kg and "
160 // |-- VMT at run time ==> method from derived class
161  << p.verbrauch() << " l/100km" << endl;
162 }
163 
164 // we can also easily also define an output function for references to the basis class
165 // similar an output function for basis class pointers could be written
166 ostream& operator<<(ostream &s, const Fahrzeug& p)
167 {
168  s << p.classname() << " : " << p.Get_kg() << " kg and "
169  << p.verbrauch() << " l/100km" << endl;
170  return s;
171 }
virtual float verbrauch() const =0
virtual std::string classname() const
Class name.
Definition: fahrzeug.h:27
int Get_kg() const
Definition: fahrzeug.h:19
Definition: fahrzeug.h:105
Definition: fahrzeug.h:156
Definition: fahrzeug.h:130
ostream & operator<<(ostream &s, const Fahrzeug &p)
Prints some info from a vehicle.
Definition: main.cpp:166
void PrintInfo(const Fahrzeug &p)
Prints some info from a vehicle.
Definition: main.cpp:154
bool fuel_consumption(const Fahrzeug *a, const Fahrzeug *b)
Compares the fuel consumption between two vehicles.
Definition: main.cpp:55
float add_fuel(float x, const Fahrzeug *y)
Adds the fuel consumption of a vehicle y to quantity x.
Definition: main.cpp:69
int main()
Definition: main.cpp:74