简体   繁体   English

调用方法将输出返回到同一类C ++的另一个方法中

[英]Calling a method return output into another method of the same class c++

The code below tries to adds two numbers. 下面的代码尝试将两个数字相加。 I'd like to build a class that does that , for the fun of it. 我想建立一个可以做到这一点的类,以获取乐趣。

So, I have method add that adds the 2 numbers and returns result I'd like the method getAdd to print the return output of the add method. 因此,我有add方法,将两个数字相加并返回result我希望方法getAdd打印出add方法的返回输出。

How do I do this ?? 我该怎么做呢 ??

Below is the code 下面是代码

#include <iostream>
#include <string>

class Addition 
{
    public:
        Addition(double a=0, double b=0);
        double add(double a, double b);
        void getAdd() const;

    private:
        double mA;
        double mB;
};

Addition::Addition(double a, double b) : mA(a), mB(b) {}

double Addition::add(double a, double b)
{
    double result = a + b;

    return result;

}    

void Addition::getAdd() const
{   

    std::cout << "Result is " << //print here the result output of add method << std::endl ;
}          

int main()
{
    Addition sum;
    double num_a;
    double num_b;
    std::cout << "Enter first number to be added\n";
    std::cin >> num_a;
    std::cout << "Enter second number to be added\n";
    std::cin >> num_b;
    sum.add(num_a,num_b);
    sum.getAdd();

    return 0;

} 

Pass the parameters to getAdd? 将参数传递给getAdd?

void Addition::getAdd(double a, double b) const
{
   std::cout << "Result is " << add(a,b);
}

Store the result? 存储结果?

class Addition 
{
    public:
        Addition(double a=0, double b=0);
        double add(double a, double b);
        void getAdd() const;

    private:
        double mResult;
};

double Addition::add(double a, double b)
{
    mResult = a + b;

    return mResult ;
}    
void Addition::getAdd() const
{
   std::cout << "Result is " << mResult ;
}

If I get you right, first you need to declare another variable (it can be double result; ). 如果我理解正确,则首先需要声明另一个变量(它可能是double result; )。 Instead of using a local variable called result , use the one you've just created. 与其使用局部变量result ,不如使用刚刚创建的局部变量。 Your class would be: 您的课程为:

class Addition 
{
    public:
        Addition(double a=0, double b=0);
        double add(double a, double b);
        void getAdd() const;

    private:
        double mA;
        double mB;
        double result;
};

Your add() method would be: 您的add()方法将是:

double Addition::add(double a, double b)
{
    result = a + b;

    return result;

}    

And your getAdd() would be: 您的getAdd()将是:

void Addition::getAdd() const {   

    std::cout << "Result is " << result << std::endl ;
}  

You need this because your getAdd() is a const function, and it cannot call add() directly. 您需要这样做,因为getAdd()是const函数,它不能直接调用add()。 =) =)

Define another class member double last_added; 定义另一个类成员double last_added; . Initialise it to something sensible in the constructor. 在构造函数中将其初始化为明智的方法。 You need to do that in case someone calls getAdd() before the first call of add() : the behaviour on reading an uninitialised variable is undefined in C++. 如果有人在第一次调用add() getAdd()之前调用getAdd()getAdd() :在C ++中未定义读取未初始化变量的行为。

Then, in your add function, replace the return with 然后,在您的add函数中,将return替换为

return last_added = result; /*the single = is deliberate*/

You could refactor getAdd() , to return that value: 您可以重构getAdd() ,以返回该值:

return last_added;

or write the last value to standard output using std::cout << "Result is " << last_added; 或使用std::cout << "Result is " << last_added;将最后一个值写入标准输出std::cout << "Result is " << last_added;

This is one approach. 这是一种方法。 Another one is to use the mA and mB members that you've already defined. 另一种方法是使用已经定义的mAmB成员。 The function body of add becomes add的功能体变为

return mA = a + mB = b;

That's a real touchstone for your operator precedence knowledge. 这是您的操作员优先知识的真正试金石。 This has the advantage of your not needing an extra field, since you could write std::cout << "Result is " << mA + mB; 这样做的好处是不需要额外的字段,因为您可以编写std::cout << "Result is " << mA + mB; . But then you're effectively computing the addition twice which is cumbersome. 但是,然后您要有效地计算两次加法,这很麻烦。

Do also consider implications of multithreading. 还要考虑多线程的含义。

We have two ways of achieving the desired result, 我们有两种方法可以达到预期的效果,

  1. Call the function add() from getAdd(), for that you'll have to change the definition of getAdd() to include two parameters. 从getAdd()调用函数add(),为此,您必须更改getAdd()的定义以包括两个参数。

    void Addition::getAdd(double a, double b) const { 无效Addition :: getAdd(double a,double b)const {
    std::cout << "Result is " << add(a,b)<< std::endl ; std :: cout <<“结果为” << add(a,b)<< std :: endl; } }

(or) (要么)

  1. store the result in a variable and print it. 将结果存储在变量中并打印。 There are two approaches to it 有两种方法

a. 一种。

 class Addition 
{
    public:
        Addition(double a=0, double b=0);
        double add(double a, double b);
        void getAdd() const;

    private:
        double mA;
        double mB;
        double add;
};

Addition::Addition(double a, double b) : mA(a), mB(b) {}

double Addition::add(double a, double b)
{
    add = a + b;

    return add;

}    

void Addition::getAdd() const
{   

    std::cout << "Result is " << add() << std::endl ;
} 

In this case, make sure to call add() first and then getAdd() 在这种情况下,请确保先调用add(),然后再调用getAdd()

sum.add(num_a,num_b);
sum.getAdd();

Else garbage value will be printed. 否则将打印其他垃圾值。

Another option is as follows 另一种选择如下

 class Addition 
{
    public:
        Addition(double a=0, double b=0);
        double add(double a, double b);
        void getAdd() const;

    private:
        double mA;
        double mB;
        double add;
};

Addition::Addition(double a, double b) : mA(a), mB(b) {}

double Addition::add(double a, double b)
{
    double result =  a + b;

    return result;

}    

void Addition::getAdd(double a, double b) const
{   
    add = add(a,b)
    std::cout << "Result is " << add << std::endl ;
} 

and finally call getAdd(a,b); 最后调用getAdd(a,b);

I don't understand the meaning of your methods, and your Addtion class. 我不了解您的方法和您的Addtion类的含义。 It's strange. 真奇怪。

Maybe your Addtion class is just a class have some methods without any members. 也许您的Addtion类只是一个具有一些没有任何成员的方法的类。 The double add(double a, double b) function is enough for you to get the sum of two double variables. double add(double a,double b)函数足以让您获得两个double变量的和。 The void getAdd(double a, double b) function may print some info about sum of two double variables. void getAdd(double a,double b)函数可能会打印一些有关两个double变量之和的信息。

class Addtion
{
public:
    static double add(double a, double b);
    static void getAdd(double a, double b);
};

Or, maybe you can just use namespace. 或者,也许您可​​以只使用名称空间。 Your really need is just some functions, not a class with members. 您真正需要的只是一些函数,而不是带有成员的类。

namespace myaddtion
{
    double add(double a, double b);
    void addinfo(double a, double b);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM