Fahrzeuge 3: shared pointer
Loading...
Searching...
No Matches
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 shared_ptr
4// STL: sorting and accumulate as examples
5// !! now we have a
6
7// 10c --> 10c_shared: use shared_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++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++11 -Wall -Wextra -pedantic *.cpp
17*/
18
19#include "fahrzeug.h"
20#include <algorithm>
21#include <iostream>
22#include <memory> // shared_ptr
23#include <numeric> // accumulate
24#include <typeinfo>
25#include <vector>
26using namespace std;
27
33ostream& operator<<(ostream &s, const Fahrzeug& p);
34
40ostream& operator<<(ostream &s, const vector<shared_ptr<Fahrzeug>>& v);
41
49bool fuel_consumption(const shared_ptr<Fahrzeug>& a, const shared_ptr<Fahrzeug>& b);
50
51bool fuel_consumption(const shared_ptr<Fahrzeug>& a, const shared_ptr<Fahrzeug>& b)
52{
53 return a->verbrauch() < b->verbrauch();
54}
55
63float add_fuel(float x, const shared_ptr<Fahrzeug>& y);
64
65float add_fuel(float x, const shared_ptr<Fahrzeug>& y)
66{
67 return x + y->verbrauch();
68}
69
70int main()
71{
72// ####################################################################################################
73 cout << " -------- New in v_10c_shared --------------\n";
74 // new in v_10c
75// // container of base class pointers
76// vector<shared_ptr<Fahrzeug>> v;
77// // |-- we need a pointer to an instance
78// v.push_back( shared_ptr<Fahrzeug> (new Raba(3600, 4000)) );
79// v.push_back( shared_ptr<Fahrzeug> (new Opel(1450)) );
80// v.push_back( shared_ptr<Fahrzeug> (new MAN(1200, 12000)) );
81// v.push_back( shared_ptr<Fahrzeug> (new Smart(950)) );
82// v.push_back( shared_ptr<Fahrzeug> (new Smart(1100)) );
83
84 // container of base class pointers
85 // possible with shared_ptr but not with unique_ptr
86 vector<shared_ptr<Fahrzeug>> v{
87 make_shared<Raba>(3600, 4000), make_shared<Opel>(1450), make_shared<MAN>(1200, 12000),
88 make_shared<Smart>(950), make_shared<Smart>(1100)
89 };
90
91
92 cout << v << endl;
93
94 cout << " -------- after sort (standard --> incorrect) -------------\n";
95 sort(v.begin(),v.end());
96 cout << v << endl;
97
98 cout << " -------- only the pointers have been compared -------------\n";
99
100 cout << " -------- after sort (compare regarding fuel consumption) -------------\n";
101 sort(v.begin(),v.end(), fuel_consumption );
102 cout << v << endl;
103
104 cout << " -------- correct-------------\n";
105
106 // Berechne Flottenverbrauch (gleich gewichtet)
107 // konventionell
108 float sum=0.0;
109 for (unsigned int i=0; i<v.size(); ++i)
110 {
111 sum += v[i]->verbrauch(); // *(v[i]).verbrauch();
112 }
113 cout << "konv: durchschnittlicher Verbrauch: " << sum/v.size() << endl;
114
115 // via STL
116 float sum2 =accumulate(v.begin(), v.end(), 0.0f, add_fuel);
117 cout << "accu: durchschnittlicher Verbrauch: " << sum2/v.size() << endl;
118
119
120 cout << " -------- sort using lambda function for 'operator>' -------------\n";
121// Sorting by using lambda-functions (ascending order wrt. fuel)
122// http://stackoverflow.com/questions/5122804/sorting-with-lambda
123 sort(v.begin(),v.end(),
124 [](const shared_ptr<Fahrzeug>& aa, const shared_ptr<Fahrzeug>& cc) -> bool
125 {
126 return aa->verbrauch() > cc->verbrauch();
127 }
128 );
129
130 cout << v << endl;
132 return 0;
133} // free the memory storing the pointers in vector 'v' at the end of the scope
134
135
136// We define an output function for references to the basis class
137// |--- parameter passing by reference allows polymorphism
138ostream& operator<<(ostream &s, const Fahrzeug& p)
139{
140// Virtual Method Table
141// |-- VMT at run time ==> method from derived class
142// | |--- always non-virtual method from base class
143 s << p.classname() << " : " << p.Get_kg() << " kg and "
144// |-- VMT at run time ==> method from derived class
145 << p.verbrauch() << " l/100km" << endl;
146 return s;
147}
148
149
150ostream& operator<<(ostream &s, const vector<shared_ptr<Fahrzeug>>& v)
151{
152 for (const auto& it: v) // Reference is required with shared_ptr. No copy constructor for shared_ptr available!
153 {
154 cout << *it;
155 };
156 return s;
157}
virtual float verbrauch() const =0
virtual std::string classname() const
Class name.
Definition fahrzeug.h:27
int Get_kg() const
Definition fahrzeug.h:19
bool fuel_consumption(const shared_ptr< Fahrzeug > &a, const shared_ptr< Fahrzeug > &b)
Compares the fuel consumption between two vehicles.
Definition main.cpp:51
ostream & operator<<(ostream &s, const Fahrzeug &p)
Prints some info from a vehicle.
Definition main.cpp:138
int main()
Definition main.cpp:70
float add_fuel(float x, const shared_ptr< Fahrzeug > &y)
Adds the fuel consumption of a vehicle y to quantity x.
Definition main.cpp:65