Return Custom Data Types
You can return custom structures and classes from functions. When you return an instance of a struct or class from a function, you essentially return a copy of the object ( the same as returning simple data types).
This allows you to encapsulate related data and behavior within a single structure or class instance, pass it around between functions, or use it in different parts of your program.
To return a structure/class, you must use the structure/class name as a type specifier in the function signature.
main.cpp
1234567891011121314151617181920212223#include <iostream> // Define a custom structure called Person struct Person { std::string name; int age; }; // Function that returns a Person object Person create_person(const std::string name, const int age) { return Person { name, age }; } int main() { // Call the function to create a Person object Person person = create_person("Alice", 30); // Access and print the attributes of the returned Person object std::cout << "Name: " << person.name << std::endl; std::cout << "Age: " << person.age << std::endl; }
This code defines a custom structure Person
with two fields: name
and age
. The create_person()
function creates and returns a Person
object initialized with the given values. In main()
, the function is called to create a Person
instance, and the object’s details are printed to the console.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Can you show me an example code for returning a struct or class from a function?
What happens if I modify the returned object later?
Can you explain the difference between returning by value and by reference?
Awesome!
Completion rate improved to 5
Return Custom Data Types
Desliza para mostrar el menú
You can return custom structures and classes from functions. When you return an instance of a struct or class from a function, you essentially return a copy of the object ( the same as returning simple data types).
This allows you to encapsulate related data and behavior within a single structure or class instance, pass it around between functions, or use it in different parts of your program.
To return a structure/class, you must use the structure/class name as a type specifier in the function signature.
main.cpp
1234567891011121314151617181920212223#include <iostream> // Define a custom structure called Person struct Person { std::string name; int age; }; // Function that returns a Person object Person create_person(const std::string name, const int age) { return Person { name, age }; } int main() { // Call the function to create a Person object Person person = create_person("Alice", 30); // Access and print the attributes of the returned Person object std::cout << "Name: " << person.name << std::endl; std::cout << "Age: " << person.age << std::endl; }
This code defines a custom structure Person
with two fields: name
and age
. The create_person()
function creates and returns a Person
object initialized with the given values. In main()
, the function is called to create a Person
instance, and the object’s details are printed to the console.
¡Gracias por tus comentarios!