/* This program demonstrates * 1. An introduction to structures * 2. Nested Structs * 3. Dynamic Struct variables * 4. Arrays of Structs * * A structure is a way to create a composite data type. * So far, we have been working with atomic/primitive data types that can only * hold one kind of data. structures allow us to bundle up several data elements * into one unit, while still allowing us to access the individual elements. * A structure can be used as a regular data type. * * We can create variables of the structure type, pass it to functions and * return it from functions * With respect to functions, struct variables pass by value. * * This program also demonstrates advanced struct concepts * 1. Structs can contain arrays as data elements * 2. Structs can be nested - ie structs can contain other struct variables */ #include #include #include using namespace std; /* To create a structure, we use the keyword "struct" followed by the name of the * structure (we choose this). We then open a code block and declare all the variables * for the elements of the structure. * We only declare them. Structure elements should NOT be initialized (given values) * in the structure declaration unless they are consts. * the structure declaration ends with a semicolon. * The following line declares a Date structure with 4 data members */ struct Date { int month, day, year; char dayOfWeek[15]; }; /* Student structure * name - string * major - string * GPA - float * EMPL - long * grades - array of 3 floats * datOfBirth - Date struct variable */ struct Student { string name, major; float gpa; long empl; float grades[3]; Date dateOfBirth; }; // If a function uses a structure, it has to be declared after the struct declaration void printDate(Date d); Date readDate(); void printStudent(Student s); void readStudent( Student &s); int main() { //Initialize the structure variable by individually initializing the data elements Student s; s.name = "Gary"; s.empl = 25465; s.gpa = 3.8; s.major = "meow"; printStudent(s); // will print junk for uninitialized parts /* We can also initialize using set notaion, but each internal composite * data memeber needs its own {} */ Student t = {"Spongebob", "Boating", 2.67, 389482, {75,78,91}, {7, 14, 1987, "Friday"}}; printStudent(t); //this function uses a Student variable as a reference variable Student s1; readStudent(s1); cout<<"Student Details:\n"; printStudent(s1); /* We can use the new keyword to create a dynamic struct variable */ Student *st1 = new Student; // We can use the same functions, but we would need to dereference to match // the function signature. readStudent(*st1); cout<<"Student Details:\n"; printStudent(*st1); /* The Arrow Operator: We use the dot operator to access the data element of an struct * variable. * If we have a pointer to a struct variable instead, we need to use the arrow operator (->), * as shown below: */ cout<name <<" is "<< 2021 - st1->dateOfBirth.year << " years old"<>n; for(int i=0; i>var.month>> junk>> var.day>>junk>>var.year; return var; } /* This function accepts a structure variable and prints out the details in the structure * We use the dot (.) operator to access individual elements. */ void printStudent( Student s) { cout<>s.gpa; cout<<"EMPLID: "; cin>>s.empl; cout<<"3 grades: "; for(int i=0; i<3;i++) cin>>s.grades[i]; cout<<"Date of birth (Day of Week, m/d/y): "; cin.getline(s.dateOfBirth.dayOfWeek, 15, ','); cin>>s.dateOfBirth.month; cin.ignore(); cin>>s.dateOfBirth.day; cin.ignore(); cin>>s.dateOfBirth.year; }