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

Time:11-24

#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;
}

I need to pass the user input variables from main to mult and the user input z and product from main to sub. I tried passing them as args in the functions I've created, but they are not accessed.

Edit: I added multiplication.h and subtraction.h In them I just have the call to the function declarations for the class.

CodePudding user response:

You should pass them to the function call as arguments

 difference = out2.sub(x, y);

In the .h files you should define them with arguments

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

Function overloading

  • Related