Question

I am newbie to C++ programming.

Below is my code having two classes, class 1 and class 2 which does some calculations based on the values from the main function sent through the object pointers myclass1 and myclass2.The code works fine but now I need to make a change, that is only when the function calc of class1 returns true value, the class2 and its member function computation should get its value x from main function and algorithm runs. i.e if class1 returns true, class 2 runs else No.

It would be grateful if someone could hint me a idea and say how to proceed with it.

Thanks in advance

Code

class1.h

class class1
{
public:
    class1()
    {
    };
    ~class1()
    {
    };

    bool calc(int a, int b, int c)
    {
    if (some condition){ // returns true or false based on some condition
    result=true;
    }
    else{
    result=false;
    }

    return result;
    };

class2.h

class class2
{
public:
    class2()
    {
    };
    ~class2()
    {
    };

    int computation (int x)
    {
    do some calculations
    result=1;
    }
    return result;
    };

main.cpp

#include "class1.h"
#include "class2.h"

using namespace std;

int main(int argc, char * argv[])

class1* myclass1 = new class1();
class2* myclass2 = new class2();

int x=0.5, y=0.7, z=0.9;


int result1 = myclass1->calc(x,y,z);

int result2 = myclass2-> computation(x); // should be assigned or activated only when result1 is true.

return 0;
}
Was it helpful?

Solution

int result = 0;
if(myclass1->calc(x,y,z))
   result = myclass2->computation(x);

Note that with Class1, you're assigning a bool to an int - make sure you have proper type assignment.

OTHER TIPS

Just use a simple if:

bool result1 = myclass1->calc(x,y,z);
int result2 = -1; // -1 means not computed in this case

if(result1){
  result2 = myclass2-> computation(x); // should be assigned or activated only when result1 is true.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top