Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming In C++ Spring Semester 2013 Lecture 9 Programming In C++, Lecture 9 By Umer Rana.

Similar presentations


Presentation on theme: "Programming In C++ Spring Semester 2013 Lecture 9 Programming In C++, Lecture 9 By Umer Rana."— Presentation transcript:

1 Programming In C++ Spring Semester 2013 Lecture 9 Programming In C++, Lecture 9 By Umer Rana

2 Structures Programming In C++, Lecture 9 By Umer Rana A structure is a user defined data type. A structure is a collection of simple variables. Variables can be of different types. Data items in structure called members of the structure. A Structure consists of a number of data items, which no need to be of the same type but they grouped together. A Structure is a data type whose format is defined by the programmer

3 Structures-Declaring Programming In C++, Lecture 9 By Umer Rana Syntax struct structure-name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3;.. } ; struct class { int rollno; char name[25]; float totalmark; } ;

4 Structures-Defining Programming In C++, Lecture 9 By Umer Rana Once we declared the structure, we can define one or more variables to be structure type. struct class var1; Or struct class var1,var2,var3;

5 Accessing Structures Members Programming In C++, Lecture 9 By Umer Rana Structure use the “dot operator” (.), which is also called the “membership operator”.

6 Accessing Structures Members Programming In C++, Lecture 9 By Umer Rana struct class { int rollno; char name[25]; float totalmark; } ; struct class var1; var1.rollno=121; var1.name=‘A’; var1.totalmark=93.5;

7 Initializing Structures Members Programming In C++, Lecture 9 By Umer Rana struct class { int rollno; char name[25]; float totalmark; } ; struct class var1={121,’A’,93.5}; Structure members can be initialized at declaration, this much the same manner as the element of an array; The initial value must appear in the order in which they will be assigned to their corresponding structure members, enclosed in braces and separated by commas struct stucture_name var={val1,val2,val3…..};

8 Nested Structures Programming In C++, Lecture 9 By Umer Rana Structure that contain other structure. struct stud_Res {int rno; char std[10]; struct stud_Marks { char subj_nm[30]; int subj_mark; }marks; }result; int main() {printf("\n\t Enter Roll Number : "); scanf("%d",&result.rno); printf("\n\t Enter Standard : "); scanf("%s",result.std); printf("\n\t Enter Subject Code : "); scanf("%s",result.marks.subj_nm); printf("\n\t Enter Marks : "); scanf("%d",&result.marks.subj_mark); printf("\n\n\t Roll Number : %d",result.rno); printf("\n\n\t Standard : %s",result.std); printf("\nSubject Code : %s",result.marks.subj_nm); printf("\n\n\t Marks : %d",result.marks.subj_mark); getch(); }

9 Passing Structures to Function Programming In C++, Lecture 9 By Umer Rana Same way that it can be passed in an assignment statement, the value of a structure variable can also be passed as a parameter to a function. void printRec(struct data); struct data { float amount=10.20; string fname=“Test String”; string lname=“Preston”; } rec; printRec(rec); void printRec(struct data x) { printf(“Amount is %f”,x.amount); printf(“Fname is %s”,x.fname); printf(“Iname is %s”,x.Iname); }

10 Arrays Of Structures Programming In C++, Lecture 9 By Umer Rana It is possible to store a structure as an array element. i.e., an array in which each element is a structure. Although a structure contains many different types, the compiler never gets to know this information because it is hidden away inside a sealed structure capsule, so it can believe that all the elements in the array have the same type, even though that type is itself made up of lots of different types.

11 Arrays Of Structures Programming In C++, Lecture 9 By Umer Rana struct student { int rollno; char name[25]; float totalmark; } stud[100]; In this declaration stud is a 100- element array of structures. Hence, each element of stud is a separate structure of type student. An array of structure can be assigned initial values just as any other array. So the above structure can hold information of 100 students.

12 Arrays Of Structures Programming In C++, Lecture 9 By Umer Rana void main() { struct student { int rollno; char name[25]; int totalmark; }stud[100]; int n,i; printf("Enter total number of students\n\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter details of %d-th student\n",i+1); printf("Name:\n"); scanf("%s",&stud[i].name); printf("Roll number:\n"); scanf("%d",&stud[i].rollno); printf("Total mark:\n"); scanf("%d",&stud[i].totalmark); } printf("STUDENTS DETAILS:\n"); for(i=0;i<n;i++) { printf("\nRoll number:%d\n",stud[i].rollno); printf("Name:%s\n",stud[i].name); printf("Totel mark:%d\n",stud[i].totalmark); } }

13 TypeCasting Programming In C++, Lecture 9 By Umer Rana Type casting is a way to make a variable of one type to an other type. int main() { printf( "%c\n", (char)65 ); printf( "%d\n", (int)'A' ); getchar(); }

14 Some More C Function Programming In C++, Lecture 9 By Umer Rana Sizeof(): This function take a data type as an argument and returns the size in bytes that the data type occupies. e.g. Int a; printf( "%d\n", sizeof(a) ); output is 4. Sqrt(): This function return a square root, but argument sent to this function must be of type double. double x = 2.0, result; result = sqrt(x); printf("The square root of %1f is %1fn", x, result);

15 Union Programming In C++, Lecture 9 By Umer Rana Similar to a struct, but all members share a single memory location, and only one member of the union can be used at a time Declared using union, otherwise the same as struct Variables defined as for struct variables Allocates memory at declaration time Provides a way to look at the same data in several different ways. Uses only one memory location.

16 Union Programming In C++, Lecture 9 By Umer Rana Union is user defined data type used to stored data under unique variable name at single memory location. Union is similar to that of structure. Syntax of union is similar to structure. But the major difference between structure and union is 'storage.' In structures, each member has its own storage location, whereas all the members of union use the same location. Union contains many members of different types, it can handle only one member at a time. To declare union data type, 'union' keyword is used. Union holds value for one data type which requires larger storage among their members.

17 Union Programming In C++, Lecture 9 By Umer Rana MEMORY ALLOCATION :

18 Union Programming In C++, Lecture 9 By Umer Rana Syntax: union union_name { element 1; element 2; element 3; }union_variable; Example: union techno { int comp_id; char name; float sal; }tch;

19 Union Programming In C++, Lecture 9 By Umer Rana union techno { int id; char nm[50]; }tch; int main() { //clrscr(); printf("\n\t Enter developer id : "); scanf("%d", &tch.id); printf("\n\n\t Enter developer name : "); scanf("%s", tch.nm); printf("\n\n Developer ID : %d", tch.id); printf("\n\n Developed By : %s", tch.nm); getch(); }

20 ROM BIOS Programming In C++, Lecture 9 By Umer Rana Set of built-in routines written in assembly language and were designed to be called by assembly language programs. These routines are a permanent part of the machine. Our C programs can make use of these routines to perform a variety of input/output activities.

21 Advantages of Using ROM BIOS Programming In C++, Lecture 9 By Umer Rana Mostly handle input/output operations like keyboard, printer, diskette drives, user defined devised, the serial port etc… Most important in Graphics The Largest category of routines deals with video display.

22 Accessing the ROM BIOS Programming In C++, Lecture 9 By Umer Rana C compiler working on the PC generally provide a method to access these routines. When we use C to call a BIOS routine, the process is somewhat different. Instead of values being placed in an area of memory, they are placed in hardware devices called registers. Registers are the heart of the microprocessor, they are used to perform arithmetic and many other operations. There are a number of registers in the microprocessor; the ones we will be most concerned are four registers, AX,BX,CX and DX, consist of two bytes. Unlike C variables, however, the registers are fixed; they are always there, and they always have the same names.

23 Dual Role of Registers Programming In C++, Lecture 9 By Umer Rana Another difference between C variables and registers is that registers can be accessed in two different ways: either as four two-byte registers, or as eight one-byte registers as shown in Figure (AX,BX,CX and DX). one byte AL register BL register CL register DL register AH register BH register CH register DH register Registers as One-Byte Storage

24 Interrupt Numbers Programming In C++, Lecture 9 By Umer Rana The ROM BIOS routines are accessed through interrupts. [ An interrupt provides access to a group of ROM BIOS routines. The mechanism used to access a ROM BIOS routine is a C library function called int86( ). The “int” stand for “interrupt” and the “86” refers to the 80x86 family of chips in processor.

25 Finding the Memory Size Programming In C++, Lecture 9 By Umer Rana This function takes the interrupt number and two union variables as arguments. The first union represents the values in the registers being sent to the ROM routine, and the second represents the values in the registers being returned from the ROM routine to the C program. int86(INT, &inregs, &outregs)

26 Making the Cursor Disappear Programming In C++, Lecture 9 By Umer Rana You can make the cursor vanish if you set bit 5, in the byte placed in the CH register, to 1. What do we mean by bit 5? The bits in every byte are numbered from 0 to 7, with the last significant bit being 0. To set bit 5 on, we can place the hex number 20 in the CH register. Hex 20 is 00100000, which is a just configuration we need.

27 Setting The Cursor Size… Programming In C++, Lecture 9 By Umer Rana On the monochrome screen, the cursor consist of 14 short horizontal lines, numbered from 0 to 13 (reading from top to bottom). The default cursor, the one you are most used to seeing, uses only two of these lines, 12 and 13, at the bottom of the cursor position. To redefine the size of the cursor, we call the BIOS routine at interrupt 1(hex), with the AH register containing 1. We also place the starting cursor line number in the CH register, and the ending line number in the CL register.


Download ppt "Programming In C++ Spring Semester 2013 Lecture 9 Programming In C++, Lecture 9 By Umer Rana."

Similar presentations


Ads by Google