I would like to ask you how to change the structure pointer value to the structure reference function value.

Asked 2 years ago, Updated 2 years ago, 22 views

void PrintOneData(Student* stu)
{
    cout << "Name: " << stu-> stdName << "Academic number: " << stu-> hakbun << endl;
    cout.width(10); cout << "========================================================" << endl;
    cout.width (20); cout << "subject name";
    cout.width (10); cout << "subject credit";
    cout.width (10); cout << "subject grade";
    cout.width (10); cout << "subject rating" << endl;
    cout.width(10); cout << "========================================================" << endl;
    for (int j = 0; j < 3; j++)
    {
        cout.width(20); cout << stu->sub[j].subName;
        cout.width(7); cout << stu->sub[j].hakjum;
        cout.width(10); cout << stu->sub[j].grade;
        cout.width(11); cout << stu->sub[j].gpa << endl;
    }
    cout << "========================================================" << endl;
    stu->ave_gpa = (stu->sub[0].gpa + stu->sub[1].gpa + stu->sub[2].gpa) / 3;
    cout.width (46); cout << "Average rating: " << stu->ave_gpa << endl;
    cout << endl << endl;
}

They want to modify the code above, which source should I add more?

void PrintOneData(const Student& stu)
{
    cout << "Name: " << stu-> stdName << "Academic number: " << stu-> hakbun << endl;
    cout.width(10); cout << "========================================================" << endl;
    cout.width (20); cout << "subject name";
    cout.width (10); cout << "subject credit";
    cout.width (10); cout << "subject grade";
    cout.width (10); cout << "subject rating" << endl;
    cout.width(10); cout << "========================================================" << endl;
    for (int j = 0; j < 3; j++)
    {
        cout.width(20); cout << stu->sub[j].subName;
        cout.width(7); cout << stu->sub[j].hakjum;
        cout.width(10); cout << stu->sub[j].grade;
        cout.width(11); cout << stu->sub[j].gpa << endl;
    }
    cout << "========================================================" << endl;
    stu->ave_gpa = (stu->sub[0].gpa + stu->sub[1].gpa + stu->sub[2].gpa) / 3;
    cout.width (46); cout << "Average rating: " << stu->ave_gpa << endl;
    cout << endl << endl;
}

c++

2022-09-21 11:08

1 Answers

In C++, the member access operators are . and ->. Of these, -> is used in pointers, and . is used in addition.

For example:

std::string* pValue = new std::string();
pValue->size();

std::string value;
value.size();

std::string& refvalue = *pValue;
refvalue.size();

pValue is a pointer. The member of the object that the pointer points to uses -> as mentioned earlier.

value is a general variable. Use . because it is not a pointer.

refvalue is a reference. Use . because it is not a pointer.

Looking at the code in the question, stu has changed from pointer to reference. Accordingly, stu->sub is stu.Changes are required, such as sub.


2022-09-21 11:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.