1 / 25

Structures and Functions

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 to proceed without programming.

neron
Télécharger la présentation

Structures and Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structures and Functions Ghulam Nasir Khan

  2. Important /\/otes • Programming is a fundamental course of IT as well as BICSE, so it will be very difficult to proceed without programming. • You will be thought different courses on programming throughout your course of study such as OOP using C++, Data Structures, Algorithms, Assembly Language, Web Designing so there is no way out. • The key point is try to develop interest in programming and try to practice as much as you can.

  3. Important /\/otes • Try to clear all of your concepts and practice as much as possible you that can make a good understanding of programming • Do not let the instructor proceed, even if you have a small confusion in mind. • Try to learn as much programming as you can in this semester so that you do not have to cry over spilt milk. BEST OF LUCK

  4. An Overview of Structures

  5. What is Structure • Structure is used to store data of same or different type. It is used to group data that may be of the same or different types. • Basically it is used to store data that has a logical relation e.g. record of a student etc. • It can also be said that structure is used to create a new data type comprising of the existing data types.

  6. Composition of Structures • Structures are composed of • Data Members • Member Functions • Data Members are memory locations that are used to store data e.g. variables, arrays etc. • Member Functions are used to manipulate the Data Members.

  7. Memory !!!! • How much memory is occupied by Structures? • 1 Byte? • 4 Bytes? • 8 Bytes? • N Bytes? (Specify the value of N) • The answer is • Zero Bytes

  8. Structure Variable • The representation of Structure is memory is called Structure Variable or Structure Object or simply Object. • Object occupies space in the memory and object actually stores the data.

  9. Memory !!!! • How much memory is occupied by Structure Object? • 0 Byte? • 1 Byte? • 4 Bytes? • 8 Bytes? • 16 Bytes? • N Bytes? (Specify the value of N)

  10. Memory !!!! • The answer is • The sum of memory occupied by all the data members of a structure. • For Example, all the objects of a structure having two integer, one float and one character data member will occupy 4 * 2 + 4 + 1 = 8 + 4 + 1 = 13 Bytes

  11. Declaring a Structure struct StructureName { DataType variable 1; DataType variable 2; . . DataType variable n; };

  12. Creating Structure Object • struct StructureName objname; • StructureName objname; • Here objname is a user defined name of the object. It must follow Variable Naming Rules.

  13. /* Program Name: struct.cpp Author: Nasir */ #include<iostream.h> struct emp { int empno; float salary; };

  14. void main() { emp e; cout<<"Enter employee number:”; cin>>e.empno; cout<<”Enter salary:”; cin>>e.salary; cout<<”You have entered:”<<endl; cout<<”Employee #”<<e.empno<<endl; cout<<”Salary:”<<e.salary; }

  15. Today’s Lecture Structures and Functions

  16. Passing Structures (Objects) To Functions • Like ordinary variables and arrays, structure object can also be passed to functions. • The syntax for passing Structure Objects to functions is the same as ordinary variable. The only difference is that the DataType in that case is replaced by The StructureName.

  17. Passing Structures (Objects) To Functions • Syntax (in case of ordinary variables): ReturnTypeFunctionName(inta,int b) { } • Syntax (in case of structure objects): ReturnTypeFunctionName(st a, st b) { } Note: • Here st is the name of the structure whose object is being passed to the function. • Structure Objects are passed by value.

  18. /* Program Name: struct.cpp Author: Nasir */ #include<iostream.h> struct emp { int empno; float salary; };

  19. void show(emp t); void main() { emp e; cout<<"Enter employee number:”; cin>>e.empno; cout<<”Enter salary:”; cin>>e.salary; cout<<”You have entered:”<<endl; show(e); }

  20. void show(emp t) { cout<<”Employee #” << t.empno; cout<<endl; cout<<”Salary:”<<t.salary; }

  21. Returning Structure Variables (Objects) • Syntax (in case of ordinary variables): ReturnType FunctionName(int a) { return variablename; } • Syntax (in case of structure objects): st FunctionName(st a, int b) { return objname; } Note: • Here st is the name of the structure whose object is being passed to the function. • Here objname is the name of the object of structure st

  22. /* Program Name: struct.cpp Author: Nasir */ #include<iostream.h> struct emp { int empno; float salary; };

  23. emp input(emp t); void main() { emp e; e=input(e); cout<<”You have entered:”<<endl; cout<<”Employee #” << e.empno; cout<<endl; cout<<”Salary:”<<e.salary; }

  24. emp input(emp t) { cout<<"Enter employee number:”; cin>>t.empno; cout<<”Enter salary:”; cin>>t.salary; return t; }

  25. Questions ?????

More Related