What is the Functionality of this C++ Code?

In summary, the program is using confusingly named classes and functions to teach the user to name their variables intelligently and to learn how to correctly trace code without relying on names. The code is also using a for loop to pass in random numbers and manipulate them in the functions, ultimately outputting the final result. The program does not store the previous values calculated from the loop.
  • #1
FallArk
127
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
  • #2
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:
  • #3
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
 
  • #4
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?
 
  • #5
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.
 

Related to What is the Functionality of this C++ Code?

1. What is C++ code interpretation?

C++ code interpretation refers to the process of translating human-readable code written in the C++ programming language into machine-readable instructions that a computer can understand and execute. This is done by a compiler, which converts the code into a lower-level language that the computer can then execute.

2. How does C++ code interpretation differ from C++ code compilation?

C++ code interpretation involves converting the code into machine-readable instructions at runtime, while C++ code compilation involves converting the code into machine-readable instructions before it is executed. This means that interpreted code can be executed immediately, while compiled code must be compiled and linked before it can be run.

3. What are the advantages and disadvantages of C++ code interpretation?

Some advantages of C++ code interpretation include faster development time and easier debugging, as the code can be executed and tested immediately after being written. However, interpreted code may run slower than compiled code, as it requires additional processing each time it is executed. Additionally, interpreted code may be less portable, as it may require a specific interpreter or runtime environment to run on different systems.

4. Can C++ code be both interpreted and compiled?

Yes, C++ code can be both interpreted and compiled. This is known as hybrid or mixed mode, where some parts of the code are interpreted at runtime and others are compiled beforehand. This allows for a balance between development speed and execution speed.

5. What are some popular C++ interpreters?

Some popular C++ interpreters include Cling, Ch interpreter, and ChaiScript. These interpreters allow for interactive execution of C++ code and can be useful for prototyping and testing code. However, it is important to note that these interpreters may have limitations and may not support all features of the C++ language.

Similar threads

  • Programming and Computer Science
2
Replies
53
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
704
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
1
Views
777
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top