我想在参数中添加两个数字。 [英] I wanna add two numbers in parameters.

查看:93
本文介绍了我想在参数中添加两个数字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如::

#include< iostream.h>

class ex

{

public :

ex()

{

a = 0;

b = 0;

}

ex(

..

...

}

};

void main()

{

ex e1(3,5),e2(5,7),e3;

e3 = e1.add(e2);

}



答案应为8 q2 ..

e1的x应添加到e2的x中.... e1的y应添加到e2的y ...

谢谢... asa p。 。



我的尝试:



我试过但我没有尝试

For example::
#include<iostream.h>
class ex
{
public:
ex()
{
a=0;
b=0;
}
ex(
..
...
}
};
void main()
{
ex e1(3,5), e2(5,7),e3;
e3=e1.add(e2);
}

The answer should be 8 q2..
The x of e1 should be added to x of e2....and y of e1 should be added to y of e2...
Thanks ... a s a p..

What I have tried:

I have tried but I didn't got it

推荐答案

e3=e1.add(e2);

看到这个陈述并且知道所有这些都适用于 ex 类,函数原型必须是

Seeing this statement and knowing that all applies to the class ex, the function prototype must be

ex add(const ex& e1) const;

和corr相应的实现是

and the corresponding implementation is

ex ex::add(const ex& e1) const
{
    ex result;
    result.a = this->a + e1.a;
    result.b = this->b + e1.b;
    return result;
}

或更短的使用构造函数(并放在类定义中)

or shorter using the constructor (and to be placed in the class definition)

ex add(const ex& e1) const
{
    return ex(this->a + e1.a, this->b + e1.b);
}


这篇关于我想在参数中添加两个数字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆