Home > Backend Development > C++ > ## Why is my temperature value being stored in the array instead of the intended variable in my C class?

## Why is my temperature value being stored in the array instead of the intended variable in my C class?

Barbara Streisand
Release: 2024-10-25 00:27:30
Original
1066 people have browsed it

## Why is my temperature value being stored in the array instead of the intended variable in my C   class?

Shadowing Variables in C

In object-oriented programming, shadowing occurs when a variable defined within a class has the same name as a variable in an outer scope. This can lead to unexpected behavior, as the inner variable takes precedence over the outer variable.

Problem: Shadowing in a Class

Consider the following class definition:

<code class="cpp">class Measure {
    int N;
    double measure_set[];
    char nomefile[];
    double T;

    public:
    void get( );
    void printall( );
    double mean( );
    double thermal_comp( );
};</code>
Copy after login

The get method in this class is intended to read values from a file and save them in the measure_set array, and to read a temperature value and store it in the T variable.

However, when you implement the get method as follows:

<code class="cpp">void Measure::get() {
    cout << "Insert filename:" << endl;
    cin >> nomefile;
    cout << endl;
    cout << nomefile << endl;
    cout << endl;

    int M = 0;
    int nmax = 50;

    ifstream f;
    f.open(nomefile);
    while (M < nmax) {
        f >> measure_set[M];
        if (f.eof()) break;
        M++;
    }
    f.close();
    N = M + 1;

    cout << "Insert temperature:" << endl;
    cin >> T;
    cout << endl;
}</code>
Copy after login

You noticed that the temperature value (T) is being stored in the first element of the measure_set array (measure_set[0]) instead of the intended T variable.

Solution

This occurs because C allows variables with the same name to be declared in different scopes. In this case, the T variable declared in the get method shadows the class member variable T.

To avoid shadowing, you can either use different names for the variables or use the scope resolution operator (::) to explicitly refer to the class member variable.

Using a different name for the temperature variable in the get method would look like this:

<code class="cpp">void Measure::get() {
    cout << "Insert filename:" << endl;
    cin >> nomefile;
    cout << endl;
    cout << nomefile << endl;
    cout << endl;

    int M = 0;
    int nmax = 50;

    ifstream f;
    f.open(nomefile);
    while (M < nmax) {
        f >> measure_set[M];
        if (f.eof()) break;
        M++;
    }
    f.close();
    N = M + 1;

    cout << "Insert temperature:" << endl;
    double temperature;  // Use a different name for the temperature variable
    cin >> temperature;
    T = temperature;
    cout << endl;
}</code>
Copy after login

Using the scope resolution operator to explicitly refer to the class member variable would look like this:

<code class="cpp">void Measure::get() {
    cout << "Insert filename:" << endl;
    cin >> nomefile;
    cout << endl;
    cout << nomefile << endl;
    cout << endl;

    int M = 0;
    int nmax = 50;

    ifstream f;
    f.open(nomefile);
    while (M < nmax) {
        f >> measure_set[M];
        if (f.eof()) break;
        M++;
    }
    f.close();
    N = M + 1;

    cout << "Insert temperature:" << endl;
    cin >> this->T;  // Use the scope resolution operator to refer to the class member variable
    cout << endl;
}</code>
Copy after login

The above is the detailed content of ## Why is my temperature value being stored in the array instead of the intended variable in my C class?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template