Fahrzeuge 3: unique pointer
main.cpp
Go to the documentation of this file.
1 // C++ Vorlesung 8. Juni 2018
2 // Klassenhierarche mit virtuellen Methoden
3 // Nutzung des Polymorphismus via unique_ptr
4 // STL: sorting and accumulate as examples
5 // !! now we have a
6 
7 // 10c --> 10c_unique: use unique_ptr instead of C-pointer
8 // fahrzeug.cpp, fahrzeug.h are mostly unchanged. Changes regarding warning messages from compilers etc.
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 <memory> // unique_ptr
23 #include <numeric> // accumulate
24 #include <typeinfo>
25 #include <vector>
26 using namespace std;
27 
33 ostream& operator<<(ostream &s, const Fahrzeug& p);
34 
40 ostream& operator<<(ostream &s, const vector<unique_ptr<Fahrzeug>>& v);
41 
49 bool fuel_consumption(const unique_ptr<Fahrzeug>& a, const unique_ptr<Fahrzeug>& b);
50 
51 bool fuel_consumption(const unique_ptr<Fahrzeug>& a, const unique_ptr<Fahrzeug>& b)
52 {
53  return a->verbrauch() < b->verbrauch();
54 }
55 
63 float add_fuel(float x, const unique_ptr<Fahrzeug>& y);
64 
65 float add_fuel(float x, const unique_ptr<Fahrzeug>& y)
66 {
67  return x + y->verbrauch();
68 }
69 
70 int main()
71 {
72  cout << "Hello world!" << endl;
73 
74 // Fahrzeug a(1768); // now, an abstract class
75 // LKW b(2500, 3100);
76 // PKW c(1459, 3);
77  MAN l1(7500, 10100);
78  Raba l2 (3500, 4000);
79  Opel p1(1450);
80  Smart p2 (1100);
81 
82  //
83  cout << p1 << endl; // derived class ==> correct information is printed, because of virtual methods!!
84  cout << l2 << endl; // derived class ==> correct information is printed, because of virtual methods!!
85 
86 // ####################################################################################################
87  cout << " -------- New in v_10c_unique --------------\n";
88  // new in v_10c
89  // container of base class pointers
92  vector<unique_ptr<Fahrzeug>> v;
93  v.push_back( make_unique<Raba>(3600, 4000) );
94  v.push_back( make_unique<Opel>(1450) );
95  v.push_back( make_unique<MAN>(1200, 12000) );
96  v.push_back( make_unique<Smart>(950) );
97  v.push_back( make_unique<Smart>(1100) );
98 
99  // !! Not possible
100 // vector<unique_ptr<Fahrzeug>> vv{
101 // make_unique<Raba>(3600, 4000), make_unique<Opel>(1450), make_unique<MAN>(1200, 12000),
102 // make_unique<Smart>(950), make_unique<Smart>(1100)
103 // };
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 unique_ptr<Fahrzeug>& aa, const unique_ptr<Fahrzeug>& cc) -> bool
138  {
139  return aa->verbrauch() > cc->verbrauch();
140  }
141  );
142 
143  cout << v << endl;
145  return 0;
146 } // free the memory storing the pointers in vector 'v' at the end of the scope
147 
148 
149 // We define an output function for references to the basis class
150 // |--- parameter passing by reference allows polymorphism
151 ostream& operator<<(ostream &s, const Fahrzeug& p)
152 {
153 // Virtual Method Table
154 // |-- VMT at run time ==> method from derived class
155 // | |--- always non-virtual method from base class
156  s << p.classname() << " : " << p.Get_kg() << " kg and "
157 // |-- VMT at run time ==> method from derived class
158  << p.verbrauch() << " l/100km" << endl;
159  return s;
160 }
161 
162 
163 ostream& operator<<(ostream &s, const vector<unique_ptr<Fahrzeug>>& v)
164 {
165  for (const auto& it: v) // Reference is required with unique_ptr. No copy constructor for unique_ptr available!
166  {
167  cout << *it;
168  }
169  return s;
170 }
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:151
float add_fuel(float x, const unique_ptr< Fahrzeug > &y)
Adds the fuel consumption of a vehicle y to quantity x.
Definition: main.cpp:65
bool fuel_consumption(const unique_ptr< Fahrzeug > &a, const unique_ptr< Fahrzeug > &b)
Compares the fuel consumption between two vehicles.
Definition: main.cpp:51
int main()
Definition: main.cpp:70