Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Passing Custom Structures as Arguments | Function Arguments Specification
C++ Functions

bookChallenge: Passing Custom Structures as Arguments

We can also pass structures (classes) as arguments of the function. We can do it the same as passing simple data type by using structure (class) name as a type specifier inside the function signature:

Like simple data types, we can pass custom structures (classes) by value, pointer, or reference.

passByValue.cpp

passByValue.cpp

passByPointer.cpp

passByPointer.cpp

passByReference.cpp

passByReference.cpp

copy
1234567891011121314151617181920212223
#include <iostream> #include <string> // Define a custom structure struct Person { std::string name; int age; }; // Function that takes a Person structure by value void displayPersonInfo(Person p) { std::cout << "Name: " << p.name << ", Age: " << p.age << std::endl; } int main() { // Create a Person structure Person person1 = {"Alice", 30}; // Call the function and pass the Person structure by value displayPersonInfo(person1); }

Note

There is another more comfortable way to access the attribute of the structure via pointer: we can use the -> operator instead of (*).. For example:

  • p->name instead of (*p).name;
  • p->age instead of (*p).age
Oppgave

Swipe to start coding

You have a student with grades in 3 subjects. Your task is to calculate the student's average grade.

  1. The function calculateAverageGrade takes a Student structure by reference, representing the student's name and grades in Math, Physics, and Chemistry.
  2. Inside calculateAverageGrade, add the three subject scores and assign the result to the variable averageScore (mathScore, physicsScore, chemistryScore).
  3. Divide averageScore by 3.0 to get the average grade as a double.
  4. Determine the overall performance:
    • If averageScore >= 90, print "Overall Performance: Excellent".
    • If averageScore >= 70, print "Overall Performance: Good".
    • Otherwise, print "Overall Performance: Needs Improvement".

Løsning

solution.cpp

solution.cpp

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 7
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain how passing by value, pointer, and reference works with structures?

Can you provide an example of a function that takes a structure as an argument?

What is the difference between using the `->` operator and the `.` operator with structures?

close

Awesome!

Completion rate improved to 5

bookChallenge: Passing Custom Structures as Arguments

Sveip for å vise menyen

We can also pass structures (classes) as arguments of the function. We can do it the same as passing simple data type by using structure (class) name as a type specifier inside the function signature:

Like simple data types, we can pass custom structures (classes) by value, pointer, or reference.

passByValue.cpp

passByValue.cpp

passByPointer.cpp

passByPointer.cpp

passByReference.cpp

passByReference.cpp

copy
1234567891011121314151617181920212223
#include <iostream> #include <string> // Define a custom structure struct Person { std::string name; int age; }; // Function that takes a Person structure by value void displayPersonInfo(Person p) { std::cout << "Name: " << p.name << ", Age: " << p.age << std::endl; } int main() { // Create a Person structure Person person1 = {"Alice", 30}; // Call the function and pass the Person structure by value displayPersonInfo(person1); }

Note

There is another more comfortable way to access the attribute of the structure via pointer: we can use the -> operator instead of (*).. For example:

  • p->name instead of (*p).name;
  • p->age instead of (*p).age
Oppgave

Swipe to start coding

You have a student with grades in 3 subjects. Your task is to calculate the student's average grade.

  1. The function calculateAverageGrade takes a Student structure by reference, representing the student's name and grades in Math, Physics, and Chemistry.
  2. Inside calculateAverageGrade, add the three subject scores and assign the result to the variable averageScore (mathScore, physicsScore, chemistryScore).
  3. Divide averageScore by 3.0 to get the average grade as a double.
  4. Determine the overall performance:
    • If averageScore >= 90, print "Overall Performance: Excellent".
    • If averageScore >= 70, print "Overall Performance: Good".
    • Otherwise, print "Overall Performance: Needs Improvement".

Løsning

solution.cpp

solution.cpp

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 7
single

single

some-alt