0% found this document useful (0 votes)
31 views14 pages

Slide 3

The document is a lecture on structures in C++ from Ghalib University's Computer Science faculty. It defines what structures are, how to declare and define them, how to create structure variables, initialize structures, use arrays of structures, and create nested structures. It provides examples of each concept to demonstrate how structures work in C++.

Uploaded by

Ahmad Rahimi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views14 pages

Slide 3

The document is a lecture on structures in C++ from Ghalib University's Computer Science faculty. It defines what structures are, how to declare and define them, how to create structure variables, initialize structures, use arrays of structures, and create nested structures. It provides examples of each concept to demonstrate how structures work in C++.

Uploaded by

Ahmad Rahimi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

ISLAMIC REPUBLIC OF AFGHANISTAN

MINISTRY OF HIGHER EDUCATION


GHALIB UNIVERSITY
COMPUTER SCEINE FACULTY
2TH SEMESTER
Advance Programming Structure
LECTURER : Zabihullah Rahmani

1 Ghalib University
Table of Contents
1. Structure
2. Structure variable
3. Arrays of Structures
4. Nested Structures

2 Ghalib University
Structure
 C++ allows you to group several variables together into a single

item known as a structure.

 Structure is the collection of same or different types elements

also called user defined data type.

 Structure is same like array but in array we can store only same

type elements while in structure we can store different types


elements.

 The elements of structure are called the members of structures.

3 Ghalib University
Structure
 struct is a key word used for defining structure.
 type is any valid data type.
 a, b, c are the name of variables.
 Example: To declare Student structure.
struct students
{
int roll_no;
char name[40];
int marks;
};

 This structure have three members data items. One is integer


type roll_no, second character type name array and third integer
type marks.
4 Ghalib University
Example of Structure Declaration
#include <iostream>
using namespace std;
struct students{
int roll_no;
char name[40];
int marks;
};
int main()
{

return 0;
}

5 Ghalib University
Structure variable
 A variable which is declared as structure type is called structure

variable.

General syntax as same like other variable.

Stucture_neme variable_name;

for example to declaring a variable of

previous students structure.

students s1;

6 Ghalib University
Accessing Members of Structure
 The dot operator(.) allows you to access structure members in a

program.

 to access the marks member of s1 variable of students structure

s1.marks=70; // assigning value though =

cin>>s1.marks; // assigning value through cin

cout<<s1.marks; // printing value of marks.

7 Ghalib University
Example of Structure variable
#include <iostream>
using namespace std;
struct students{
int roll_no;
char name[40];
int marks;};

int main()
{
students s1;
cout<<"please enter the roll number :";
cin>>s1.roll_no;
cout<<"please enter the name :";
cin>>s1.name;
cout<<"please enter the marks :";
cin>>s1.marks;
return 0;
}

8 Ghalib University
Initializing a Structure
 Assigning values to a structure variables at the time of

declaration is called initialization of structure variables.

 At the time of assigning value we most take care of the order of

the structure members in which they are written in structure.

 For Example Initializing a variable S1 of the previous students

structure.

 Students S1={20,”Mirwais”,75};

9 Ghalib University
Arrays of Structures
 Arrays of structures can simplify some programming tasks.

 A structure variable can also be defined as an array. Means we

can define an array of structure type. In which we can store a lot


amount of structure variables.
 struct student{
char name[15];
int marks;
char city[20];
};
 students s1[10];

10 Ghalib University
Structure Array Initialization
 Like other arrays the structure array can also be initialized.
struct students
{
int roll;
char name[20];
int total;
};
students S1[3]=
{{101,”Ferdows”450},{102,”Zobair”,500},{103,”Kabir”,650} }

11 Ghalib University
Nested Structures
 It’s possible for a structure variable to be a member of another

structure variable.

 Sometimes it’s helpful to nest structures inside other structures.

12 Ghalib University
Example of Nested Structures
#include<iostream>
using namespace std;
struct Price
{
double wholesale;
double retail;
};
struct item
{
int code;
string description;
Price p;
};
int main()
{ item item1;
item1.p.wholesale = 100.0;
item1.p.retail = 150.0;
cout<<item1.p.wholesale<<endl;
cout<<item1.p.retail<<endl;
return 0;
}
13 Ghalib University
Any question…?????????????

14 Ghalib University

You might also like