Fahrzeuge 3: raw pointer
main.cpp
Go to the documentation of this file.
1 // C++ Vorlesung 8. Juni 2018
2 // Klassenhierarchie 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
8 // fahrzeug.cpp, fahrzeug.h remain unchanged
9 
10 /*
11  g++ -pedantic -std=c++11 -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++11 -fsyntax-only -Wdocumentation -Wconversion -Wshadow -Wfloat-conversion -pedantic *.cpp
14  clang++ -std=c++11 -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 <numeric> // accumulate
23 #include <typeinfo>
24 #include <vector>
25 using namespace std;
26 
32 // |--- parameter passing by reference allows polymorphism
33 ostream& operator<<(ostream &s, const Fahrzeug& p);
34 
35 // |--- parameter passing by pointer allows polymorphism
36 //ostream& operator<<(ostream &s, const Fahrzeug* p);
37 
38 // |--- n o polymorphism if parameter is passed by v a l u e
39 //ostream& operator<<(ostream &s, const Fahrzeug p);
40 
46 ostream& operator<<(ostream &s, const vector<Fahrzeug*>& v);
47 
55 bool fuel_consumption(const Fahrzeug* a, const Fahrzeug* b);
56 
57 bool fuel_consumption(const Fahrzeug* a, const Fahrzeug* b)
58 {
59  return a->verbrauch() < b->verbrauch();
60 }
61 
69 float add_fuel(float x, const Fahrzeug* y);
70 
71 float add_fuel(float x, const Fahrzeug* y)
72 {
73  return x + y->verbrauch();
74 }
75 
76 int main()
77 {
78  cout << "Hello world!" << endl;
79 
80  //Fahrzeug a(1768); // abstract class now
81  //LKW b(2500, 3100); // abstract class now
82  //PKW c(1459, 3); // abstract class now
83  MAN l1(7500, 10100);
84  Raba l2 (3500, 4000);
85  Opel p1(1450);
86  Smart p2 (1100);
87 
88  //
89  //cout << a << endl; // basis class ==> correct information is printed, virtual methods are used
90  cout << p1 << endl; // derived class ==> correct information is printed, because of virtual methods!!
91  cout << l2 << endl; // derived class ==> correct information is printed, because of virtual methods!!
92 
93 // ####################################################################################################
94  cout << " -------- New in v_10c --------------\n";
95  // new in v_10c
96  // container of base class pointers
97  vector<Fahrzeug*> v;
98  // |-- we need a pointer to an instance
99  v.push_back( new Raba(3600, 4000) );
100  v.push_back( new Opel(1450) );
101  v.push_back( new MAN(1200, 12000) );
102  v.push_back( new Smart(950) );
103  v.push_back( new Smart(1100));
104 
105  cout << v << endl;
106 
107  cout << " -------- after sort (standard --> incorrect) -------------\n";
108  sort(v.begin(),v.end());
109  cout << v << endl;
110 
111  cout << " -------- only the pointers have been compared -------------\n";
112 
113  cout << " -------- after sort (compare regarding fuel consumption) -------------\n";
114  sort(v.begin(),v.end(), fuel_consumption );
115  cout << v << endl;
116 
117  cout << " -------- correct-------------\n";
118 
119  // Berechne Flottenverbrauch (gleich gewichtet)
120  // konventionell
121  float sum=0.0;
122  for (unsigned int i=0; i<v.size(); ++i)
123  {
124  sum += v[i]->verbrauch();
125  }
126  cout << "konv: durchschnittlicher Verbrauch: " << sum/v.size() << endl;
127 
128  // via STL
129  float sum2 =accumulate(v.begin(), v.end(), 0.0f, add_fuel);
130  cout << "accu: durchschnittlicher Verbrauch: " << sum2/v.size() << endl;
131 
132 
133  cout << " -------- sort using lambda function for 'operator>' -------------\n";
134 // Sorting by using lambda-functions (ascending order wrt. fuel)
135 // http://stackoverflow.com/questions/5122804/sorting-with-lambda
136  sort(v.begin(),v.end(),
137  [](const Fahrzeug* const aa, const Fahrzeug* const cc) -> bool
138  {
139  return aa->verbrauch() > cc->verbrauch();
140  }
141  );
142 
143  cout << v << endl;
144 
145 // Free those element that have been allocated by new
146  for (auto it: v)
147  {
148  delete it;
149  }
150 
151  return 0;
152 } // free the memory storing the pointers in vector 'v' at the end of the scope
153 
154 
155 // We define an output function for references to the basis class
156 // |--- parameter passing by reference allows polymorphism
157 ostream& operator<<(ostream &s, const Fahrzeug& p)
158 {
159 // Virtual Method Table
160 // |-- VMT at run time ==> method from derived class
161 // | |--- always non-virtual method from base class
162  s << p.classname() << " : " << p.Get_kg() << " kg and "
163 // |-- VMT at run time ==> method from derived class
164  << p.verbrauch() << " l/100km" << endl;
165  return s;
166 }
167 
168 
169 ostream& operator<<(ostream &s, const vector<Fahrzeug*>& v)
170 {
171  for (auto it: v)
172  {
173  cout << *it;
174  }
175  return s;
176 }
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:157
bool fuel_consumption(const Fahrzeug *a, const Fahrzeug *b)
Compares the fuel consumption between two vehicles.
Definition: main.cpp:57
float add_fuel(float x, const Fahrzeug *y)
Adds the fuel consumption of a vehicle y to quantity x.
Definition: main.cpp:71
int main()
Definition: main.cpp:76