Chapter 2 AAU
Chapter 2 AAU
Chapter Two
C++ Structure
structure type.
• The type itself has a name, just like ‘int’, ‘double’, or ‘char’ but it is
defined by the user and follows the normal rules of identifiers.
• Once the type has been defined through the C++ ‘struct’ keyword, you
can create variables from it just like you would any other type.
• You can refer to a structure as a single variable, and you also can initialize,
read and change the parts of a structure (the individual variables that make it
up).
• Within the structure block you declare all the member variables you want
associated with that type.
• Declare them as you would normal variables, but do not try to initialize them.
• This is simply a data blue print, it is not logic or instructions and the compiler
does not execute it.
• If we need two variables to have the above structure property then the declaration would be:
• The structure tag Inventory informs C++ that the tag called Inventory looks like two character
arrays followed by one integer and one float variables.
• A structure tag is actually a newly defined data type that you, the programmer, defined.
2024-12-25 Kassahun @2024 9
Referencing members of a structure
▪ To refer to the members of a structure we need to use the dot operator (.)
▪ The General syntax to access members of a structure variable would be:
VarName.Member
▪ Where VarName is the variable name of the structure variable And Member is
variable name of the members of the structure.
Syntax: For the above student structure:
struct Student Stud; //declaring Stud to have the property of the Student structure
strcpy(Stud.FName,”Abebe”); //assigned Abebe as First Name
Stud.CGPA=3.21; //assignes 3.21 as CGPA value of Abebe
cout<<Stud.FName; //display the name
cout<<Stud.CGPA; // display the CGPA of Abebe
2024-12-25 Kassahun @2024 10
Initialization of Structure
• To initialize a structure variable’s members, you follow the original
declaration with the assignment operator (=).
• These values are assigned to member variables in the order that they
occur.
2024-12-25 Kassahun @2024 11
Initializing Structure Data
• You can initialize members when you declare a structure, or you can
initialize a structure in the body of the program. Here is a complete
program.
struct cd_collection cout<<"\nhere is the info about cd1"<<endl;
{ cout<<cd1.title<<endl;
char title[25]; cout<<cd1.artist<<endl;
char artist[20];
cout<<cd1.num_songs<<endl;
int num_songs;
cout<<cd1.price<<endl;
float price;
char date_purchased[9]; cout<<cd1.date_purchased<<endl;
} cd1 = {"Red Moon Men","Sams and the Sneeds",
12, 11.95,"08/13/93"};
2024-12-25 Kassahun @2024 12
Initializing cont.…
• A better approach to initialize structures is to use the dot operator(.). the dot
operator is one way to initialize individual members of a structure variable in the
body of your program.
The syntax of the dot operator is :
structureVariableName.memberName
#include<iostream> //initialize members here //print the data
#include<cstring>
strcpy(cd1.title,"computer science"); cout<<"\nHere is the info"<<endl;
using namespace std;
int main() strcpy(cd1.artist,"Programmer"); cout<<"Title : "<<cd1.title<<endl;
{
cd1.num_songs= 12; cout<<"Artist : "<<cd1.artist<<endl;
struct cd_collection{
char title[25]; cd1.price = 11.95f; cout<<"Songs : "<<cd1.num_songs<<endl;
char artist[20];
strcpy(cd1.date_purchased,"22/12/02"); cout<<"Price : "<<cd1.price<<endl;
int num_songs;
float price; cout<<"Date purchased “ <<
char date_purchased[9];
cd1.date_purchased<<endl;
} cd1;
2024-12-25 Kassahun @2024 } 13
Accessing members of a structure variable
• You can use a member variable in any place you’d use a normal variable, but
you must specify it by the structure variable’s name as well as the member
variable’s name using the member operator.
• To specify that you want a member of a specific structure variable, you use the
structure member operator which is the period (also known as a “dot”).
• Simply use the structure’s name, follow with the period, and end with the
member: structure.member
Struct student{
String id,name;
} s1;
cin>>s1.id; //storing to id item of s1
cin>>s1.name; //storing a name to s1
cout<<s1.id; //displaying the content of id of s1.
cout<<s1.name; //displaying name
2024-12-25 Kassahun @2024 15
Example
• Example:-a program that creates student struct and uses it to store student
information.
• In one quick declaration, this code creates 1,000 store structures with the definition of the
Company structure, each one containing three members.
• NB. Be sure that your computer does not run out of memory when you create a large number
of structures. Arrays of structures quickly consume valuable information.
2024-12-25 Kassahun @2024 17
Declaration of array of Structure
• You can also define the array of structures after the declaration of the
structure. struct Company
{
int employees;
int registers;
double sales;
}; // no structure variables defined yet
#include<iostream.h>
…
void main()
{
struct Company store[1000]; //the variable store is array of the structure Company
…
}
2024-12-25 Kassahun @2024 18
Declaration of array of Structure
#include <stdio.h>
// Example: Print values for the first store
int main() { cout<<("Store 1:\n");
struct Company { cout<<("Employees: ", store[0].employees);
int employees; cout("Registers:", store[0].registers);
int registers; cout("Sales:", store[0].sales);
double sales;
} store[1000]; return 0;
}
// Example: Assign values to the first
store
store[0].employees = 15;
store[0].registers = 3;
store[0].sales = 12345.67;
• Any change in the value of formal parameter inside function body, will not affect
the value of actual parameter.
struct employee {
char name[100];
int age;
float salary;
char department[50];
};
void printEmployeeDetails (employee emp);
2024-12-25 Kassahun @2024 24
• We can also pass address of a structure to a function.
• In this case, any change in the formal parameter inside
function's body will reflect in actual parameter also.
• To take a structure pointer as parameter a function declare a
structure pointer as it's formal parameter.