KEMBAR78
Programming Fundamentals lecture-24-1.pptx
1
Programming Fundamentals
LECTURE-24
GHULAM ABBAS
abbas.sbcws@gmail.com
2
In the Previous Lecture
 Structure
 Defining the Structure
 Structure initialization
 Accessing Structure member
 Copying and Comparing Structure Variables
 Array of structure
3
Today’s Lecture
 Structure within another Structure
 Function and Structure
Structure within another Structure
 Also known as nested structure
 Let us consider a structure personal_record to store the information of a
person as:
struct personal_record
{
char name[20];
int day_of_birth;
int month_of_birth;
int year_of_birth;
float salary;
}person;
Nested structure
 In the structure above, we can group all the items related to birthday
together and declare them under a substructure as:
struct Date
{
int day_of_birth;
int month_of_birth;
int year_of_birth;
};
struct personal_record
{
char name[20];
Date birthday; //structure variable
float salary;
}person;
Nested structure
 Here, the structure personal_record contains a member named
birthday which itself is a structure with 3 members. This is called
structure within structure.
 The members contained within the inner structure can be
accessed as:
person.birthday.day_of_birth
person.birthday.month_of_birth
person.birthday. year_of_birth
 The other members within the structure personal_record are
accessed as usual:
person.name
person.salary
Example: Nested structure
struct Date
{
int day_of_birth;
int month_of_birth;
int year_of_birth;
};
struct personal_record
{
char name[20];
struct Date birthday; //structure variable
float salary;
};
int main()
{
Date birthday={12,03,200};
personal_record
person={"Ghulam ali",birthday ,20000};
cout<<person.birthday.day_of_birth<<
endl;
cout<<person.name;
}
Example: Nested structure
struct date
{
int day;
int month;
int year;
};
struct name
{
char first_name[10];
char middle_name[10];
char last_name[10];
};
struct personal_record
{
float salary;
date birthday,deathday;
name full_name;
};
Try yourself
Create a structure named Date that has day, month
and year as its members. Include this structure as a
member in another structure named Employee which
has name, id and salary as other members. Use this
structure to read and display employee’s name, id,
date of birthday and salary.
Function and Structure
 Passing the individual members to functions
 Passing whole structure to functions
 Passing array of structure to functions
Passing the individual members to functions
Structure members can be passed to functions
as actual arguments in function call like ordinary
variables.
Problem: Huge number of structure members
Example: Let us consider a structure Employee
having members name, id and salary and pass
these members to a
Example: Passing the individual members to functions
Structure members can be passed to functions
as actual arguments in function call like ordinary
variables.
Problem: Huge number of structure members
Example: Let us consider a structure Employee
having members name, id and salary and pass
these members to a
int display(char [],int,double);f//unction declaration
struct Emplyee
{
char name[20];
int ID;
double salary;
};
int main()
{
Emplyee emp={"junaid Khan", 123, 20000};
display(emp.name,emp.ID,emp.salary);//function call
}
int display(char eName[],int id, double sal)//definition
{
cout<<eName<<"t"<<id<<"t"<<sal;
}
Passing whole structure to functions
 Whole structure can be passed to a function by the syntax:
function_name(structure_variable_name);
 The called function has the form:
return_type function_name(struct tag_name structure_variable_name)
{
… … … … …;
}
Example: Passing whole structure to functions
int display(struct Emplyee emp);//function declaration
struct Emplyee
{
char name[20];
int ID;
double salary;
};
int main()
{
Emplyee emp={"junaid Khan", 123, 20000};
display(emp);//function call
}
int display(Emplyee emp)//definition
{
cout<<emp.name<<"t"<<emp.ID<<"t"<<emp.salary;
}
Passing array of structure to functions
 Passing an array of structure type to a function is similar to passing an array of any
type to a function.
 That is, the name of the array of structure is passed by the calling function which is
the base address of the array of structure.
 Note: The function prototype comes after the structure definition.
Example:Passing array of structure to functions
struct Emplyee
{
char name[20];
int ID;
double salary;
};
int display(struct Emplyee
emp[2]);//function declaration
int main()
{
Emplyee emp[2]={{"junaid
Khan", 123, 20000},"junaid Khan", 123,
20000};
display(emp);//function call
}
int display(Emplyee emp[])//definition
int display(Emplyee emp[])//definition
{
int i=0;
cout<<"n Namett IDtt Salaryn";
cout<<"________________________________________
n";
for(;i<2;i++)
{
cout<<emp[i].name<<"t
"<<emp[i].ID<<"tt "<<emp[i].salary<<endl;
}
}
output

Programming Fundamentals lecture-24-1.pptx

  • 1.
  • 2.
    2 In the PreviousLecture  Structure  Defining the Structure  Structure initialization  Accessing Structure member  Copying and Comparing Structure Variables  Array of structure
  • 3.
    3 Today’s Lecture  Structurewithin another Structure  Function and Structure
  • 4.
    Structure within anotherStructure  Also known as nested structure  Let us consider a structure personal_record to store the information of a person as: struct personal_record { char name[20]; int day_of_birth; int month_of_birth; int year_of_birth; float salary; }person;
  • 5.
    Nested structure  Inthe structure above, we can group all the items related to birthday together and declare them under a substructure as: struct Date { int day_of_birth; int month_of_birth; int year_of_birth; }; struct personal_record { char name[20]; Date birthday; //structure variable float salary; }person;
  • 6.
    Nested structure  Here,the structure personal_record contains a member named birthday which itself is a structure with 3 members. This is called structure within structure.  The members contained within the inner structure can be accessed as: person.birthday.day_of_birth person.birthday.month_of_birth person.birthday. year_of_birth  The other members within the structure personal_record are accessed as usual: person.name person.salary
  • 7.
    Example: Nested structure structDate { int day_of_birth; int month_of_birth; int year_of_birth; }; struct personal_record { char name[20]; struct Date birthday; //structure variable float salary; }; int main() { Date birthday={12,03,200}; personal_record person={"Ghulam ali",birthday ,20000}; cout<<person.birthday.day_of_birth<< endl; cout<<person.name; }
  • 8.
    Example: Nested structure structdate { int day; int month; int year; }; struct name { char first_name[10]; char middle_name[10]; char last_name[10]; }; struct personal_record { float salary; date birthday,deathday; name full_name; };
  • 9.
    Try yourself Create astructure named Date that has day, month and year as its members. Include this structure as a member in another structure named Employee which has name, id and salary as other members. Use this structure to read and display employee’s name, id, date of birthday and salary.
  • 10.
    Function and Structure Passing the individual members to functions  Passing whole structure to functions  Passing array of structure to functions
  • 11.
    Passing the individualmembers to functions Structure members can be passed to functions as actual arguments in function call like ordinary variables. Problem: Huge number of structure members Example: Let us consider a structure Employee having members name, id and salary and pass these members to a
  • 12.
    Example: Passing theindividual members to functions Structure members can be passed to functions as actual arguments in function call like ordinary variables. Problem: Huge number of structure members Example: Let us consider a structure Employee having members name, id and salary and pass these members to a
  • 13.
    int display(char [],int,double);f//unctiondeclaration struct Emplyee { char name[20]; int ID; double salary; }; int main() { Emplyee emp={"junaid Khan", 123, 20000}; display(emp.name,emp.ID,emp.salary);//function call } int display(char eName[],int id, double sal)//definition { cout<<eName<<"t"<<id<<"t"<<sal; }
  • 14.
    Passing whole structureto functions  Whole structure can be passed to a function by the syntax: function_name(structure_variable_name);  The called function has the form: return_type function_name(struct tag_name structure_variable_name) { … … … … …; }
  • 15.
    Example: Passing wholestructure to functions int display(struct Emplyee emp);//function declaration struct Emplyee { char name[20]; int ID; double salary; }; int main() { Emplyee emp={"junaid Khan", 123, 20000}; display(emp);//function call } int display(Emplyee emp)//definition { cout<<emp.name<<"t"<<emp.ID<<"t"<<emp.salary; }
  • 16.
    Passing array ofstructure to functions  Passing an array of structure type to a function is similar to passing an array of any type to a function.  That is, the name of the array of structure is passed by the calling function which is the base address of the array of structure.  Note: The function prototype comes after the structure definition.
  • 17.
    Example:Passing array ofstructure to functions struct Emplyee { char name[20]; int ID; double salary; }; int display(struct Emplyee emp[2]);//function declaration int main() { Emplyee emp[2]={{"junaid Khan", 123, 20000},"junaid Khan", 123, 20000}; display(emp);//function call } int display(Emplyee emp[])//definition int display(Emplyee emp[])//definition { int i=0; cout<<"n Namett IDtt Salaryn"; cout<<"________________________________________ n"; for(;i<2;i++) { cout<<emp[i].name<<"t "<<emp[i].ID<<"tt "<<emp[i].salary<<endl; } }
  • 18.