Advertisement
Guest User

Untitled

a guest
May 24th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string.h>
  4.  
  5. extern void foo(char* m);
  6. using namespace std;
  7. static int anzahl;
  8.  
  9. class Auto {
  10.     int leistung = 0;
  11.  
  12.     char fabrikat[50];
  13.     char* ptr = fabrikat;
  14.  
  15. public:
  16.     Auto() {
  17.         leistung = 50;
  18.         ptr = (char*)"skoda";
  19.         anzahl++;
  20.         cout << "Es gibt " << anzahl << " Auto(s)." << endl;
  21.     }
  22.     Auto (int l) {
  23.         leistung = l;
  24.         ptr = (char*)"skoda";
  25.         anzahl++;
  26.         cout << "Es gibt " << anzahl << "Auto(s)." << endl;
  27.     }
  28.     Auto (int l, char* f) {
  29.         leistung = l;
  30.         ptr = f;
  31.         anzahl++;
  32.         cout << "Es gibt " << anzahl << "Auto(s)." << endl;
  33.     }
  34.     ~Auto() {
  35.         delete [] fabrikat;
  36.         anzahl--;
  37.         cout << "Es gibt " << anzahl << "Auto(s)." << endl;
  38.     }
  39.     int getAnzahl() {
  40.         return anzahl;
  41.     }
  42.     int getLeistung() {
  43.         return leistung;
  44.     }
  45.     void setLeistung(int l) {
  46.         leistung = l;
  47.     }
  48.     void setFabrikat(char* f) {
  49.         ptr = f;
  50.     }
  51.     void print() {
  52.         cout << "Leistung: " << leistung << " Fabrikat: " << ptr << endl;
  53.     }
  54. };
  55.  
  56. int main(int argc, char *argv[]) {
  57.  
  58.     int n, leistungTemp;
  59.  
  60.  
  61.     cout << "Wie viele Autos sollen erstellt werden?" << endl;
  62.     cin >> n;
  63.     Auto array1 [n];
  64.  
  65.     for(int i = 0; i < n; i++) {
  66.         char * fabrikatTemp = new char[50];
  67.         cout << "Bitte geben Sie die Leistung des Autos ein." << endl;
  68.         cin >> leistungTemp;
  69.         cout << "Bitte geben Sie das Fabrikat des Autos ein." << endl;
  70.         cin >> fabrikatTemp;
  71.  
  72.         //Es wird der Standardkonstruktor verwendet und die Werte werden nach der Eingabe gesetzt
  73.         array1[i].setLeistung(leistungTemp);
  74.         array1[i].setFabrikat(fabrikatTemp);
  75.     }
  76.     for(int j = 0; j < n; j++) {
  77.         array1[j].print();
  78.     }
  79.  
  80.     cout << "--------------------------------" << endl;
  81.     //Es wird der Standardkonstruktor verwendet
  82.     Auto array2 [3] = { Auto(), Auto(), Auto()};
  83.     for(int i = 0; i <3; i++) {
  84.         array2[i].print();
  85.     }
  86.  
  87.     system("PAUSE");
  88.     return EXIT_SUCCESS;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement