So then my guess would be that each time I create an object of type person, the constructor will store the value in each object that I create? So if I do p1, p2, p3 it would store the information in each instance of the object?
Ideally do not write a constructor for a struct like person in its present form; it is needlessly and tiresomely verbose. Instead use uniform initialisation - that is: initialise the members with a braced-init-list.
Write a constructor only after you have identified the class invariants (for example: age must be non-negative).
Then make sure that the constructor establishes the invariants, and use access specifiers to prevent unrestricted access to the member variables.
#include <iostream>
#include <string>
struct person
{
std::string name ;
int age ;
int height_in_inches ;
};
// return an person object initialised with input from stdin
// note: the function does not have any input parameters
person make_person();
int main()
{
const person user = make_person();
std::cout << "Your name is " << user.name << '\n' ;
std::cout << "Your age is " << user.age << '\n' ;
std::cout << "Your heigh is " << user.height_in_inches << " in." << '\n' ;
}
person make_person()
{
std::cout << "Please enter your name: ";
std::string name ;
std::cin >> name; // assumes that the name does not have spaces in it
std::cout << "Please enter your age: ";
int age ;
std::cin >> age;
std::cout << "Please enter your height in inches: ";
int height ;
std::cin >> height ;
// perhaps validate input: eg, verify that age is not negative
// return a person object initialised with these three values
returnperson { name, age, height } ;
}