Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. class Lista;
  6.  
  7. class obj
  8. {
  9.  
  10.     int a;
  11.     char *b;
  12.     obj *urm;
  13.  
  14. public:
  15.     obj(int a1, char *b1)
  16.     {
  17.         a = a1;
  18.         strcpy(b, b1);
  19.         urm = NULL;
  20.     }
  21.  
  22.     virtual void afisare()
  23.     {
  24.  
  25.         if (a == 1)
  26.             cout << "obiect1" << endl;
  27.         if (a == 2)
  28.             cout << "obiect2" << endl;
  29.         cout << "Nume" << b << endl;
  30.     }
  31.  
  32.     friend class Lista;
  33. };
  34. class lucru : public obj
  35. {
  36. private:
  37.     char *greutate;
  38.  
  39. public:
  40.     lucru(int a1, char *b1, char *g) : obj(a1, b1)
  41.     {
  42.         greutate = new char[strlen(g) + 1];
  43.         strcpy(greutate, g);
  44.     }
  45.     void afisare()
  46.     {
  47.         obj::afisare();
  48.         cout << "Greutate:" << greutate << endl;
  49.     }
  50.     friend class Lista;
  51. };
  52.  
  53. class chestie : public obj
  54. {
  55.     int lung;
  56.  
  57. public:
  58.     chestie(int a1, char *b1, int l) : obj(a1, b1)
  59.     {
  60.         lung = l;
  61.     }
  62.  
  63.     void afisare()
  64.     {
  65.         obj::afisare();
  66.         cout << "Lungime" << lung << endl;
  67.     }
  68.     friend class Lista;
  69. };
  70.  
  71. class Lista
  72. {
  73. public:
  74.     obj *head;
  75.     void adaugare(obj *a);
  76.     void afisare();
  77.     void cautare();
  78. };
  79. void Lista::adaugare(obj *a)
  80. {
  81.     obj *p;
  82.     if (head == NULL)
  83.     {
  84.         head = a;
  85.     }
  86.     else
  87.     {
  88.         p = head;
  89.         while (p->urm != NULL)
  90.             p = p->urm;
  91.         p->urm = a;
  92.     }
  93. }
  94. void Lista::afisare()
  95. {
  96.     obj *p;
  97.     p = head;
  98.     if (!p)
  99.     {
  100.         cout << "Lista vida!";
  101.     }
  102.     else
  103.     {
  104.         while (p)
  105.         {
  106.             p->afisare();
  107.             p = p->urm;
  108.             system("pause");
  109.         }
  110.     }
  111. }
  112. void introducere(Lista &l, int a)
  113. {
  114.     obj *p;
  115.     char *b;
  116.     int lung;
  117.     char *greutate;
  118.     cout << "Dati nume" << endl;
  119.     cin >> b;
  120.  
  121.     if (a == 1)
  122.     {
  123.         lucru *i;
  124.         cout << "Greutate:" << endl;
  125.         cin >> greutate;
  126.         i = new lucru(a, b, greutate);
  127.         p = i;
  128.         l.adaugare(p);
  129.     }
  130.     if (a == 2)
  131.     {
  132.         chestie *i;
  133.         cout << "Lungime:" << endl;
  134.         cin >> lung;
  135.         i = new chestie(a, b, lung);
  136.         p = i;
  137.         l.adaugare(p);
  138.     }
  139. }
  140. int main()
  141. {
  142.     Lista l;
  143.     l.head = NULL;
  144.  
  145.     introducere(l, 1);
  146.     introducere(l, 2);
  147.     l.afisare();
  148.  
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement