You are on page 1of 21

Structures

Lecture 2

Instructor: Maria Amjad


Structure basics
• A structure is a user-defined data type in C/C++.
A structure creates a data type that can be used
to group items of possibly different types into a
single type.
How to create a structure?
• The ‘struct’ keyword is used to create a structure.
The general syntax to create a structure is as
shown below:
struct structureName{
member1;
member2;
member3;
...
memberN; };
Members of Structures
• Structures in C++ can contain two types of
members:
• Data Member: These members are normal
C++ variables. We can create a structure with
variables of different data types in C++.
• Member Functions: These members are
normal C++ functions. Along with variables, we
can also include functions inside a structure
declaration
Example
// Data Members
int roll;
int age;
int marks;

// Member Functions
void printDetails()
{
cout<<"Roll = "<<roll<<"\n";
cout<<"Age = "<<age<<"\n";
cout<<"Marks = "<<marks;
}
How to declare structure variables?
// A variable declaration with structure declaration.
struct Point
{
int x, y;
} p1; // The variable p1 is declared with 'Point'

// A variable declaration like basic data types


struct Point
{
int x, y;
};

int main()
{
struct Point p1; // The variable p1 is declared like a normal variable
}
How to initialize structure members?
Structure members can be initialized using curly braces
‘{}’. For example, following is a initialization.
struct Point
{
int x, y;
};

int main()
{
// A initialization. member x gets value 0 and y
// gets value 1. The order of declaration is followed.
struct Point p1 = {0, 1};
}
How to access structure elements?
#include <iostream>
using namespace std;

struct Point {
int x, y;
};

int main()
{
struct Point p1 = { 0, 1 };

// Accessing members of point p1


p1.x = 20;
cout << "x = " << p1.x << ", y = " << p1.y;

return 0;
}
Output:
x = 20, y = 1
What is an array of structures?
#include <iostream>
using namespace std;

struct Point {
int x, y;
};

int main()
{
// Create an array of structures
struct Point arr[10];

// Access array members


arr[0].x = 10;
arr[0].y = 20;

cout << arr[0].x << " " << arr[0].y;


return 0;
}
Output:
10 20
Structure within structures
• When a structure contains another structure, it
is called nested structure. For example,we have
two structures named Address and Employee.
To make Address nested to Employee, we have
to define Address structure before and outside
Employee structure and create an object of
Address structure inside Employee structure.
Syntax
struct structure1
{
----------
----------
};
struct structure2
{
----------
----------
struct structure1 obj;
};
Example
#include<iostream.h>
struct Address
{
char HouseNo[25];
char City[25];
char PinCode[25];
};
struct Employee
{
int Id;
char Name[25];
float Salary;
struct Address Add;
};
Example (Cont.)
void main()
{
int i;
Employee E;
cout << "\n\tEnter Employee Id : ";
cin >> E.Id;
cout << "\n\tEnter Employee Name : ";
cin >> E.Name;
cout << "\n\tEnter Employee Salary : ";
cin >> E.Salary;
cout << "\n\tEnter Employee House No : ";
cin >> E.Add.HouseNo;
cout << "\n\tEnter Employee City : ";
cin >> E.Add.City;
cout << "\n\tEnter Employee House No : ";
cin >> E.Add.PinCode;
Example (Cont.)
cout << "\nDetails of Employees";
cout << "\n\tEmployee Id : " << E.Id;
cout << "\n\tEmployee Name : " << E.Name;
cout << "\n\tEmployee Salary : " << E.Salary;
cout << "\n\tEmployee House No : " << E.Add.HouseNo; cout << "\n\tEmployee City : " << E.Add.City; cout << "\n\tEmployee
House No : " << E.Add.PinCode;
}
Output :
Enter Employee Id : 101
Enter Employee Name : Suresh
Enter Employee Salary : 45000
Enter Employee House No : 4598/D
Enter Employee City : Delhi
Enter Employee Pin Code : 110056

Details of Employees
Employee Id : 101
Employee Name : Suresh
Employee Salary : 45000
Employee House No : 4598/D
Employee City : Delhi
Employee Pin Code : 110056
ENUMERATIONS
• An enumeration is a user-defined data type that
consists of integral constants. To define an
enumeration, keyword enum is used.
enum season { spring, summer, autumn, winter };
• Here, the name of the enumeration is season.
• And, spring, summer and winter are values of
type season.
• By default, spring is 0, summer is 1 and so on.
Changing default Value of enum
element
You can change the default value of an enum element during
declaration (if necessary).

enum season {
spring = 0,
summer = 4,
autumn = 8,
winter = 12
};
Enumerated Type Declaration
• When you create an enumerated type, only blueprint for the variable
is created. Here's how you can create variables of enum type.

enum boolean { false, true };


enum boolean check; // inside function
Here, a variable check of type enum boolean is created.

• Here is another way to declare same check variable using different


syntax.
enum boolean { false, true } check;
Enumerations
Example
#include <iostream>
using namespace std;
enum week
{ Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
int main()
{
week today;
today = Wednesday; //An enum variable takes only one value out of
many possible value
cout << "Day " << today+1; OUTPUT:
return 0;
} Day 4
Example
#include <iostream>
using namespace std;
enum seasons //enum
{
spring = 34, //elements of enum
summer = 4,
autumn = 9,
winter = 32
};
int main()
{ OUTPUT:
seasons s; //variable of enum
s = summer; Summer = 4
cout << "Summer = " << s << endl;
return 0; }

You might also like