Reputation: 337
I can't figure out how this works, the code is really complicated because it is for a programming class I'm in. I can't seem to get the program's output when I work it through manually, it is the practice test for our final next week, I wouldn't cheat on a test, the professor gave us the program and the output, I just don't understand why that is the output..:
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"
}
And the output is:
1
5
I have looked at this for about an hour and I don't understand.
Upvotes: 0
Views: 162
Reputation: 64682
Here's a line-by-line breakdown:
1 FinalExam Block;
Uses the constructor with default values, so Block.This
= 3, and Block.That
= 5.
2 FinalExam Chunk(6, 7);
Uses the constructor, specifying values, so Chunk.This
= 6 and Chunk.That
= 7.
Block.One(2);
Block.This
(3 ==> 2), and assigns Block.That
= 2 (was previously 5).
4 Block.Two(3)
returns Block.This
+ Block.That
-3 ==> 2+2-3 ==> 1, which is output.
5 Chunk.Two(8)
returns Chunk.This
+ Chunk.That
- 8 ==> 6+7-8 ==> 5, which is output.
Q.E.D. the output is "1 \n 5"
Upvotes: 1
Reputation: 2761
Firstly, you'll notice that we're creating two objects of type FinalExam called Block and Chunk.
Block: since block wasn't passed any values via the parameter, it will take on the default values which come from this code block:
FinalExam :: FinalExam(int A = 3, int B = 5)
{
This = A;
That = B;
}
so This = 3 and That = 5
The next line that effects Block is:
Block.One(2);
which refers to this code block:
void FinalExam :: One(int A)
{
This --;
That = A;
}
so This = 2 (This = This - 1 or This = 3 - 1) and That = 2 (That = A = 2(passed by value in parameters)
The last line effecting Block is:
cout << Block.Two(3)
which refers to this code block:
int FinalExam :: Two(int A) // Two gets the int 8
{
int Scrap;
Scrap = This + That - A; // 5 + 2 - 8 = -1????
return Scrap;
}
so we create a new integer called Scrap = 1 (This + That - A or 2 + 2 - 3(passed by value))
Chunk: the first line that refers to Chunk is:
FinalExam Chunk(6, 7);
this set A = 6 and B = 7 and because of this code block:
FinalExam :: FinalExam(int A = 3, int B = 5)
{
This = A;
That = B;
}
This = 6 and That = 7 (This = A = 6 and That = B = 7)
and finally, we have this line:
Chunk.Two(8);
which refers to this code block:
int FinalExam :: Two(int A) // Two gets the int 8
{
int Scrap;
Scrap = This + That - A; // 5 + 2 - 8 = -1????
return Scrap;
}
A is set to 8 since we passed it by value through parameters and Scrap = 5 (b + 7 - 8 or This + That - A)
Output: We output Scrap from Block which is 1 and create a new line and output Scrap from Chunk which is 5
Hope this helps, leave a comment if you have any additional questions
Upvotes: 0
Reputation: 7725
Explanation of what's happening in the comments between the lines.
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
}
Upvotes: 1
Reputation: 34625
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.
Similarly try the second one.
Upvotes: 3