#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。我嘗試在我創建的函式中將它們作為 args 傳遞,但無法訪問它們。
編輯:我添加了 multiplication.h 和減法.h 在它們中,我只需要呼叫類的函式宣告。
uj5u.com熱心網友回復:
您應該將它們作為引數傳遞給函式呼叫
difference = out2.sub(x, y);
在 .h 檔案中,您應該使用引數定義它們
class subtraction
{
public:
int sub(int x, int y);
};
函式多載
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/364592.html
