Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Create two classes food and clothes. Class food should have the following private variables price, expiration date

+4 votes
asked Jul 7, 2019 by anonymous
Create two classes food and clothes. Class food should have the following private variables price, expirationdate (it should be string in the following format dd.mm.YYYY) and category, it should be possible to initialize an object of class food using the constructor. Class clothes should have the following private variables price, size and category, it should be possible to identify an object of class clothes using the constructor. Use the concept of friend classes and write the friend class of food and clothes which will print out the categories of objects of the classes food and clothes, and also print the total price payed for objects of the food and clothes classes.

1 Answer

0 votes
answered Nov 27, 2025 by Brooklyn Taylor (140 points)

#include <iostream>
#include <string>
using namespace std;

class Clothes; // Forward declaration

class Food {
private:
    double price;
    string expirationDate; // format dd.mm.YYYY
    string category;

public:
    // Constructor
    Food(double p, string expDate, string cat) 
        : price(p), expirationDate(expDate), category(cat) {}

    // Declare friend class
    friend class Shop;
};

class Clothes {
private:
    double price;
    string size;
    string category;

public:
    // Constructor
    Clothes(double p, string s, string cat) 
        : price(p), size(s), category(cat) {}

    // Declare friend class
    friend class Shop;
};

class Shop {
public:
    // Print categories of Food and Clothes
    void printCategories(const Food &f, const Clothes &c) {
        cout << "Food category: " << f.category << endl;
        cout << "Clothes category: " << c.category << endl;
    }

    // Print total price of Food and Clothes
    void printTotalPrice(const Food &f, const Clothes &c) {
        double total = f.price + c.price;
        cout << "Total price paid: $" << total << endl;
    }
};

int main() {
    // Create objects
    Food apple(2.5, "27.11.2025", "Fruit");
    Clothes tshirt(15.0, "M", "Casual Wear");

    // Friend class usage
    Shop shop;
    shop.printCategories(apple, tshirt);
    shop.printTotalPrice(apple, tshirt);

    return 0;
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...