Programming In C++

Download Report

Transcript Programming In C++

Programming In C++
Spring Semester 2013
Lecture 9
Programming In C++, Lecture 9 By Umer Rana
Structures
•
•
•
•
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
Programming In C++, Lecture 9 By Umer Rana
Structures-Declaring
Syntax
struct structure-name
{
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
};
Programming In C++, Lecture 9 By Umer Rana
struct class
{
int rollno;
char name[25];
float totalmark;
};
Structures-Defining
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;
Programming In C++, Lecture 9 By Umer Rana
Accessing Structures Members
Structure use the “dot operator” (.), which is
also called the “membership operator”.
Programming In C++, Lecture 9 By Umer Rana
Accessing Structures Members
struct class
{
int rollno;
char name[25];
float totalmark;
};
struct class var1;
var1.rollno=121;
var1.name=‘A’;
var1.totalmark=93.5;
Programming In C++, Lecture 9 By Umer Rana
Initializing Structures Members
• 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…..};
struct class
{
int rollno;
char name[25];
float totalmark;
};
struct class var1={121,’A’,93.5};
Programming In C++, Lecture 9 By Umer Rana
Nested Structures
Structure that contain other structure.
struct stud_Res
{
int rno;
int main()
char std[10];
{
printf("\n\t Enter Roll Number : ");
scanf("%d",&result.rno);
struct stud_Marks
printf("\n\t Enter Standard : ");
{
scanf("%s",result.std);
char subj_nm[30];
printf("\n\t Enter Subject Code : ");
int subj_mark;
scanf("%s",result.marks.subj_nm);
}marks;
printf("\n\t Enter Marks : ");
}result;
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();
}
Programming In C++, Lecture 9 By Umer Rana
Passing Structures to Function
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
{
void printRec(struct data x)
float amount=10.20;
{
string fname=“Test String”;
printf(“Amount is %f”,x.amount);
printf(“Fname is %s”,x.fname);
string lname=“Preston”;
printf(“Iname is %s”,x.Iname);
} rec;
}
printRec(rec);
Programming In C++, Lecture 9 By Umer Rana
Arrays Of Structures
• 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.
Programming In C++, Lecture 9 By Umer Rana
Arrays Of Structures
struct student
{
int rollno;
char name[25];
float totalmark;
} stud[100];
Programming In C++, Lecture 9 By Umer Rana
In this declaration stud is a 100element 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.
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);
Programming In C++, Lecture 9}By Umer Rana
Arrays Of Structures
TypeCasting
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();
}
Programming In C++, Lecture 9 By Umer Rana
Some More C Function
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);
Programming In C++, Lecture 9 By Umer Rana
Union
• 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.
Programming In C++, Lecture 9 By Umer Rana
Union
• 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.
Programming In C++, Lecture 9 By Umer Rana
Union
MEMORY ALLOCATION :
Programming In C++, Lecture 9 By Umer Rana
Union
Syntax:
union union_name
{
<data-type> element 1;
<data-type> element 2;
<data-type> element 3;
}union_variable;
Example:
union techno
{
int
comp_id;
char
name;
float sal;
}tch;
Programming In C++, Lecture 9 By Umer Rana
Union
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();
}
Programming In C++, Lecture 9 By Umer Rana
ROM BIOS
• 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.
Programming In C++, Lecture 9 By Umer Rana
Advantages of Using ROM BIOS
• 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.
Programming In C++, Lecture 9 By Umer Rana
Accessing the ROM BIOS
• 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.
Programming In C++, Lecture 9 By Umer Rana
Dual Role of Registers
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
one byte
AH register
AL register
BH register
BL register
CH register
CL register
DH register
DL register
Registers as One-Byte Storage
Programming In C++, Lecture 9 By Umer Rana
Interrupt Numbers
• 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.
•
[
Programming In C++, Lecture 9 By Umer Rana
Finding the Memory Size
• 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)
Programming In C++, Lecture 9 By Umer Rana
Making the Cursor Disappear
• 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.
Programming In C++, Lecture 9 By Umer Rana
Setting The Cursor Size…
• 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.
Programming In C++, Lecture 9 By Umer Rana