Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading