Aufgabe 18a
Schwierigkeitsgrad: Einfach
Themen: Objektorientierung Standardbibliothek
- Aufgabenstellung
- Lösung
Schreiben Sie eine Klasse Track, welche ein einzelnes Lied repräsentieren soll. Die Anforderungen an einen Track sind:
- Abspeichern von Titel, Namen aller Künstler, Dauer in ms, Tempo in bpm und Genre des Songs
- Auslesen aller abgespeicherten Werte, ohne diese zu verändern
- Ausgabe von Title und Namen aller Künstler über die Konsole
Implementieren Sie diese Anforderungen mithilfe von Membervariablen, Methoden und Operatorüberladungen.
track.hpp
#pragma once
#include <iostream>
#include <string>
#include <vector>
class Track {
private:
std::string title;
std::vector<std::string> artists;
int duration;
int tempo;
std::string genre;
public:
Track(std::string title, std::vector<std::string> artists, int duration, int tempo, std::string genre);
std::string get_title() const;
std::vector<std::string> get_artists() const;
int get_duration() const;
int get_tempo() const;
std::string get_genre() const;
friend std::ostream& operator<< (std::ostream& os, const Track& track);
};
bool operator== (const Track& track, const Track& other);
track.cpp
#include "track.hpp"
Track::Track(std::string title, std::vector<std::string> artists, int duration, int tempo, std::string genre): title(title), artists(artists), duration(duration), tempo(tempo), genre(genre) {
}
std::string Track::get_title() const {
return this->title;
}
std::vector<std::string> Track::get_artists() const {
return this->artists;
}
int Track::get_duration() const {
return this->duration;
}
int Track::get_tempo() const {
return this->tempo;
}
std::string Track::get_genre() const {
return this->genre;
}
std::ostream& operator<< (std::ostream& os, const Track& track) {
os << "Title: " << track.title << std::endl;
os << "Artists: ";
for (auto it = track.artists.begin(); it != track.artists.end(); ++it) {
os << *it << " ";
}
os << std::endl;
return os;
}
bool operator== (const Track& track, const Track& other) {
return track.get_title() == other.get_title();
}