0

I don't know how to print a returned value of a custom object.

#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

class Person{
private:
    string name;
    int age; 
public:
    Person(){
    };

    Person(string name, int age){
       this->name = name; 
       this->age = age; 
    }

    string getName(){
        return name; 
    }
};

class listOfPeople{
private:
    vector <Person> myList;
public:
    void fillTheList(Person p){
        myList.push_back(p);
    }

    Person findPerson(string name){
        for(Person p : myList){
            if(p.getName() == name) {
                return p; // returns a person
            }
        }
        return {};
    }
};

int main(){
   Person p1("Vitalik Buterin", 30); 
   Person p2("Elon musk", 50); 
   listOfPeople L; 
   L.fillTheList(p1); 
   L.fillTheList(p2); 
   Person p = L.findPerson("Vitalik"); //  I don't know what to do here (I want to print Vitalik's information, the full name and age)
   return 0; 
}

I don't know how to print the informations of the returned value. I tried different things but can't get the right logic.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
ThisIsMe
  • 15
  • 4
  • Sorry to hear that you "don't know how to print the informations of the returned value", but your question is not clear. What does "print a returned value of a custom object" mean? – Sam Varshavchik Dec 08 '22 at 22:38
  • `name_of_person_object.name_of_member_to_access` – NathanOliver Dec 08 '22 at 22:39
  • @SamVarshavchik the function findPerson returns a person if he's found in the list. That person has name and age. I store the person in a variable in the main method and now I want to print the name and age of that person – ThisIsMe Dec 08 '22 at 22:41
  • related/dupe: https://stackoverflow.com/questions/2981836/how-can-i-use-cout-myclass – NathanOliver Dec 08 '22 at 22:51

3 Answers3

1

you just need

 std::cout << p.name << " is " << p.age <<'/n';
pm100
  • 48,078
  • 23
  • 82
  • 145
  • is this the only way to do it? what if I have a person with say 20 personal information like name, last name, age, job, salary ect... do I have to print them all like that? is there a way to automatically print the person's info? – ThisIsMe Dec 08 '22 at 22:49
  • @ThisIsMe there is no automatic way – pm100 Dec 08 '22 at 22:55
  • 1
    In C++ there is no built-in reflection mechanism for the object to inspect itself and determine what its members are let alone know how to print them all. – user4581301 Dec 08 '22 at 22:59
  • @user4581301 oh yeah I see – ThisIsMe Dec 08 '22 at 23:30
0

You can add a toString() function in the person class that returns a string with the object properties and then print it using:

// inside person
std::string toString() {
return this->name + " age: " + std::to_string(this->age);
}
std::cout << p.toString();

By adding this function to your class, every time you need to print the properties of person, just call toString().

You could also add implementation to the operator <<, you can search how.

nokla
  • 451
  • 1
  • 8
0

Boost.PFR a.k.a. "magic get" could be one option if you are willing to turn Person into an aggregate:

#include <iostream>
#include <string>

#include "boost/pfr.hpp"

struct Person {
    std::string name;
    int age;
};

int main() {
    Person p{"Foo Bar", 85};

    std::cout << boost::pfr::io(p) << '\n';
}

Output

{"Foo Bar", 85}

Demo

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108