This C++ program implements a basic application to manage two...
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
-
Base Class
Log
:- Defines a pure virtual method
esegui
that must be implemented by derived classes. - Acts as a common interface for the
Cliente
andOrdine
classes.
- Defines a pure virtual method
-
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).
- Represents a customer with attributes for name, surname, email, and loyalty points (
-
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
).
- Represents an order with attributes for the product name, an order ID (
Program Execution (via main
function):
-
Prompt for Input:
- The user is asked whether they want to create a
Cliente
or anOrdine
. Input is read viastd::getline
, allowing string inputs with spaces.
- The user is asked whether they want to create a
-
Object Creation:
- Based on the user's choice:
- For
Cliente
:- The program prompts the user to enter the
Cliente
'snome
(name),cognome
(surname), andemail
. - A new
Cliente
object is created with default loyalty points (puntiFedelta = 0
).
- The program prompts the user to enter the
- For
Ordine
:- The program prompts the user to enter the
Ordine
'sprodotto
(product name),cid
(order ID),quantita
(quantity), andprezzo
(unit price). - A new
Ordine
object is created.
- The program prompts the user to enter the
- For
- Based on the user's choice:
-
Execution Using Polymorphism:
- Regardless of whether the user creates a
Cliente
or anOrdine
, the object is stored in aLog*
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.
- For a
- After execution, the object is deleted to free the allocated memory.
- Regardless of whether the user creates a
-
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:
-
Dynamic Polymorphism:
- The use of a base class pointer (
Log* log
) allows the program to handle bothCliente
andOrdine
objects transparently. - The
esegui
function is declared asvirtual
, enabling derived classes to override it with their specific implementations.
- The use of a base class pointer (
-
Input/Output Interaction:
- User inputs are collected using
std::getline
(for strings) orstd::cin
(for numeric inputs). - Output is written to the console using
std::cout
.
- User inputs are collected using
-
Memory Management:
- Dynamically allocated objects (using
new
) are explicitly deleted to prevent memory leaks.
- Dynamically allocated objects (using
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
).