Advertisement
AntoniiaG

Car

Jan 29th, 2023
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | Source Code | 0 0
  1. // AutoCar.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. /*
  3. Даден е базов абстрактен клас Auto, който съдържа полетата тип на автомобила (лек, товарен).
  4. Съдържа символен низ със съответната дума - марка на автомобила и година на производство.
  5. Инициализиращия конструктор да задава стойност на тези полета чрез параметри.
  6. Реализирайте абстрактен метод за извеждане на съдържанието на обекта в символен низ.
  7. Даден е производен клас на класа Auto - лек автомобил - Car с полета наследени от Auto и
  8. максимална скорост.
  9. Демонстрирайте работата на класовете със създаване на колекция от различен тип автомобили и
  10. извеждане на информация за всички леки автомобили, които са произведени преди 2000 година.
  11. */
  12.  
  13. #include <iostream>
  14. #include <string.h>
  15. using namespace std;
  16.  
  17. class Auto
  18. {
  19. private:
  20.     string type;
  21.     string brand;
  22.     int buildYear;
  23. public:
  24.     Auto(string Type, string Brand, int yearBuild)
  25.     {
  26.         type = Type;
  27.         brand = Brand;
  28.         buildYear = yearBuild;
  29.     }
  30.     virtual void display() = 0;
  31.  
  32.     string getType()
  33.     {
  34.         return type;
  35.     }
  36.  
  37.     string getBrand()
  38.     {
  39.         return brand;
  40.     }
  41.  
  42.     int getYearBuild()
  43.     {
  44.         return buildYear;
  45.     }
  46. };
  47.  
  48. class Car : public Auto
  49. {
  50. private:
  51.     int maxSpeed;
  52. public:
  53.     Car(int speedMax, string Type, string Brand, int yearBuild) : Auto (Type,  Brand, yearBuild)
  54.     {
  55.         maxSpeed = speedMax;
  56.     }
  57.  
  58.     void display()
  59.     {
  60.         cout << "Tip: " << getType() << endl;
  61.         cout << "Marka: " << getBrand() << endl;
  62.         cout << "Godina: " << getYearBuild () << endl;
  63.         cout << "Max skorost: " << maxSpeed << endl;
  64.     }
  65. };
  66.  
  67. Car* carArray[2];
  68. void addCarToArray(Car* car, int size)
  69. {
  70.     for (int i = 0; i < size; i++)
  71.     {
  72.         carArray[i] = car;
  73.     }
  74. };
  75.  
  76. int main()
  77. {
  78.     Auto* p;
  79.     Auto* arr[2];
  80.  
  81.     Car c1(202, "lek", "AUDI", 1997);
  82.     p = &c1;
  83.     arr[0] = p;
  84.  
  85.     Car c2(201, "tovaren", "BMW", 2005);
  86.     p = &c2;
  87.     arr[1] = p;
  88.  
  89.     for (int i = 0; i < 2; i++)
  90.     {
  91.         if (arr[i]->getYearBuild() < 2000 && arr[i]->getType() == "lek") {
  92.             arr[i]->display();
  93.         }
  94.     }
  95. }
  96.  
  97. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  98. // Debug program: F5 or Debug > Start Debugging menu
  99.  
  100. // Tips for Getting Started:
  101. //   1. Use the Solution Explorer window to add/manage files
  102. //   2. Use the Team Explorer window to connect to source control
  103. //   3. Use the Output window to see build output and other messages
  104. //   4. Use the Error List window to view errors
  105. //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  106. //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement