/* This program demonstrates * 1. An introduction to structures * * 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. */ #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]; }; // If a function uses a structure, it has to be declared after the struct declaration void printDate(Date d); Date readDate(); int main() { /* To declare a structure variable, just use the Structure name as a data type. * Just like an array can be initialized with set notation, we can initialize * structures with set notation as well. Any composite types in the structure * (like arrays, other structures) need their own set of {} */ Date today = {11, 15, 2021, "Monday"}; /* To access an internal element of the structure variable, we use the dot (.) operator */ cout<<"Today is "<< today.dayOfWeek<<", "<< today.month<<"/"<< today.day <<"/"<< today.year<>var.month>> junk>> var.day>>junk>>var.year; return var; }