我无法弄清楚它是如何工作的,代码非常复杂,因为它适用于我所在的编程类。当我通过手动操作时,我似乎无法获得程序的输出,它是下周我们最后的练习测试,我不会在测试中作弊,教授给了我们程序和输出,我只是不明白为什么那是输出..:
class FinalExam
{
private:
int This;
int That;
public:
FinalExam(int, int);
void One(int);
int Two(int);
};
FinalExam :: FinalExam(int A = 3, int B = 5)
{
This = A;
That = B;
}
void FinalExam :: One(int A)
{
This --;
That = A;
}
int FinalExam :: Two(int A) // Two gets the int 8
{
int Scrap;
Scrap = This + That - A; // 5 + 2 - 8 = -1????
return Scrap;
}
main()
{
FinalExam Block;
FinalExam Chunk(6, 7);
Block.One(2);
cout << Block.Two(3)
<< '\n'
<< Chunk.Two(8); //I get lost on this 8, It should go to "Two"
}
输出是:
1
5
我已经看了大约一个小时,我不明白。
答案 0 :(得分:3)
FinalExam Block; // Not passing any arguments to the constructor. In that case,
// default argument values are taken. So, This = 3, That = 5
Block.One (2); // This = 2; That = 2
// Because This is decremented and That is assigned the value
// passed to the method which is 2.
cout << Block.Two (3) ; // 2 + 2 - 3 = 1 which is returned and is printed.
同样尝试第二个。
答案 1 :(得分:1)
以下是逐行细分:
1 FinalExam Block;
使用具有默认值的构造函数,因此Block.This
= 3 ,Block.That
= 5 。
2 FinalExam Chunk(6, 7);
使用构造函数,指定值,因此Chunk.This
= 6 和Chunk.That
= 7 。
<小时/> 3
Block.One(2);
Block.This
(3 ==&gt; 2),并指定Block.That
= 2 (之前为5)。
4 Block.Two(3)
返回Block.This
+ Block.That
- 3 ==&gt; 2 + 2-3 ==&gt; 1,即输出。
5 Chunk.Two(8)
返回Chunk.This
+ Chunk.That
- 8 ==&gt; 6 + 7-8 ==&gt; 5,即输出。
<强> Q.E.D。输出为“1 \ n 5”
答案 2 :(得分:1)
解释行之间的评论中发生了什么。
int main(int, char**) {
FinalExam Block;
// At this point ..
// Block.This = 3;
// Block.That = 5
FinalExam Chunk(6, 7);
// Chunk.This = 6
// Chunk.That = 7
Block.One(2);
// Block.One decrement's This and assigns 2 to That so ..
// Block.This = 2
// Block.That = 2
std::cout << Block.Two(3)
// Block.Two(3) returns the result of this calculation
// This + That - 3
// This and That are both 2 at this point so..
// 2 + 2 - 3 == 1
// It returns 1 and prints out '1'
<< std::endl
<< Chunk.Two(8);
// Chunk's This and That are 6 and 7 respectively so ..
// cout << 6 + 7 - 8 == 5
}
答案 3 :(得分:0)
首先,您会注意到我们正在创建两个名为Block和Chunk的FinalExam对象。
块: 由于块没有通过参数传递任何值,它将采用来自此代码块的默认值:
FinalExam :: FinalExam(int A = 3, int B = 5)
{
This = A;
That = B;
}
所以这= 3而那= 5
影响Block的下一行是:
Block.One(2);
引用此代码块:
void FinalExam :: One(int A)
{
This --;
That = A;
}
so this = 2(This = This - 1或This = 3 - 1) 那个= 2(那个= A = 2(通过参数值传递)
影响Block的最后一行是:
cout << Block.Two(3)
引用此代码块:
int FinalExam :: Two(int A) // Two gets the int 8
{
int Scrap;
Scrap = This + That - A; // 5 + 2 - 8 = -1????
return Scrap;
}
所以我们创建一个名为Scrap = 1的新整数(This + That - A或2 + 2 - 3(按值传递))
组块: 引用Chunk的第一行是:
FinalExam Chunk(6, 7);
此集合A = 6且B = 7 并且因为这个代码块:
FinalExam :: FinalExam(int A = 3, int B = 5)
{
This = A;
That = B;
}
这= 6且那= 7(这= A = 6且那= B = 7)
最后,我们有这一行:
Chunk.Two(8);
引用此代码块:
int FinalExam :: Two(int A) // Two gets the int 8
{
int Scrap;
Scrap = This + That - A; // 5 + 2 - 8 = -1????
return Scrap;
}
A设置为8,因为我们通过参数传递值 和报废= 5(b + 7 - 8或此+那 - A)
输出: 我们从Block输出Scrap,即1 并创建一个新行 并从Chunk输出Scrap,即5
希望这会有所帮助,如果您有任何其他问题,请发表评论