0% found this document useful (0 votes)
33 views

Programming Fundametals in C+ Lec 15

The document discusses structures in C++. It defines a structure as a compound data type that contains different variables of different types. Structures allow storing together elements of different data types. The key points are: - Structures are declared using the "struct" keyword followed by an identifier. - Structure variables are declared and members accessed using the dot operator. - Arrays of structures can be used to store multiple records. - Structures can be passed as arguments to functions.

Uploaded by

Aqsa gul
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Programming Fundametals in C+ Lec 15

The document discusses structures in C++. It defines a structure as a compound data type that contains different variables of different types. Structures allow storing together elements of different data types. The key points are: - Structures are declared using the "struct" keyword followed by an identifier. - Structure variables are declared and members accessed using the dot operator. - Arrays of structures can be used to store multiple records. - Structures can be passed as arguments to functions.

Uploaded by

Aqsa gul
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Lecture 15

Structures

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Structures

 Structure is a compound data type that contains different variables of different types.
 A STRUCT is a C++ data structure that can be used to store together elements of different
data types. 
 Declaring Structure in C++:
 To declare a structure “struct” keyword followed by an identifier. The identifier becomes
the name of the struct.

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Syntax
struct structname
{
// struct members
};
Note: Structure declaration ends with “;” (Semicolon).

 Example:
struct Student
{
string name;
int id;
int age;
};
Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar
Creating Struct Instance

struct Student
{
string name;
int id;
int age;
};

 Struct Student s;

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Accessing Struct member

 To access the struct members, we use the instance of the struct and the dot (.) operator.

 Example
 s.age = 27;

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Example
#include <iostream>
using namespace std;
struct Student
{
int id;
int age;
};
int main( ) {
struct Student s;
s.id = 1;
s.age = 27;
cout << “Student ID: " << s.id << endl;
cout << “Student age: " << s.age << endl;

return 0;
Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar
}
Example
struct student
{
cout << "First Student" << endl;
int roll_no;
cout << "roll no : " << p1.roll_no << endl;
string name; cout << "name : " << p1.name << endl;
int phone_number; cout << "phone no : " << p1.phone_number << endl;
}; cout << "Second Student" << endl;
Int main(){ cout << "roll no : " << p2.roll_no << endl;
cout << "name : " << p2.name << endl;
struct student p1 = {1,"Brown",123443};
cout << "phone no : " << p2.phone_number << endl;
struct student p2, p3;
cout << "Third Student" << endl;
p2.roll_no = 2; cout << "roll no : " << p3.roll_no << endl;
p2.name = "Sam"; cout << "name : " << p3.name << endl;
p2.phone_number = 1234567822; cout << "phone no : " << p3.phone_number << endl;
p3.roll_no = 3;
return 0;
}
p3.name = "Addy";
p3.phone_number = 1234567844;
Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar
Task

 Write C++ Program to assign data to members of a structure variable and display it.

 Structure stores data of person name, age and salary.

 Let
person name = Iqbal
Age = 34
Salary = 25000

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Storing value in char array with struct

Struct student
{
char name[30];
};
int main()
{
Struct student s;
cout<<“Enter student name”<<endl;
cin.get(s.name,30);
cout<<“Name of student : ”<<s.name<<endl;
}
Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar
Structure and Function

 Structure variable can be passed to a function in a similar way as argument is passed.

 Example:

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


struct Person
{ int main()
char name[50]; {
Person p;
int age;
cout << "Enter Full name: ";
float salary; cin.get(p.name, 50);
cout << "Enter age: ";
};
cin >> p.age;
void displayData(Person p) cout << "Enter salary: ";
{ cin >> p.salary;
// Function call with structure variable as an argument
cout << "\nDisplaying Information." << endl; displayData(p);
cout << "Name: " << p.name << endl; return 0;
}
cout <<"Age: " << p.age << endl;
cout << "Salary: " << p.salary;
}

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Array of structure
 if we want more than one record of structure type, we have to create an array of structure.
 an array is a collection of similar type, therefore an array can be of structure type.

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Syntax of declaring Structure array

struct struct-name
{
datatype var1;
datatype var2;
----------
----------
datatype varN;
};

struct-name obj [ size ];

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar


Example
for(int i=0; i<3; i++)
{
struct Employee
cout << "\nEnter details of frirst Employee";
{ cout << "\n\tEnter Employee Id : ";
int Id; cin >> Emp[i].Id;
cout << "\n\tEnter Employee Name : ";
string name; cin >> Emp[i].Name;
int Age; cout << "\n\tEnter Employee Age : ";
cin >> Emp[i].Age;
long Salary; cout << "\n\tEnter Employee Salary : ";
}; cin >> Emp[i].Salary;
}
void main() cout << "\nDetails of Employees";
{ for(int j=0;j<3;j++)
{
Employee Emp[3]; cout << "\n"<< Emp[j].Id <<"\t"<< Emp[j].Name <<"\t"
<< Emp[j].Age <<"\t"<< Emp[j].Salary;
}
Prepared by Aqsa Gul, Lecturer Institute of Management Sciences}
Peshawar
Tasks

 Write a program to store and print the roll no., name , age, department name and marks of
a student using structures.
 4 students data using structure.

Prepared by Aqsa Gul, Lecturer Institute of Management Sciences Peshawar

You might also like