Advertisement
aidanozo

Untitled

Nov 26th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. // Interfața Vehicle
  6. class Vehicle {
  7. public:
  8.     virtual void displayInfo() const = 0; // metodă pur virtuală
  9.     virtual void setPrice(int price) = 0; // metodă pur virtuală
  10.     virtual int getPrice() const = 0; // metodă pur virtuală
  11.     virtual ~Vehicle() {}
  12. };
  13.  
  14. // Clasa abstractă Electric
  15. class Electric {
  16. protected:
  17.     char* brand;
  18.     int year;
  19.     int price;
  20.  
  21. public:
  22.     Electric(const char* brand = "", int year = 0, int price = 0)
  23.         : year(year), price(price) {
  24.         this->brand = new char[strlen(brand) + 1];
  25.         strcpy(this->brand, brand);
  26.     }
  27.  
  28.     Electric(const Electric& other) {
  29.         brand = new char[strlen(other.brand) + 1];
  30.         strcpy(brand, other.brand);
  31.         year = other.year;
  32.         price = other.price;
  33.     }
  34.  
  35.     Electric& operator=(const Electric& other) {
  36.         if (this != &other) {
  37.             delete[] brand;
  38.             brand = new char[strlen(other.brand) + 1];
  39.             strcpy(brand, other.brand);
  40.             year = other.year;
  41.             price = other.price;
  42.         }
  43.         return *this;
  44.     }
  45.  
  46.     virtual ~Electric() {
  47.         delete[] brand;
  48.     }
  49.  
  50.     // Getters și setters
  51.     const char* getBrand() const { return brand; }
  52.     void setBrand(const char* newBrand) {
  53.         delete[] brand;
  54.         brand = new char[strlen(newBrand) + 1];
  55.         strcpy(brand, newBrand);
  56.     }
  57.  
  58.     int getYear() const { return year; }
  59.     void setYear(int newYear) { year = newYear; }
  60.  
  61.     int getPrice() const { return price; }
  62.     void setPrice(int newPrice) { price = newPrice; }
  63.  
  64.     virtual void displayInfo() const {
  65.         cout << "Brand: " << brand << ", Year: " << year << ", Price: " << price << endl;
  66.     }
  67. };
  68.  
  69. // Clasa derivată Car
  70. class Car : public Vehicle {
  71. protected:
  72.     int price;
  73.     int year;
  74.     char* brand;
  75.  
  76. public:
  77.     Car(const char* brand = "", int year = 0, int price = 0)
  78.         : year(year), price(price) {
  79.         this->brand = new char[strlen(brand) + 1];
  80.         strcpy(this->brand, brand);
  81.     }
  82.  
  83.     Car(const Car& other) {
  84.         brand = new char[strlen(other.brand) + 1];
  85.         strcpy(brand, other.brand);
  86.         year = other.year;
  87.         price = other.price;
  88.     }
  89.  
  90.     Car& operator=(const Car& other) {
  91.         if (this != &other) {
  92.             delete[] brand;
  93.             brand = new char[strlen(other.brand) + 1];
  94.             strcpy(brand, other.brand);
  95.             year = other.year;
  96.             price = other.price;
  97.         }
  98.         return *this;
  99.     }
  100.  
  101.     virtual ~Car() {
  102.         delete[] brand;
  103.     }
  104.  
  105.     void setPrice(int price) override { this->price = price; }
  106.     int getPrice() const override { return price; }
  107.  
  108.     void displayInfo() const override {
  109.         cout << "Car - Brand: " << brand << ", Year: " << year << ", Price: " << price << endl;
  110.     }
  111.  
  112.     // Operator <
  113.     bool operator<(const Car& other) const {
  114.         return this->price < other.price;
  115.     }
  116. };
  117.  
  118. // Clasa derivată ElectricCar
  119. class ElectricCar : public Car, public Electric {
  120. private:
  121.     double batteryCapacity;
  122.  
  123. public:
  124.     ElectricCar(const char* brand = "", int year = 0, int price = 0, double batteryCapacity = 0.0)
  125.         : Car(brand, year, price), Electric(brand, year, price), batteryCapacity(batteryCapacity) {}
  126.  
  127.     void setBatteryCapacity(double capacity) { batteryCapacity = capacity; }
  128.     double getBatteryCapacity() const { return batteryCapacity; }
  129.  
  130.     void displayInfo() const override {
  131.         cout << "ElectricCar - Brand: " << brand
  132.              << ", Year: " << year
  133.              << ", Price: " << price
  134.              << ", Battery Capacity: " << batteryCapacity << " kWh" << endl;
  135.     }
  136. };
  137.  
  138. int main() {
  139.     // Nu putem crea un obiect de tip Vehicle deoarece este o clasă abstractă.
  140.     // Vehicle v;
  141.  
  142.     // Testarea clasei Car
  143.     Car car1("Toyota", 2020, 20000);
  144.     Car car2("Honda", 2018, 15000);
  145.     car1.displayInfo();
  146.     car2.displayInfo();
  147.     cout << (car1 < car2 ? "car1 is cheaper" : "car2 is cheaper") << endl;
  148.  
  149.     // Testarea clasei ElectricCar
  150.     ElectricCar ecar1("Tesla", 2022, 50000, 75.0);
  151.     ecar1.displayInfo();
  152.     ecar1.setPrice(48000);
  153.     ecar1.setBatteryCapacity(80.0);
  154.     ecar1.displayInfo();
  155.  
  156.     // Obiecte de tip Vehicle* alocând ElectricCar și Car
  157.     Vehicle* v1 = new ElectricCar("Nissan Leaf", 2021, 30000, 40.0);
  158.     Vehicle* v2 = new Car("Ford", 2019, 18000);
  159.  
  160.     v1->displayInfo();
  161.     v2->displayInfo();
  162.  
  163.     delete v1;
  164.     delete v2;
  165.  
  166.     return 0;
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement