Professional Documents
Culture Documents
Lecture 2
// 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'
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 };
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];
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.