Understanding structs

closed account (4wvoLyTq)
Just trying to grasp my head around structs and storing information. Would this be an effective way to store information utilizing a struct?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>

struct person
{
	std::string name;
	int age;
	int heigh_in_inches;
};

person employee(person emp);

int main()
{
	person p1;

	p1 = employee(p1);

	std::cout << "Your name is " << p1.name << std::endl;
	std::cout << "Your age is " << p1.age << std::endl;
	std::cout << "Your heigh is " << p1.heigh_in_inches << " in." << std::endl;

	system("pause");

	return 0;
}

person employee(person emp)
{
	
	std::cout << "Please enter your name: ";
	std::cin >> emp.name;
	std::cout << "Please enter your age: ";
	std::cin >> emp.age;
	std::cout << "Please enter your heigh in inches: ";
	std::cin >> emp.heigh_in_inches;

	return emp;
}
Would this be an effective way to store information utilizing a struct?
almost there but since you're populating the data-members of person you could also use the person ctor instead of a separate employee() function
closed account (4wvoLyTq)
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.

Something like this, perhaps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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
	return person { name, age, height } ;
}
Topic archived. No new replies allowed.