structs

Ex. employee's characteristics/attributes: Convenient to store all information about an employee in one data "structure".
In C, must define/create a new type:
struct employee {   //name of this new type is employee
  string name;     //four members/fields
  int emp_num;
  int dept;
  float salary;
};
A struct can have any number of members, each of any type, each with a name.

struct definitions are typically global:

	includes
	...
	consts
	...
	struct type definitons
	...
	prototypes
	...
	function definitions
	...
Now employee is a type of the language, like int or float is.
Can declare variables of type employee, can have function arguments that are of type employee, can have functions whose return type is employee, can have arrays of type employee, can have another struct type with an employee struct member.
employee emp1, emp2;   //2 employee variables

The members of a struct variable are variables that can be processed like any other variables of that type.
Access a member of a struct with the . dot operator:

	struct_variable.member_name


emp1.emp_num = 37;
emp1.dept = 6;
emp1.salary = 30000;
emp1.name = "Smith";

if (emp1.dept == 8)
  cout << "Dept 8 employee" << emp1.name;

emp2.dept = emp1.dept; //assign value of an int variable to an int variable
cin >> emp2.name;      //name member is a string variable
cout << emp1.emp_num;  //int variable 

//cin and cout can not handle entire structs.  
//Can only input and output chars, strings, ints, floats.
cin >> emp1.name >> emp1.dept;
cout << emp1.salary;
Only operation on entire structs is assignment (struct to struct copy): All members of one struct copied to other struct:
emp2 = emp1;  //can assign structs of same type



-----------------------------------------
// structure for student data
struct student {
  string lastname;
  string firstname;
  int id;
  float gpa;
};

student s1, s2, s3;	//3 student struct variables
cout << "Enter student's last name: ";
cin >> s1.lastname;
cout << "Enter student's first name: ";
cin >> s1.firstname;
cout << "Enter student's id: ";
cin >> s1.id;
cout << "Enter student's GPA: ";
cin >> s1.gpa;

get_student_info(s2); //argument is student struct
get_student_info(s3); //needs to change actual arg, so reference arg


void get_student_info (student &stu) {      
 //reference arg since function is to change actual arg
  cout << "Enter student's last name: ";
  cin >> stu.lastname;
  cout << "Enter student's first name: ";
  cin >> stu.firstname;
  cout << "Enter student's id: ";
  cin >> stu.id;
  cout << "Enter student's GPA: ";
  cin >> stu.gpa;
}

Prototype:
void get_student_info (student &);


---------
Alternatively:
function that returns a student struct

student get_student_info () {      
  student stu;
  cout << "Enter student's last name: ";
  cin >> stu.lastname;
  cout << "Enter student's first name: ";
  cin >> stu.firstname;
  cout << "Enter student's id: ";
  cin >> stu.id;
  cout << "Enter student's GPA: ";
  cin >> stu.gpa;
  return stu;
}


Use it like this:
s1 = get_student_info();



-------------------------------
display_student(s1);
display_student(s2);

Output:
 12345   Smith,Bob     3.45
 43256   Freud,Sigmund 2.70


void display_student (student pupil) {
  cout << setw(6) << pupil.id << "\t"
       << pupil.lastname << "," 
       << pupil.firstname << "\t"
       << pupil.gpa << endl;
}


----------------------------------------




//structure for dates
struct date {
  int year;
  int month;
  int day;
};

date today, tomorrow, birthday, christmas;

birthday.year = 1980;
birthday.month = 4;
birthday.day = 30;

cout << "Enter today's date" << endl;
get_date(today);



void get_date (date &d) {
  cout << "Enter day: ";
  cin >> d.day;
  cout << "Enter month: ";
  cin >> d.month;
  cout << "Enter year: ";
  cin >> d.year;
}
//Or include error checking.


print_date(today);

Output:
 26 November 1994

You write the definition.
-----------------------------------


Function to compare two dates for ordering.  Similar to strcmp.

date my_birthday, your_birthday;

cout << "Enter first birthday";
get_date(my_birthday);
cout << "Enter second birthday";
get_date(your_birthday);

if (datecmp(my_birthday,your_birthday) == 0)
  cout << "Same age";
else if (datecmp(my_birthday,your_birthday) == -1)
  cout << "I'm older";
else
  cout << "You're older";


//Returns -1 if 1st is < (earlier than) 2nd,
// 1 if 2nd is < 1st, 0 if same
int datecmp (date d1, date d2) {
  if (d1.year < d2.year)
    return -1;
  else if (d2.year < d1.year)
    return 1;
  else  //years are same
    if (d1.month < d2.month)
      return -1;
    else if (d2.month < d1.month)
      return 1;
    else  //months are same too
      if (d1.day < d2.day)
        return -1;
      else if (d2.day < d1.day)
        return 1;
      else  //days are same too
        return 0;
}

Next (arrays of structs)


©David Wills