Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by hakan on 13.12.2019.
- //
- #include <iostream>
- #include <string>
- using namespace std;
- class Fahrzeug{
- public:
- float gewicht;
- string marke;
- static int anzahl;
- Fahrzeug(float g, string m):gewicht{g}, marke{m}{
- anzahl++;
- }
- /*
- Fahrzeug(Fahrzeug& f):gewicht{f.gewicht}, marke{f.marke}{
- anzahl++;
- }
- */
- Fahrzeug(){
- }
- static int getAnzahl(){
- return anzahl;
- }
- friend ostream& operator<<(ostream& os,const Fahrzeug& f){
- os<<"Ein Fahrzeug von "<<f.marke<<" mit einem Gewicht von "<< f.gewicht<<" kg"<<endl;
- return os;
- }
- };
- int Fahrzeug::anzahl=0;
- class Kfz: public virtual Fahrzeug{
- public:
- unsigned ps;
- Kfz(unsigned p):ps{p}{
- }
- Kfz(float g,string m,unsigned p):Fahrzeug(g,m),ps{p}{
- }
- friend ostream& operator<<(ostream& os,const Kfz& k){
- os<<"Ein Kfz von "<<k.marke<<" mit einem gewicht von "<<k.gewicht<<" kg und "<<k.ps<<" Ps"<<endl;
- return os;
- }
- };
- class PKW: public virtual Fahrzeug,public Kfz {
- private:
- unsigned sitzplaetze;
- public:
- PKW(float g, string f, unsigned p, unsigned s): Fahrzeug(g,f),Kfz{p},sitzplaetze{s} {
- }
- friend ostream& operator<<(ostream& os,const PKW& p){
- os<<"Ein PKW von "<<p.marke<<" mit einem Gewicht von "<<p.gewicht<<" kg, "<<p.ps<<" PS"<<" und "<<p.sitzplaetze<<" Sitzplaetzen"<<endl;
- return os;
- }
- };
- class LKW: public virtual Fahrzeug , Kfz {
- private:
- unsigned nutzlast;
- public:
- LKW(float g, string f, unsigned p, unsigned n):Fahrzeug(g,f),Kfz{p},nutzlast{n}{
- }
- friend ostream& operator<<(ostream& os,const LKW& l){
- os<<"Ein PKW von "<<l.marke<<" mit einem Gewicht von "<<l.gewicht<<" kg, "<<l.ps<<" PS"<<" und "<<l.nutzlast<<" Nutzplast"<<endl;
- return os;
- }
- };
- class Fahrrad: public Fahrzeug {
- private:
- unsigned rahmenhoehe;
- public:
- Fahrrad(float g, string f, unsigned r):Fahrzeug(g,f),rahmenhoehe{r}{
- }
- friend ostream& operator<<(ostream& os,const Fahrrad& f){
- os<<"Ein Fahrrad von "<<f.marke<<"mit einem gewicht von "<<f.gewicht<<" kg"<<" und Rahmenhoehe "<<f.rahmenhoehe<<endl;
- return os;
- }
- };
- int main() {
- Fahrzeug f1(1260.5, "Audi");
- cout << f1 << endl;
- Fahrzeug f2(f1);
- cout << f2 << endl;
- Kfz k1(1200, "Mercedes", 120);
- cout << k1 << endl;
- PKW p1(1125.52, "Seat", 90, 5);
- cout << p1 << endl;
- LKW l1(12300, "BMW", 500, 20000);
- cout << l1 << endl;
- Fahrrad r1(10.7, "Kettler", 57);
- cout << r1 << endl;
- cout << "Anzahl Fahrzeuge: " << Fahrzeug::getAnzahl() << endl;
- //Anzahl stimmt nicht, weil der Copy constructor die anzahl nicht erhöht, mann müsste die Klasse Fahrzeug um einen eigenen Copy Constructor erweitern.
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement