您如何将用户输入从 main 传递到其他 类?

How do you pass user input from main to other classes?

#include <iostream>
#include "multiplication.h"
#include "subtraction.h"
using namespace std;

int main() {

    multiplication out;
    subtraction out2;

    int x, y, z;
    int product;
    int difference;

    cout << "Enter two numbers to multiply by: ";
    cin >> x;
    cin >> y; 
    product = out.mult();
    cout << "the product is: " << product;

    cout << "Now enter a number to subtract the product by: ";
    cin >> z;
    difference = out2.sub();
    cout << "the difference is: " << difference;
}

    #pragma once

class multiplication
{
public:

    int mult();

};

#include <iostream>
using namespace std;
#include "multiplication.h"

int multiplication::mult(int x, int y) {

    return x * y;
}

    #pragma once
class subtraction
{
public:

    int sub();
};

#include <iostream>
using namespace std;
#include "subtraction.h"

int subtraction::sub(int product, int z) {

    return product - z;
}

我需要将用户输入变量从 main 传递到 mult,并将用户输入 z 和 product 从 main 传递到 sub。我尝试在我创建的函数中将它们作为参数传递,但它们未被访问。

编辑:我添加了 multiplication.h 和 subtraction.h 在它们中,我只调用了 class.

的函数声明

您应该将它们作为参数传递给函数调用

 difference = out2.sub(x, y);

在 .h 文件中,您应该使用参数定义它们

class subtraction
{
    public:    
    int sub(int x, int y);
};

Function overloading