What is the Functionality of this C++ Code?

Click For Summary
SUMMARY

The C++ code discussed implements two classes, X and Y, where class Y contains a method to multiply a given input by a stored value, while class X accumulates results from class Y's method. The main function creates an instance of X and calls its method Xx in a loop with random values. The randomness of the output is emphasized, as the rand() function generates different results each execution unless seeded with srand(). Understanding this code requires tracing variable states and memory allocation throughout its execution.

PREREQUISITES
  • Understanding of C++ class structures and object-oriented programming
  • Familiarity with the rand() and srand() functions for random number generation
  • Knowledge of memory management and variable scope in C++
  • Ability to read and trace code execution line by line
NEXT STEPS
  • Learn about C++ memory management and variable lifetime
  • Explore the use of srand() for consistent random number generation in C++
  • Study object-oriented programming principles in C++ for better class design
  • Practice tracing code execution with various C++ examples to enhance debugging skills
USEFUL FOR

C++ developers, computer science students, and anyone looking to improve their understanding of object-oriented programming and code tracing techniques.

FallArk
Messages
127
Reaction score
0
The way the classes and functions are named confuses me. What is the program trying to do?
Code:
class Y {
long y; 
public:
Y() {
y = 4;
}
long get(long yy) { 
long ret = yy * y; 
y = yy;
return ret;
}
};
class X {
long x;
 Y y;
public:
X() {
x = 7;
}
void Xx(long xx) {
x += y.get(xx);
}
int get() {
return x;
}
};
int main() {
X q;
for (int z = 0; z < 11; z++) { 
q.Xx(rand()%93);
}
cout << q.get() << endl;

}

The first 20 random numbers are:

11527,4365,19738,9290,29090,29206,21427,28828,21650,14538,23366,
18453,32723,11594,31040,24829,11476,20054,28717,30531
 
Technology news on Phys.org
FallArk said:
The way the classes and functions are named confuses me.
That's the point. It's supposed to confuse you. The two main reasons that's done. First, so that you learn to name your variables/functions/classes intelligently so you can look at the code and have it be easily understandable. Second so that you learn to correctly trace code without relying on names to help you figure out what's going on, because when you look at someone elses code, or even your own from the past, it won't be guaranteed to be nicely written.

What is the program trying to do?
Code:
class Y {
long y; 
public:
Y() {
y = 4;
}
long get(long yy) { 
long ret = yy * y; 
y = yy;
return ret;
}
};
class X {
long x;
 Y y;
public:
X() {
x = 7;
}
void Xx(long xx) {
x += y.get(xx);
}
int get() {
return x;
}
};
int main() {
X q;
for (int z = 0; z < 11; z++) { 
q.Xx(rand()%93);
}
cout << q.get() << endl;

}

The first 20 random numbers are:

11527,4365,19738,9290,29090,29206,21427,28828,21650,14538,23366,
18453,32723,11594,31040,24829,11476,20054,28717,30531

Before dealing with anything else those are not the first twenty random numbers, the first 20 random numbers, as returned by rand() in the code will be different each and every time the program is run because they're (pseudo)random. If you wanted them to be the same every time the code was run, for testing purposes, then you'd need to have a call to srand() with some number as the parameter before the for loop (i.e. srand(10); ).

As for what the code is doing you'd need to start by looking at main and drawing a memory diagram for all the variables that get created. For example after the first line you'd have a variable named q of type X in your memory diagram, and since it's a class inside of it you'd have places for all the variables that X contains and if any of those are classes you repeat the process. Then follow the code line by line. What does the body of the for loop do? What kind of values get passed into the function? What happens to that value in the function?
 
Last edited:
squidsk said:
That's the point. It's supposed to confuse you. The two main reasons that's done. First, so that you learn to name your variables/functions/classes intelligently so you can look at the code and have it be easily understandable. Second so that you learn to correctly trace code without relying on names to help you figure out what's going on, because when you look at someone elses code, or even your own from the past, it won't be guaranteed to be nicely written.
Before dealing with anything else those are not the first twenty random numbers, the first 20 random numbers, as returned by rand() in the code will be different each and every time the program is run because they're (pseudo)random. If you wanted them to be the same every time the code was run, for testing purposes, then you'd need to have a call to srand() with some number as the parameter before the for loop (i.e. srand(10);).

As for what the code is doing you'd need to start by looking at main and drawing a memory diagram for all the variables that get created. For example after the first line you'd have a variable named q of type X in your memory diagram, and since it's a class inside of it you'd have places for all the variables that X contains and if any of those are classes you repeat the process. Then follow the code line by line. What does the body of the for loop do? What kind of values get passed into the function? What happens to that value in the function?

Okay, I guess I need to try to get use to it then.
Btw, the 20 random numbers are given, then we trace it. I'm going to try tracing it now:p
 
One question:
Does the program stores the previous values calculated from the for loop, or does it simply runs the loop once then discard the data and starts again?
 
FallArk said:
One question:
Does the program stores the previous values calculated from the for loop, or does it simply runs the loop once then discard the data and starts again?

Depends what you asking. If you asking if the values calculated in the loop are kept for the next time the program starts up then no. Each time you start a program it starts fresh. If you're asking if each iteration of the loop stores it values, then the answer is "it depends". It depends what code actually happens inside the loop as to what values if any from the code within the loop are kept for the next iteration.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
53
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
6K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 6 ·
Replies
6
Views
1K