mardi 26 mai 2020

How to set default parameters In case a user does not type in the other one

#include <iostream>

class Person {
    friend std::ostream &operator<<(std::ostream &os, const Person &person);
    std::string name;
    int age;
public:
    std::string get_name () {return name;}
    int get_age() {return age;}
    Person() : name{"Unknown"}, age{0} {}
    Person(std::string name, int age) : name{name}, age{age} {}
};

std::ostream &operator<<(std::ostream &os, const Person &person) {
    os << person.name << ": " << person.age;
    return os;
}

int main () {
    Person Austin{"Austin", 12}; 
}

I am trying to have it where for my name I type in "Austin", but If I don't type in age it will automatically make it a 0 because I did not enter a age.

Aucun commentaire:

Enregistrer un commentaire