1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include <iostream> #include <vector> #include <string> #include <iomanip> // Command class class Command { public: Command(const std::string& type, double parameter) : type(type), parameter(parameter) {} std::string getType() const { return type; } double getParameter() const { return parameter; } private: std::string type; // Type of operation (e.g., "Cut", "Drill") double parameter; // Parameter for the operation (e.g., depth, diameter) }; // CNC Machine class class CNCMachine { public: void addCommand(const Command& command) { commands.push_back(command); } void executeCommands() { for (const auto& command : commands) { std::cout << "Executing " << command.getType() << " with parameter: " << std::fixed << std::setprecision(2) << command.getParameter() << std::endl; } commands.clear(); } private: std::vector<Command> commands; // List of commands to be executed }; // Main function int main() { CNCMachine cncMachine; // Adding commands to the CNC machine cncMachine.addCommand(Command("Cut", 15.0)); // Cut operation with depth 15.0 cncMachine.addCommand(Command("Drill", 5.0)); // Drill operation with diameter 5.0 cncMachine.addCommand(Command("Cut", 10.0)); // Another Cut operation with depth 10.0 // Executing all commands std::cout << "Starting CNC machine operations..." << std::endl; cncMachine.executeCommands(); return 0; } |
Explanation:
- Command Class:
type
: Represents the type of operation (e.g., “Cut”, “Drill”).parameter
: Represents the parameter for the operation (e.g., depth for cutting, diameter for drilling).Command(const std::string& type, double parameter)
: Constructor to initialize the command.getType() const
: Returns the type of the command.getParameter() const
: Returns the parameter of the command.
- CNCMachine Class:
commands
: A vector to store a list of commands to be executed.addCommand(const Command& command)
: Adds a command to the list.executeCommands()
: Executes all commands in the list. Prints out each command’s type and parameter, then clears the command list.
- Main Function:
- Creates a
CNCMachine
object. - Adds several commands to the CNC machine (e.g., cutting and drilling).
- Executes all commands and prints the results.
- Creates a