This C++ program implements a basic application to manage two...

August 27, 2025 at 02:39 PM

#include <iostream> #include <string> // Classe base class Log { public: virtual void esegui() = 0; virtual ~Log() = default; }; // Cliente class Cliente : public Log { public: std::string nome, cognome, email; int puntiFedelta; void esegui() override { std::cout << "Cliente: " << nome << " " << cognome << " Email:" << email << " Punti fedeltà: " << puntiFedelta << "\n"; puntiFedelta += 100; // Simulo aggiornamento punti std::cout << "Punti omaggio: " << puntiFedelta << "\n"; } }; // Ordine class Ordine : public Log { public: std::string prodotto; std::string cid; int quantita; double prezzo; void esegui() override { double totale = quantita * prezzo; std::cout << "Calcolo prezo totale ordine.\n"; std::cout << "Ordine confermato: [" << cid << "] " << prodotto << " - Quantità: " << quantita << " Prezzo unitario: " << prezzo << " €" << " -> Totale: " << totale << " €\n"; } }; int main() { std::string scelta; std::cout << "Vuoi inserire Cliente o Ordine? "; std::getline(std::cin, scelta); // Inizializzo il puntatore alla classe base Log* log = nullptr; if (scelta == "Cliente" || scelta == "cliente") { Cliente* cliente = new Cliente(); std::cout << "Nome: "; std::getline(std::cin, cliente->nome); std::cout << "Cognome: "; std::getline(std::cin, cliente->cognome); std::cout << "Email: "; std::getline(std::cin, cliente->email); cliente->puntiFedelta = 0; // Salvo il cliente nel log log = cliente; } else if (scelta == "Ordine" || scelta == "ordine") { Ordine* ordine = new Ordine(); std::cout << "Prodotto: "; std::getline(std::cin, ordine->prodotto); std::cout << "Codice ID: "; std::getline(std::cin, ordine->cid); std::cout << "Quantità: "; std::cin >> ordine->quantita; std::cout << "Prezzo unitario: "; std::cin >> ordine->prezzo; // Salvo il ordine nel log log = ordine; } else { std::cout << "Scelta non valida.\n"; } // Eseguo le funzionalita di Ordine e Client if (log) { log->esegui(); delete log; } return 0; }

This C++ program implements a basic application to manage two entities: a Cliente (Customer) and an Ordine (Order). It uses polymorphism and the concept of a base class (Log) with a virtual function (esegui) to allow operations to be executed generically regardless of the type (Cliente or Ordine). Here's a step-by-step breakdown of what the code does:


Functional Overview

  1. Base Class Log:

    • Defines a pure virtual method esegui that must be implemented by derived classes.
    • Acts as a common interface for the Cliente and Ordine classes.
  2. Cliente Class:

    • Represents a customer with attributes for name, surname, email, and loyalty points (puntiFedelta).
    • Overrides the esegui method to display customer details and simulate the addition of loyalty points (+100).
  3. Ordine Class:

    • Represents an order with attributes for the product name, an order ID (cid), the quantity of items in the order, and the price per unit.
    • Overrides esegui to calculate and display the total price of the order (quantita * prezzo).

Program Execution (via main function):

  1. Prompt for Input:

    • The user is asked whether they want to create a Cliente or an Ordine. Input is read via std::getline, allowing string inputs with spaces.
  2. Object Creation:

    • Based on the user's choice:
      • For Cliente:
        • The program prompts the user to enter the Cliente's nome (name), cognome (surname), and email.
        • A new Cliente object is created with default loyalty points (puntiFedelta = 0).
      • For Ordine:
        • The program prompts the user to enter the Ordine's prodotto (product name), cid (order ID), quantita (quantity), and prezzo (unit price).
        • A new Ordine object is created.
  3. Execution Using Polymorphism:

    • Regardless of whether the user creates a Cliente or an Ordine, the object is stored in a Log* pointer (log) to the base class.
    • The esegui function is called on the respective object (using dynamic polymorphism) to execute the appropriate functionality:
      • For a Cliente: Displays the customer's details and updates loyalty points.
      • For an Ordine: Calculates and displays the total price of the order.
    • After execution, the object is deleted to free the allocated memory.
  4. Invalid Input Handling:

    • If the user enters a choice other than "Cliente/cliente" or "Ordine/ordine," the program notifies them of an invalid choice and does not proceed further.

Key Concepts and Behaviors:

  1. Dynamic Polymorphism:

    • The use of a base class pointer (Log* log) allows the program to handle both Cliente and Ordine objects transparently.
    • The esegui function is declared as virtual, enabling derived classes to override it with their specific implementations.
  2. Input/Output Interaction:

    • User inputs are collected using std::getline (for strings) or std::cin (for numeric inputs).
    • Output is written to the console using std::cout.
  3. Memory Management:

    • Dynamically allocated objects (using new) are explicitly deleted to prevent memory leaks.

Example Run 1 (Creating a Cliente):

Vuoi inserire Cliente o Ordine? Cliente
Nome: Mario
Cognome: Rossi
Email: mario.rossi@example.com
Cliente: Mario Rossi Email:mario.rossi@example.com Punti fedeltà: 0
Punti omaggio: 100

Example Run 2 (Creating an Ordine):

Vuoi inserire Cliente o Ordine? Ordine
Prodotto: Laptop
Codice ID: A123
Quantità: 2
Prezzo unitario: 1200
Calcolo prezo totale ordine.
Ordine confermato: [A123] Laptop - Quantità: 2 Prezzo unitario: 1200 € -> Totale: 2400 €

Example Run 3 (Invalid Choice):

Vuoi inserire Cliente o Ordine? Test
Scelta non valida.

The code demonstrates a practical use of polymorphism, dynamic memory allocation, and user input handling to manage two types of entities (Cliente and Ordine) with different behaviors under a shared interface (Log).

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node