0% found this document useful (0 votes)
91 views9 pages

Structures

The document discusses structures in C++. It defines a structure as a collection of multiple data types that can be referenced with a single name. The document then provides examples of declaring and defining structure variables, accessing structure members, initializing structure variables, assigning one structure variable to another, and defining arrays of structures and nested structures. It includes code examples for declaring structures to store student, birth, book, employee, phone number, and payroll data and demonstrates initializing, inputting, assigning, and outputting structure variable values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views9 pages

Structures

The document discusses structures in C++. It defines a structure as a collection of multiple data types that can be referenced with a single name. The document then provides examples of declaring and defining structure variables, accessing structure members, initializing structure variables, assigning one structure variable to another, and defining arrays of structures and nested structures. It includes code examples for declaring structures to store student, birth, book, employee, phone number, and payroll data and demonstrates initializing, inputting, assigning, and outputting structure variable values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Global Institute of Computer Science

Topic

1-Structures
1.1.1-Declaring a structure
1.1.2-Defining a structure variable
1.1.3-Accessing members of structure variable
1.1.4-Initializing structure variables at declaration
1.1.5-Assigning one structure variable to other
1.1.6-Array as member of structure
1.1.6.1-Initialzing a structure with array as member

1.2-Arrays of structures
1.2.1-Initializing Array of structures
1.3- Nested Structure

1-Structures
A structure is a collection of multiple data types that can be referenced with single name. It may
contain similar or different data types. The data items in a structure are called structure
members, elements or fields. The structures are used to join simple variables together to form
complex variables.

Difference between array and structure?


The structures are used to define new data types. The user can define a new data type that may
contain different types of data. A simple variable can store only one value at a time but the
structure variable can store multiple values at the same time.

1.1-Declaring a structure
Syntax
Struct structure name
{
Data type1 identifier1;
Data type2 identifier2;
};

e.g
struct Student
{
Int RollNo;
Int Marks;
Float avg;
Char Grade;
};
Global Institute of Computer Science
1.2 Defining structure variable

Student ali;

1.3 Accessing members of structure variable


Struct_var.member_var (dot)

Example:
student ali;
ali.RollNo=20;
ali.Marks=89;
ali.avg=67.6;
ali.Grade=’a’;

you can do it by cin>>


you can see what inside it is by cout<<

/*
write a program that declares a structure Student to store Roll No, Marks, Average and
Grade of a student. The program should define a structure variable, input the
values and then displays these values.
*/

#include<iostream.h>

struct Student
{
int rollNo;
int marks;
float avg;
char grade;
};//end of structure Student

int main()
{
Student s;
cout<<"Enter Roll No";
cin>>s.rollNo;
cout<<"Enter Marks";
Global Institute of Computer Science
cin>>s.marks;
cout<<"Enter Average";
cin>>s.avg;
cout<<"Enter Grade";
cin>>s.grade;
cout<<"You Entered following details of student:\n";
cout<<"Roll No:"<<s.rollNo<<endl;
cout<<"Marks:"<<s.marks<<endl;
cout<<"Average:"<<s.avg<<endl;
cout<<"Grade:"<<s.grade<<endl;
}//end of main

////////////////// Example .2////////////////////////////////


/*
write a program that declares a structure named Birth to store day,month and year of birth date.it
inputs three values and displays date of birth in dd/mm/yyyy format.
*/

#include<iostream.h>

struct Birth
{
int day;
int month;
int year;
};//end of structure Birth

int main()
{
Birth b;
//cout<<"Memory Occupy by structure variable"<<sizeof(b)<<endl;
cout<<"Enter Date of Birth:";
cin>>b.day;
cout<<"Enter Month of Birth:";
cin>>b.month;
cout<<"Enter Year of Birth:";
cin>>b.year;
cout<<"Your Complete date of birth is:"<<b.day<<"/"<<b.month<<"/"<<b.year;

}//end of main
Global Institute of Computer Science

/* Example .3
write a program that declares a structure named Book to store BookID,price and
pages of a book. it defines two variables and inputs values. it displays the record
of most costly book..Note you have to compare the book by price
*/

#include<iostream.h>
struct Book
{
int BookID;
int pages;
float price;

};//end of structure Book

void main()
{
Book first;
Book second;
cout<<"Enter first book Details"<<endl;
cout<<"Enter BookID:";
cin>>first.BookID;
cout<<"Enter Book Pages:";
cin>>first.pages;
cout<<"Enter Book Price:";
cin>>first.price;

cout<<"Enter second book Details"<<endl;


cout<<"Enter BookID:";
cin>>second.BookID;
cout<<"Enter Book Pages:";
cin>>second.pages;
cout<<"Enter Book Price:";
cin>>second.price;

cout<<"The most costly book is"<<endl;


if(first.price>second.price)
{
cout<<"BookID:"<<first.BookID<<endl;
cout<<"Book Pages:"<<first.pages<<endl;
Global Institute of Computer Science
cout<<"Book Price:"<<first.price<<endl;
}//end of if
else
{
cout<<"BookID:"<<second.BookID<<endl;
cout<<"Book Pages:"<<second.pages<<endl;
cout<<"Book Price:"<<second.price<<endl;
}//end of else

}//end of main

1.4 Initializing structure variables (At declaration time)


The process of assigning the values to the members variables of structure is called initialization
of structure variable.
The assignment operator is used for initialization. The declaration of structure variable is
specified on the left side of assignment operator. The values are written in the braces. The values
are written in the same sequence or order in which they are specified in structure declaration.
Each value is separated by comma.

Syntax
Struct_Name identifier={value1,value2…..};

Example:
Student ali={1,223,89,’A’};

/* Example .4
Initialize structure variable at the time of declaration

write a program that declares a structure Employee to store employee id and salary of an
employee. it defines and initializes a structure variable and displays it.
*/

#include<iostream.h>

struct Employee
{
int empId;
int esalary;
};//end of Employee structure
Global Institute of Computer Science
//Employee em; //if you declare structure variable outside the main then initialize to zero
//if you declare structure variable inside the main then initialize
to garbage

int main()
{

Employee e={1,5000};
cout<<"Employee ID:"<<e.empId<<endl;
cout<<"Employee Salary:"<<e.esalary<<endl;

} //end of main

/* Example .5
Initialize structure variable at the time of declaration

write a program that declares a structure Phone to store three parts of phone number i.e country
Code, City Code and number separately.
create two variables of structure phone, initialize one variable at the time of declaration and get
inputs from the user in the second variable and displays both of them.

*/

#include<iostream.h>

struct Phone
{
int countrycode;
int citycode;
long number;
};//end of Phone structure

/* Example .6
write a program that uses a structure named as PayRoll to store employee ID, name, hours
worked, hourly rate and gross pay.
the program inputs employee number, name, hours worked and hourly rate. the user calculates
gross pay and then displays all employee data on the screen.

note:
the gross pay=hours worked*hourly rate;
Global Institute of Computer Science
*/

#include<iostream.h>
#include<stdio.h>

struct PayRoll
{
int empID;
char firstname[50];
char lastname[50];
double hoursworked;
double hourlyrate;
double grosspay;

};//end of PayRoll structure

int main()
{
PayRoll employee;
cout<<"Enter Employee ID:";
cin>>employee.empID;
cout<<"Enter Employee First name:";
gets(employee.firstname);
cout<<"Enter Employee Last name:";
gets(employee.lastname);
cout<<"Enter Number of hours worked by Employee:";
cin>>employee.hoursworked;
cout<<"Enter hourly rate of Employee:";
cin>>employee.hourlyrate;

// calculating gross pay

employee.grosspay=employee.hoursworked*employee.hourlyrate;
cout<<"-------EMPLOYEE PAY ROLL DATA---------"<<endl;
cout<<"Enter Employee ID:"<<employee.empID<<endl;
cout<<"Enter Employee First name:"<<employee.firstname<<endl;
cout<<"Enter Employee Last name:"<<employee.lastname<<endl;
cout<<"Enter Number of hours worked by Employee:"<<employee.hoursworked<<endl;
cout<<"Enter hourly rate of Employee:"<<employee.hourlyrate<<endl;
cout<<"Gross Pay of "<<employee.firstname<<"
"<<employee.lastname<<":"<<employee.grosspay<<endl;
Global Institute of Computer Science
} //end of main

Int main()
{
Phone p1 = {92,42,420420}; //if you don’t initialize any index than by default it initializes to
zero
Phone p2;
cout<<"Enter Country Code of Second Phone:";
cin>>p2.countrycode;
cout<<"Enter City Code of Second Phone:";
cin>>p2.citycode;
cout<<"Enter number of Second Phone:";
cin>>p2.number;

cout<<"Phone Number 1"<<endl;


cout<<p1.countrycode<<"-"<<p1.citycode<<"-"<<p1.number<<endl;

cout<<"Phone Number 2"<<endl;


cout<<p2.countrycode<<"-"<<p2.citycode<<"-"<<p2.number<<endl;

} //end of main

1.5 Assigning One structure variable to Other

A structure variable can be initialized by assigning another structure variable to it by using


assignment operator as follows:
Student Usman={1,765,78.5,’B’};
Student Ali=Usman;

/* Assigning one strucutre variable to other


write a program that declares as structure named Marks to store marks and grade of a student. It
defines two structre variables. it inputs the values in one variable
and assigns that variable to the second variable. it finally dispalys the vlaue of both variables.

*/

#include<iostream.h>

struct Marks
{
int m;
char grade;
Global Institute of Computer Science
};//end of sturcture Marks

int main()
{
Marks first,second;
cout<<"Enter Marks in first";
cin>>first.m;
cout<<"Enter Grade in first";
cin>>first.grade;

second=first; //assigning one structure variable to another

cout<<"First Record is as follows:"<<endl;


cout<<"Marks"<<first.m<<endl;
cout<<"Marks"<<first.grade<<endl;

cout<<"-------------------------------------"<<endl;
cout<<"Second Record is as follows:"<<endl;
cout<<"Marks"<<second.m<<endl;
cout<<"Marks"<<second.grade<<endl;

}//end of main

You might also like