25
Structures and Structures and Functions Functions Ghulam Nasir Khan Ghulam Nasir Khan

Structures and Functions Ghulam Nasir Khan. Important /\/otes Programming is a fundamental course of IT as well as BICSE, so it will be very difficult

Embed Size (px)

Citation preview

Structures and FunctionsStructures and Functions

Ghulam Nasir KhanGhulam Nasir Khan

Important /\/otesImportant /\/otes

Programming is a fundamental course of IT as Programming is a fundamental course of IT as well as BICSE, so it will be very difficult to well as BICSE, so it will be very difficult to proceed without programming.proceed without programming.

You will be thought different courses on You will be thought different courses on programming throughout your course of study programming throughout your course of study such as OOP using C++, Data Structures, such as OOP using C++, Data Structures, Algorithms, Assembly Language, Web Algorithms, Assembly Language, Web Designing so there is no way out.Designing so there is no way out.

The key point is try to develop interest in The key point is try to develop interest in programming and try to practice as much as you programming and try to practice as much as you can. can.

Important /\/otesImportant /\/otes

Try to clear all of your concepts and Try to clear all of your concepts and practice as much as possible you that can practice as much as possible you that can make a good understanding of make a good understanding of programmingprogramming

Do not let the instructor proceed, even if Do not let the instructor proceed, even if you have a small confusion in mind.you have a small confusion in mind.

Try to learn as much programming as you Try to learn as much programming as you can in this semester so that you do not can in this semester so that you do not have to cry over spilt milk.have to cry over spilt milk.

BEST OF LUCKBEST OF LUCK

An Overview of StructuresAn Overview of Structures

What is StructureWhat is Structure

Structure is used to store data of same or Structure is used to store data of same or different type. It is used to group data that different type. It is used to group data that may be of the same or different types.may be of the same or different types.

Basically it is used to store data that has a Basically it is used to store data that has a logical relation e.g. record of a student etc.logical relation e.g. record of a student etc.

It can also be said that structure is used to It can also be said that structure is used to create a new data type comprising of the create a new data type comprising of the existing data types.existing data types.

Composition of StructuresComposition of Structures

Structures are composed ofStructures are composed of Data MembersData Members Member FunctionsMember Functions

Data Members are memory locations that Data Members are memory locations that are used to store data e.g. variables, are used to store data e.g. variables, arrays etc.arrays etc.

Member Functions are used to manipulate Member Functions are used to manipulate the Data Members.the Data Members.

Memory !!!!Memory !!!!

How much memory is occupied by How much memory is occupied by Structures?Structures?

1 Byte?1 Byte? 4 Bytes?4 Bytes? 8 Bytes?8 Bytes? N Bytes? (Specify the value of N)N Bytes? (Specify the value of N)

The answer is The answer is Zero BytesZero Bytes

Structure VariableStructure Variable

The representation of Structure is memory The representation of Structure is memory is called is called Structure VariableStructure Variable or or Structure Structure ObjectObject or simply or simply ObjectObject..

Object occupies space in the memory and Object occupies space in the memory and object actually stores the data.object actually stores the data.

Memory !!!!Memory !!!!

How much memory is occupied by How much memory is occupied by Structure Object?Structure Object?

0 Byte?0 Byte? 1 Byte?1 Byte? 4 Bytes?4 Bytes? 8 Bytes?8 Bytes? 16 Bytes?16 Bytes? N Bytes? (Specify the value of N)N Bytes? (Specify the value of N)

Memory !!!!Memory !!!!

The answer is The answer is The sum of memory occupied by all The sum of memory occupied by all

the data members of a structure.the data members of a structure.

For Example, all the objects of a structure For Example, all the objects of a structure having two integer, one float and one having two integer, one float and one character data member will occupy 4 * 2 + character data member will occupy 4 * 2 + 4 + 1 = 8 + 4 + 1 = 13 Bytes4 + 1 = 8 + 4 + 1 = 13 Bytes

Declaring a StructureDeclaring a Structure

struct StructureNamestruct StructureName{{ DataType variable 1;DataType variable 1;

DataType variable 2;DataType variable 2; .. .. DataType variable n;DataType variable n;

};};

Creating Structure ObjectCreating Structure Object

struct StructureName objname;struct StructureName objname;

StructureName objname;StructureName objname;

Here Here objnameobjname is a user defined name of is a user defined name of the object. It must follow Variable the object. It must follow Variable Naming Rules.Naming Rules.

/*/*

Program Name: struct.cppProgram Name: struct.cpp

Author: NasirAuthor: Nasir

*/*/

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

struct empstruct emp

{{

int empno;int empno;

float salary;float salary;

};};

void main()void main(){{

emp e;emp e;cout<<"Enter employee number:”;cout<<"Enter employee number:”;cin>>e.empno;cin>>e.empno;cout<<”Enter salary:”;cout<<”Enter salary:”;cin>>e.salary;cin>>e.salary;cout<<”You have entered:”<<endl;cout<<”You have entered:”<<endl;cout<<”Employee cout<<”Employee

#”<<e.empno<<endl;#”<<e.empno<<endl;cout<<”Salary:”<<e.salary;cout<<”Salary:”<<e.salary;

}}

Today’s LectureToday’s Lecture

Structures and FunctionsStructures and Functions

Passing Structures (Objects) To Passing Structures (Objects) To FunctionsFunctions

Like ordinary variables and arrays, Like ordinary variables and arrays, structure object can also be passed to structure object can also be passed to functions.functions.

The syntax for passing Structure Objects The syntax for passing Structure Objects to functions is the same as ordinary to functions is the same as ordinary variable. The only difference is that the variable. The only difference is that the DataType in that case is replaced by The DataType in that case is replaced by The StructureName.StructureName.

Passing Structures (Objects) To Passing Structures (Objects) To FunctionsFunctions

Syntax (in case of ordinary variables):Syntax (in case of ordinary variables):ReturnType FunctionName(int a,int b) ReturnType FunctionName(int a,int b) {{}}Syntax (in case of structure objects):Syntax (in case of structure objects):ReturnType FunctionName(st a, st b)ReturnType FunctionName(st a, st b){{}}

Note:Note:Here Here stst is the name of the structure whose object is is the name of the structure whose object is being passed to the function.being passed to the function.Structure Objects are passed by value.Structure Objects are passed by value.

/*/*

Program Name: struct.cppProgram Name: struct.cpp

Author: NasirAuthor: Nasir

*/*/

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

struct empstruct emp

{{

int empno;int empno;

float salary;float salary;

};};

void show(emp t);void show(emp t);void main()void main(){{

emp e;emp e;cout<<"Enter employee number:”;cout<<"Enter employee number:”;cin>>e.empno;cin>>e.empno;cout<<”Enter salary:”;cout<<”Enter salary:”;cin>>e.salary;cin>>e.salary;cout<<”You have entered:”<<endl;cout<<”You have entered:”<<endl;show(e);show(e);

}}

void show(emp t)void show(emp t)

{{

cout<<”Employee #” << t.empno; cout<<”Employee #” << t.empno; cout<<endl;cout<<endl;

cout<<”Salary:”<<t.salary;cout<<”Salary:”<<t.salary;

}}

Returning Structure Variables Returning Structure Variables (Objects)(Objects)

Syntax (in case of ordinary variables):Syntax (in case of ordinary variables):ReturnType FunctionName(int a) ReturnType FunctionName(int a) {{

return variablename;return variablename;}}Syntax (in case of structure objects):Syntax (in case of structure objects):st FunctionName(st a, int b)st FunctionName(st a, int b){{

return objname;return objname;}}

Note:Note:Here Here stst is the name of the structure whose object is is the name of the structure whose object is being passed to the function.being passed to the function.Here Here objname objname is the name of the object of structure is the name of the object of structure stst

/*/*

Program Name: struct.cppProgram Name: struct.cpp

Author: NasirAuthor: Nasir

*/*/

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

struct empstruct emp

{{

int empno;int empno;

float salary;float salary;

};};

emp input(emp t);emp input(emp t);

void main()void main(){{

emp e;emp e;e=input(e);e=input(e);cout<<”You have entered:”<<endl;cout<<”You have entered:”<<endl;cout<<”Employee #” << e.empno; cout<<”Employee #” << e.empno; cout<<endl;cout<<endl;cout<<”Salary:”<<e.salary;cout<<”Salary:”<<e.salary;

}}

emp input(emp t)emp input(emp t){{

cout<<"Enter employee number:”;cout<<"Enter employee number:”;cin>>t.empno;cin>>t.empno;cout<<”Enter salary:”;cout<<”Enter salary:”;cin>>t.salary;cin>>t.salary;return t;return t;

}}

Questions ?????Questions ?????