Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. //
  2. // Created by hakan on 13.12.2019.
  3. //
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. class Fahrzeug{
  10. public:
  11. float gewicht;
  12. string marke;
  13. static int anzahl;
  14.  
  15. Fahrzeug(float g, string m):gewicht{g}, marke{m}{
  16.     anzahl++;
  17. }
  18.  
  19. /*
  20. Fahrzeug(Fahrzeug& f):gewicht{f.gewicht}, marke{f.marke}{
  21.     anzahl++;
  22. }
  23. */
  24.  
  25. Fahrzeug(){
  26.  
  27. }
  28.  
  29.   static int getAnzahl(){
  30.     return anzahl;
  31. }
  32. friend ostream& operator<<(ostream& os,const Fahrzeug& f){
  33.     os<<"Ein Fahrzeug von "<<f.marke<<" mit einem Gewicht von "<< f.gewicht<<" kg"<<endl;
  34.     return os;
  35. }
  36.  
  37. };
  38. int Fahrzeug::anzahl=0;
  39.  
  40.  
  41. class Kfz: public virtual Fahrzeug{
  42. public:
  43.     unsigned ps;
  44.     Kfz(unsigned p):ps{p}{
  45.     }
  46.     Kfz(float g,string m,unsigned p):Fahrzeug(g,m),ps{p}{
  47.     }
  48.  
  49.     friend ostream& operator<<(ostream& os,const Kfz& k){
  50.         os<<"Ein Kfz von "<<k.marke<<" mit einem gewicht von "<<k.gewicht<<" kg und "<<k.ps<<" Ps"<<endl;
  51.         return os;
  52.     }
  53. };
  54.  
  55.  
  56. class PKW: public virtual Fahrzeug,public Kfz {
  57. private:
  58.     unsigned sitzplaetze;
  59.  
  60. public:
  61.     PKW(float g, string f, unsigned p, unsigned s): Fahrzeug(g,f),Kfz{p},sitzplaetze{s} {
  62.     }
  63.  
  64.     friend ostream& operator<<(ostream& os,const PKW& p){
  65.         os<<"Ein PKW von "<<p.marke<<" mit einem Gewicht von "<<p.gewicht<<" kg, "<<p.ps<<" PS"<<" und "<<p.sitzplaetze<<" Sitzplaetzen"<<endl;
  66.         return os;
  67.     }
  68. };
  69.  
  70.  
  71.  
  72. class LKW: public virtual Fahrzeug , Kfz {
  73. private:
  74.     unsigned nutzlast;
  75.  
  76. public:
  77.  
  78.     LKW(float g, string f, unsigned p, unsigned n):Fahrzeug(g,f),Kfz{p},nutzlast{n}{
  79.  
  80.     }
  81.     friend ostream& operator<<(ostream& os,const LKW& l){
  82.         os<<"Ein PKW von "<<l.marke<<" mit einem Gewicht von "<<l.gewicht<<" kg, "<<l.ps<<" PS"<<" und "<<l.nutzlast<<" Nutzplast"<<endl;
  83.         return os;
  84.     }
  85. };
  86.  
  87. class Fahrrad: public Fahrzeug {
  88. private:
  89.     unsigned rahmenhoehe;
  90.  
  91. public:
  92.     Fahrrad(float g, string f, unsigned r):Fahrzeug(g,f),rahmenhoehe{r}{
  93.     }
  94.     friend ostream& operator<<(ostream& os,const Fahrrad& f){
  95.         os<<"Ein Fahrrad von "<<f.marke<<"mit einem gewicht von "<<f.gewicht<<" kg"<<" und Rahmenhoehe "<<f.rahmenhoehe<<endl;
  96.         return os;
  97.     }
  98. };
  99.  
  100. int main() {
  101.  
  102.     Fahrzeug f1(1260.5, "Audi");
  103.     cout << f1 << endl;
  104.     Fahrzeug f2(f1);
  105.     cout << f2 << endl;
  106.     Kfz k1(1200, "Mercedes", 120);
  107.     cout << k1 << endl;
  108.     PKW p1(1125.52, "Seat", 90, 5);
  109.     cout << p1 << endl;
  110.     LKW l1(12300, "BMW", 500, 20000);
  111.     cout << l1 << endl;
  112.     Fahrrad r1(10.7, "Kettler", 57);
  113.     cout << r1 << endl;
  114.     cout << "Anzahl Fahrzeuge: " << Fahrzeug::getAnzahl() << endl;
  115.  
  116.     //Anzahl stimmt nicht, weil der Copy constructor die anzahl nicht erhöht, mann müsste die Klasse Fahrzeug um einen eigenen Copy Constructor erweitern.
  117.  
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement