No title

 #include <iostream>

#include <string>

using namespace std;


// ==========================

// 1. Product Class

// ==========================

class Product {

private:

    string name;    // [cite: 12]

    string id;      // [cite: 13]

    double price;   // [cite: 14]


public:

    // Default Constructor [cite: 19]

    Product() {}


    // Parameterized Constructor [cite: 20]

    Product(string name, string id, double price) {

        this->name = name;

        this->id = id;

        this->price = price;

    }


    // Display function: Prints details of the product [cite: 16]

    void display() {

        cout << "Name: " << name 

             << ", ID: " << id 

             << ", Price: " << price << " BDT" << endl;

    }


    // Getters and Setters (Helper functions for Cart logic)

    string getId() { return id; }

    double getPrice() { return price; }

    void setPrice(double newPrice) { price = newPrice; }

};


// ==========================

// 2. Cart Class

// ==========================

class Cart {

private:

    Product* products; // Dynamically allocated array [cite: 23]

    int itemCount;     // Current number of items [cite: 24]

    int maxItems;      // Maximum capacity [cite: 25]


public:

    // Parameterized Constructor [cite: 36]

    Cart(int maxItems) {

        this->maxItems = maxItems;

        this->itemCount = 0;

        this->products = new Product[maxItems]; // Allocate memory

    }


    // Copy Constructor: Performs Deep Copy 

    Cart(const Cart& other) {

        this->maxItems = other.maxItems;

        this->itemCount = other.itemCount;

        

        // Allocate NEW memory for this cart (Deep Copy)

        this->products = new Product[maxItems];

        

        // Copy each product individually from the other cart

        for (int i = 0; i < itemCount; i++) {

            this->products[i] = other.products[i];

        }

    }


    // Destructor: Frees dynamically allocated memory 

    ~Cart() {

        delete[] products;

    }


    // Add Product: Adds a new product to the cart [cite: 27]

    void addProduct(Product p) {

        if (itemCount < maxItems) {

            products[itemCount] = p;

            itemCount++;

        } else {

            cout << "Cart is full!" << endl;

        }

    }


    // Update Price: Updates price of specific product by ID [cite: 30]

    void updatePrice(string id, double newPrice) {

        for (int i = 0; i < itemCount; i++) {

            if (products[i].getId() == id) {

                products[i].setPrice(newPrice);

                return;

            }

        }

        cout << "Product ID not found." << endl;

    }


    // Get Most Expensive: Returns product with highest price [cite: 31]

    Product getMostExpensive() {

        if (itemCount == 0) return Product();


        int maxIndex = 0;

        for (int i = 1; i < itemCount; i++) {

            if (products[i].getPrice() > products[maxIndex].getPrice()) {

                maxIndex = i;

            }

        }

        return products[maxIndex];

    }


    // Show Cart: Displays all products [cite: 34]

    void showCart() {

        for (int i = 0; i < itemCount; i++) {

            products[i].display();

        }

    }

};


// ==========================

// 3. Main Function

// ==========================

// The main function strictly follows the sample provided in the task [cite: 38-53]

int main() {

    Cart c1(5);

    Product p1("Gaming Laptop", "GL-501", 125000);

    Product p2("Mechanical Keyboard", "MK-02", 4500);

    Product p3("Wireless Mouse", "WM-10", 1200);


    c1.addProduct(p1);

    c1.addProduct(p2);

    c1.addProduct(p3);


    cout << "Cart 1 Contents:\n";

    c1.showCart();


    // Test Copy Constructor (Deep Copy)

    Cart c2 = c1;


    // Modify c2: Eid Discount on Laptop, and add a Monitor

    c2.updatePrice("GL-501", 110000);

    c2.addProduct(Product("Curved Monitor", "CM-24", 28000));


    cout << "\nCart 1 Most Expensive:\n";

    c1.getMostExpensive().display(); // Should be 125000 BDT


    cout << "\nCart 2 Contents (After Discount & Add):\n";

    c2.showCart(); // Laptop is 110000, Monitor added


    cout << "\nCart 2 Most Expensive:\n";

    c2.getMostExpensive().display(); // Should be 110000 BDT


    return 0;

}

Rakib

রাকিব "এক্সপার্ট বাংলাদেশ" এর প্রতিষ্ঠাতা এবং মালিক। সে অবসর সময়ে ব্লগিং ও লেখালেখি করতে ভালোবাসে। তাছাড়াও, অনলাইনে নতুন কিছু শেখা তার প্রধান শখ।

Post a Comment

কমেন্ট করার মিনতি করছি। আমরা আপনার কমেন্টকে যথেস্ট মূল্য প্রদান করি। এটি আমাদের সার্ভিসের অংশ।

তবে কোনো ওয়েবসাইট লিংক প্রকাশ না করার অনুরোধ রইল।

Previous Post Next Post