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

//****************** .h ***********************

class Funcion {
public:
    Funcion();
    ~Funcion();

    friend void LeerCoeficientes(Funcion &f);
    friend void MostrarFuncion(const Funcion &f);
    friend double DerivadaParcial_x(const Funcion &f, double x, double y);
    friend double DerivadaParcial_y(const Funcion &f, double x, double y);

private:
    double a, b, c, d; // Coeficientes de la función z = ax^2 + by^2 + cxy + d
};

//****************** .cpp ***********************

Funcion::Funcion() : a(0), b(0), c(0), d(0) {}
Funcion::~Funcion() {
    cout << "Cálculos de derivadas parciales finalizados." << endl;
}

void LeerCoeficientes(Funcion &f) {
    cout << "Introduce el coeficiente a (de x^2): ";
    cin >> f.a;
    cout << "Introduce el coeficiente b (de y^2): ";
    cin >> f.b;
    cout << "Introduce el coeficiente c (de xy): ";
    cin >> f.c;
    cout << "Introduce el coeficiente d (término independiente): ";
    cin >> f.d;
}

void MostrarFuncion(const Funcion &f) {
    cout << "Función: z = " << f.a << "x^2 + " << f.b << "y^2 + " << f.c << "xy + " << f.d << endl;
}

double DerivadaParcial_x(const Funcion &f, double x, double y) {
    // Parcial de z respecto a x: ∂z/∂x = 2ax + cy
    return 2 * f.a * x + f.c * y;
}

double DerivadaParcial_y(const Funcion &f, double x, double y) {
    // Parcial de z respecto a y: ∂z/∂y = 2by + cx
    return 2 * f.b * y + f.c * x;
}

//****************** .main ***********************

int main() {
    Funcion f;
    int opc;
    double x, y;

    cout << "===================================================" << endl;
    cout << "*      DERIVADAS PARCIALES: CLASES AMIGAS         *" << endl;
    cout << "===================================================" << endl;
    cout << "Presiona ENTER para continuar..." << endl;
    cin.get();

    do {
        cout << "****************** MENU ******************" << endl;
        cout << "1. Leer coeficientes de la función" << endl;
        cout << "2. Mostrar la función" << endl;
        cout << "3. Calcular derivada parcial respecto a x" << endl;
        cout << "4. Calcular derivada parcial respecto a y" << endl;
        cout << "0. Salir" << endl;
        cout << "Elige una opción: ";
        cin >> opc;

        switch (opc) {
        case 1:
            LeerCoeficientes(f);
            break;
        case 2:
            MostrarFuncion(f);
            break;
        case 3:
            cout << "Introduce el valor de x: ";
            cin >> x;
            cout << "Introduce el valor de y: ";
            cin >> y;
            cout << "Derivada parcial respecto a x: " << DerivadaParcial_x(f, x, y) << endl;
            break;
        case 4:
            cout << "Introduce el valor de x: ";
            cin >> x;
            cout << "Introduce el valor de y: ";
            cin >> y;
            cout << "Derivada parcial respecto a y: " << DerivadaParcial_y(f, x, y) << endl;
            break;
        case 0:
            cout << "Saliendo del programa..." << endl;
            break;
        default:
            cout << "Opción no válida." << endl;
        }
        //cout << endl;
    } while (opc != 0);

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: