computer programming lecture – 41 to 42

Download Computer programming lecture – 41 to 42

Post on 08-Jul-2015

112 views

Category:

Education

4 download

Embed Size (px)

DESCRIPTION

Computer programming lecture 41 to 42

TRANSCRIPT

  • 1. Lecture 41 to 42Computer Programming14 Computer Systems Engineering Second SemesterBy: Mr. Ali Asghar Manjotho, Lecturer, CSE-MUET

2. ContentsStructure (LL 02)Defining Structure in C++ (LL 04)Defining Car Structure in C++ (LL 04)Defining Point Structure in C++ (LL 04)Defining Time Structure in C++ (LL 04)Declaring Structure Variables in C++ (LL 04)Initializing Structure Variables in C++ (LL 04)Accessing Structure Members in C++ (LL 04)Structure of Structure in C++ (LL 04)Rectangle Structure in C++ (LL 04)Program Examples (LL 04)LL 02 = Learning Level 02 Comprehension, LL 04 = Learning Level 04 AnalysisAli Asghar Manjotho, Lecturer CSE-MUET 2 3. StructureSingle variable can store only one value at a time.Single array can store multiple values at a time but they all must be of same data type.What if you want to store the data which is collection of different data types.In this case neither variable nor array is helpful.Here comes the role of a structure.Ali Asghar Manjotho, Lecturer CSE-MUET 3 4. StructureStructure is the collection of heterogeneous data items unlike an array.Structure can also be defined as a collection of a fixed number of components in which the components are accessed by name. The components may be of different types.All the data items in a structure may or may not be of same data type.There are some data items which are group of multiple values instead of a single value.Ali Asghar Manjotho, Lecturer CSE-MUET 4 5. StructureQ: In what data type of a variable you will store a time?A: As the time is not a single valued item. It has three integer values i.e. Hours, Minutes and Seconds. Hence we create a structure of three integer variables to store time.Time{int hours;int minutes;int seconds;}Ali Asghar Manjotho, Lecturer CSE-MUET 5 6. StructureQ: In what data type of a variable you will store a point?A: As the point is not a single valued item. It has two integer values i.e. X, and Y. Hence we create a structure of two integer variables to store a point.Point{int x;int y;}Ali Asghar Manjotho, Lecturer CSE-MUET 6 7. StructureQ: We want to store the data about a car, the data contains the information about the name, color, company, max speed and transition mode of the car. In what data type of a variable you will store all the information of car?A: As the car data is not a single valued item. It has five values i.e. Name, Company, Color, Maximum Speed and Transition mode (Automatic = 0 or Manual = 1). Hence we create a structure of five variables to store car.Car{string name;string company;string color;float speed;bool transition;}Ali Asghar Manjotho, Lecturer CSE-MUET 7 8. Defining Structure in C++In C++ the structure is defined by the keyword structfollowed by the name of the structure.Then we specify all the variables of different data types, which constitute to form a structure.All the variables inside the structure are called as structure members.Finally the definition is terminated by the semicolon.Consider the Carstructure:Ali Asghar Manjotho, Lecturer CSE-MUET 8 9. Defining Car Structure in C++Ali Asghar Manjotho, Lecturer CSE-MUET 9struct Car{string name;string company;string color;float maxspeed;bool transition;};Structure NameStructure Members 10. Defining Point Structure in C++Ali Asghar Manjotho, Lecturer CSE-MUET 10struct Point{int x;int y;}; 11. Defining Time Structure in C++Ali Asghar Manjotho, Lecturer CSE-MUET 11struct Time{int hours;int minutes;int seconds;}; 12. Declaring Structure Variables in C++Once you have created a structure, it can be treated as new data type.You can create the variables of the structure in the same way as you create the variables for built-in data types like; int, float, char etc.A structure variable can be declared by just writing the name of the structure followed by the variable name.Ali Asghar Manjotho, Lecturer CSE-MUET 12 13. Declaring Structure Variables in C++Ali Asghar Manjotho, Lecturer CSE-MUET 13Car mycar ;Point p1 ;Time now ;Variable NameStructure Name 14. Initializing Structure Variables in C++While initializing the structure variable we will also provide the values of each of the member of the structure.All the members values are placed in curly brackets { } , each value separated by comma ( , ).The order of the values does matter.Ali Asghar Manjotho, Lecturer CSE-MUET 14 15. Initializing Structure Variables in C++Ali Asghar Manjotho, Lecturer CSE-MUET 15Car mycar = {"Corolla","Honda","Maroon",350.5,0};Point p1 = {5,3};Time now = {8,35,14}; 16. Accessing Structure Members in C++Once structure is defined and its variable is created, we can access the individual members of the structure with the help of dot operator ( . ).The dot operator is also called as member access operator.Ali Asghar Manjotho, Lecturer CSE-MUET 16 17. Accessing Structure Members in C++Ali Asghar Manjotho, Lecturer CSE-MUET 17Point p1;p1.x = 10;p1.y = 14;cout