Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- // Interfața Vehicle
- class Vehicle {
- public:
- virtual void displayInfo() const = 0; // metodă pur virtuală
- virtual void setPrice(int price) = 0; // metodă pur virtuală
- virtual int getPrice() const = 0; // metodă pur virtuală
- virtual ~Vehicle() {}
- };
- // Clasa abstractă Electric
- class Electric {
- protected:
- char* brand;
- int year;
- int price;
- public:
- Electric(const char* brand = "", int year = 0, int price = 0)
- : year(year), price(price) {
- this->brand = new char[strlen(brand) + 1];
- strcpy(this->brand, brand);
- }
- Electric(const Electric& other) {
- brand = new char[strlen(other.brand) + 1];
- strcpy(brand, other.brand);
- year = other.year;
- price = other.price;
- }
- Electric& operator=(const Electric& other) {
- if (this != &other) {
- delete[] brand;
- brand = new char[strlen(other.brand) + 1];
- strcpy(brand, other.brand);
- year = other.year;
- price = other.price;
- }
- return *this;
- }
- virtual ~Electric() {
- delete[] brand;
- }
- // Getters și setters
- const char* getBrand() const { return brand; }
- void setBrand(const char* newBrand) {
- delete[] brand;
- brand = new char[strlen(newBrand) + 1];
- strcpy(brand, newBrand);
- }
- int getYear() const { return year; }
- void setYear(int newYear) { year = newYear; }
- int getPrice() const { return price; }
- void setPrice(int newPrice) { price = newPrice; }
- virtual void displayInfo() const {
- cout << "Brand: " << brand << ", Year: " << year << ", Price: " << price << endl;
- }
- };
- // Clasa derivată Car
- class Car : public Vehicle {
- protected:
- int price;
- int year;
- char* brand;
- public:
- Car(const char* brand = "", int year = 0, int price = 0)
- : year(year), price(price) {
- this->brand = new char[strlen(brand) + 1];
- strcpy(this->brand, brand);
- }
- Car(const Car& other) {
- brand = new char[strlen(other.brand) + 1];
- strcpy(brand, other.brand);
- year = other.year;
- price = other.price;
- }
- Car& operator=(const Car& other) {
- if (this != &other) {
- delete[] brand;
- brand = new char[strlen(other.brand) + 1];
- strcpy(brand, other.brand);
- year = other.year;
- price = other.price;
- }
- return *this;
- }
- virtual ~Car() {
- delete[] brand;
- }
- void setPrice(int price) override { this->price = price; }
- int getPrice() const override { return price; }
- void displayInfo() const override {
- cout << "Car - Brand: " << brand << ", Year: " << year << ", Price: " << price << endl;
- }
- // Operator <
- bool operator<(const Car& other) const {
- return this->price < other.price;
- }
- };
- // Clasa derivată ElectricCar
- class ElectricCar : public Car, public Electric {
- private:
- double batteryCapacity;
- public:
- ElectricCar(const char* brand = "", int year = 0, int price = 0, double batteryCapacity = 0.0)
- : Car(brand, year, price), Electric(brand, year, price), batteryCapacity(batteryCapacity) {}
- void setBatteryCapacity(double capacity) { batteryCapacity = capacity; }
- double getBatteryCapacity() const { return batteryCapacity; }
- void displayInfo() const override {
- cout << "ElectricCar - Brand: " << brand
- << ", Year: " << year
- << ", Price: " << price
- << ", Battery Capacity: " << batteryCapacity << " kWh" << endl;
- }
- };
- int main() {
- // Nu putem crea un obiect de tip Vehicle deoarece este o clasă abstractă.
- // Vehicle v;
- // Testarea clasei Car
- Car car1("Toyota", 2020, 20000);
- Car car2("Honda", 2018, 15000);
- car1.displayInfo();
- car2.displayInfo();
- cout << (car1 < car2 ? "car1 is cheaper" : "car2 is cheaper") << endl;
- // Testarea clasei ElectricCar
- ElectricCar ecar1("Tesla", 2022, 50000, 75.0);
- ecar1.displayInfo();
- ecar1.setPrice(48000);
- ecar1.setBatteryCapacity(80.0);
- ecar1.displayInfo();
- // Obiecte de tip Vehicle* alocând ElectricCar și Car
- Vehicle* v1 = new ElectricCar("Nissan Leaf", 2021, 30000, 40.0);
- Vehicle* v2 = new Car("Ford", 2019, 18000);
- v1->displayInfo();
- v2->displayInfo();
- delete v1;
- delete v2;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement