Challenge: 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
passByPointer.cpp
passByReference.cpp
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->nameinstead of(*p).name;p->ageinstead of(*p).age
Swipe to start coding
You have a student with grades in 3 subjects. Your task is to calculate the student's average grade.
- The function
calculateAverageGradetakes aStudentstructure by reference, representing the student's name and grades in Math, Physics, and Chemistry. - Inside
calculateAverageGrade, add the three subject scores and assign the result to the variableaverageScore(mathScore,physicsScore,chemistryScore). - Divide
averageScoreby 3.0 to get the average grade as adouble. - Determine the overall performance:
- If
averageScore >= 90, print"Overall Performance: Excellent". - If
averageScore >= 70, print"Overall Performance: Good". - Otherwise, print
"Overall Performance: Needs Improvement".
- If
Solution
solution.cpp
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 5
Challenge: Passing Custom Structures as Arguments
Swipe to show menu
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
passByPointer.cpp
passByReference.cpp
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->nameinstead of(*p).name;p->ageinstead of(*p).age
Swipe to start coding
You have a student with grades in 3 subjects. Your task is to calculate the student's average grade.
- The function
calculateAverageGradetakes aStudentstructure by reference, representing the student's name and grades in Math, Physics, and Chemistry. - Inside
calculateAverageGrade, add the three subject scores and assign the result to the variableaverageScore(mathScore,physicsScore,chemistryScore). - Divide
averageScoreby 3.0 to get the average grade as adouble. - Determine the overall performance:
- If
averageScore >= 90, print"Overall Performance: Excellent". - If
averageScore >= 70, print"Overall Performance: Good". - Otherwise, print
"Overall Performance: Needs Improvement".
- If
Solution
solution.cpp
Thanks for your feedback!
single