Comprehensive Study Notes on C++ Structs and Data Management

Comparison of Data Types: Array vs. Struct

  • Arrays are used for variables containing data that share the same data type. An example provided is int marks[10]int\,marks[10], which stores 10 integer values.

  • Structs (Structures) are used for variables that encompass different data types within a single entity. Examples include grouping int creditint\,credit and float cgpafloat\,cgpa together.

  • Memory Allocation and Declaration:

    • For standard types like intint, floatfloat, and charchar, as well as arrays, memory is allocated in the computer during declaration.
    • Example: int mark;int\,mark; allocates a single integer space.
    • Example: int mark[2];int\,mark[2]; allocates two adjacent spaces labeled mark[0]mark[0] and mark[1]mark[1].

Struct Definition and Declaration Protocols

  • To allocate memory for a struct, both a definition and a declaration are required.

  • Struct Definition Syntax:

    • A struct is defined with the keyword structstruct followed by a name (e.g., CourseInfoCourseInfo).
    • The structural members are placed inside curly braces.
    • Example:     struct CourseInfo {… };struct\,CourseInfo\,\{\dots\};
  • Defining the CourseInfoCourseInfo Structure:

    • Members include:
    • string namestring\,name
    • string codestring\,code
    • int creditint\,credit
  • Struct Declaration:

    • Once defined, you can declare instances of that struct.
    • Example: CourseInfo Course1;CourseInfo\,Course1; and CourseInfo Course2;CourseInfo\,Course2;
  • Combined Definition and Declaration Syntax:

    • A struct can be defined and declared simultaneously by placing variable names after the closing brace but before the semicolon.
    • Example:     struct { string name; string code; int credit; } Course1, Course2;struct\,\{\,string\,name;\,string\,code;\,int\,credit;\,\}\,Course1,\,Course2;

Initialization and Member Access

  • Initialization of a Struct:

    • Data can be assigned to a struct using curly braces in chronological order of the definition.
    • Example: CourseInfo Course1={"Structured Programming", "MTS3013", 3};CourseInfo\,Course1 = \{"Structured\,Programming",\,"MTS3013",\,3\};
  • Member Access Operators:

    • Arrays use indexes or subscripts (e.g., [i][i]).
    • Structs use the Dot Operator or Member Access Operator (..).
    • Example: Course1.nameCourse1.name refers to the name member of the Course1Course1 instance.

Core Operations on Structs

  • Assignment (Input Data):

    • Values can be assigned directly to specific members.
    • Example:
    • Course1.name="Data Struct";Course1.name = "Data\,Struct";
    • Course1.code="MTS3023";Course1.code = "MTS3023";
    • Course1.credit=3;Course1.credit = 3;
  • Standard Input (cincin):

    • Members can be populated via user input.
    • Example:
    • cin>>Course1.name;cin >> Course1.name;
    • cin>>Course1.code;cin >> Course1.code;
    • cin>>Course1.credit;cin >> Course1.credit;
  • Standard Output (coutcout):

    • Members are displayed using the dot operator.
    • Example:
    • cout<<Course1.name;cout << Course1.name;
    • cout<<Course1.code;cout << Course1.code;
    • cout<<Course1.credit;cout << Course1.credit;

Dynamic Variables and Memory Management

  • Dynamic variables are constructed during the execution of a program (a/ca/c).

  • newnew Keyword:

    • Used to build a new temporary variable by allocating a memory location indicated by a pointer.
  • deletedelete Keyword:

    • Used to destroy a previously built dynamic variable by removing that specific memory location.

Practical Exercises and Scenario-Based Coding

  • Question 1: Car Information Management

    • Structure Definition:
    • struct car { char no_plat[9]; char name[40]; int year; double price; };struct\,car\,\{\,char\,no\_plat[9];\,char\,name[40];\,int\,year;\,double\,price;\,\};
    • Task (i): Declare a single instance named car1car1.
    • Solution: car car1;car\,car1;
    • Task (ii): Declare an array named listlist for 20 cars.
    • Solution: car list[20];car\,list[20];
    • Task (iii): Write input statements for both.
    • For car1car1: Uses standard cincin flow for all four members.
    • For listlist: Uses a forfor loop where xx ranges from 00 to 1919, accessing list[x].member namelist[x].member\,name.
  • Question 2: Camera Database Application

    • Structure Definition: Includes cameraMakecameraMake, cameraModelcameraModel, zoomLevelzoomLevel, megaPixelsmegaPixels, and priceprice.
    • Task (a): Declare an array for 25 cameras.
    • Solution: Camera Camera1[25];Camera\,Camera1[25];
    • Task (b): Segment for inputting data.
    • Uses a loop with variable yy from 00 to 2424 to input data for each cataloged camera.
    • Task (c): Filter display based on price.
    • Logic: if (Camera1[y].price>500) { cout<<Camera1[y].cameraModel; }if\,(Camera1[y].price > 500)\,\{\,cout << Camera1[y].cameraModel;\,\}
  • Question 3: Info_Kad Debit Card Records

    • Structure Definition: string namestring\,name, int no_kadint\,no\_kad, int yearint\,year.
    • Task (a): Declare kad_debitkad\_debit for 10 customers.
    • Solution: Info_Kad kad_debit[10];Info\_Kad\,kad\_debit[10];
    • Task (b): Main function for input/output.
    • Utilizes two separate forfor loops (typically using index ii) to carry out sequential input and output for all 10 records.
  • Question 4: Staff Personal Records

    • Structure Definition (Personal_RekodPersonal\_Rekod):
    • string namaTerakhirstring\,namaTerakhir (Last Name)
    • string namaPertamastring\,namaPertama (First Name)
    • int umurint\,umur (Age)
    • Task (a): Declaration for 10 staffs.
    • Solution: Personal_Rekod staf[10];Personal\_Rekod\,staf[10];
    • Task (b): Implement main function to manage academic staff.
    • Solution includes prompts: "Masukkan nama Pertama", "Masukkan nama Terakhir", and "Masukkan umur".
    • The output combines both names and the age in one line: cout<<ahli_akademik[i].namaPertama<<""<<ahli_akademik[i].namaTerakhir<<""<<ahli_akademik[i].umur;cout << ahli\_akademik[i].namaPertama << " " << ahli\_akademik[i].namaTerakhir << " " << ahli\_akademik[i].umur;