C++ Beispiele
Dieses Verzeichnis enthält C++-Vergleichsbeispiele zu den C-Modulen.
Übersicht
| Datei | C-Modul | Konzept |
|---|---|---|
module1_memory_cpp.cpp |
Modul 1 | new/delete, Smart Pointers, RAII |
module5_datastructures_cpp.cpp |
Modul 5 | STL (vector, list, map, algorithms) |
module7_oop_cpp.cpp |
Modul 7 | Klassen, Vererbung, Polymorphie |
module8_stl_cpp.cpp |
Modul 8 | Iteratoren, Funktoren, Lambdas |
Kompilieren
# Alle Beispiele
g++ -std=c++17 -o memory module1_memory_cpp.cpp
g++ -std=c++17 -o datastruct module5_datastructures_cpp.cpp
g++ -std=c++17 -o oop module7_oop_cpp.cpp
g++ -std=c++17 -o stl module8_stl_cpp.cpp
# Ausführen
./memory
./datastruct
./oop
./stl
Makefile
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra
all: memory datastruct oop stl
memory: module1_memory_cpp.cpp
$(CXX) $(CXXFLAGS) -o $@ $<
datastruct: module5_datastructures_cpp.cpp
$(CXX) $(CXXFLAGS) -o $@ $<
oop: module7_oop_cpp.cpp
$(CXX) $(CXXFLAGS) -o $@ $<
stl: module8_stl_cpp.cpp
$(CXX) $(CXXFLAGS) -o $@ $<
clean:
rm -f memory datastruct oop stl
.PHONY: all clean
Hauptunterschiede C vs C++
| Aspekt | C | C++ |
|---|---|---|
| Speicher | malloc/free | new/delete, Smart Pointers |
| Datentypen | struct, primitive | class, string, vector, etc. |
| Generics | void* | templates |
| Fehlerbehandlung | Return codes | Exceptions (optional) |
| OOP | Manuell (Function Pointer) | Nativ (class, virtual) |
Empfohlene Lernreihenfolge:
- C-Modul durcharbeiten
- C++-Vergleich anschauen
- Unterschiede notieren
- C++-Version selbst modifizieren