Object Oriented Programming

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

CSE 1203

OBJECT ORIENTED PROGRAMMING


Data Types in C

 Which data types are known to you till now?


 Can you create a new data type?
Custom Data type in C

 Structures
 Union
 Bit-field
 Enumeration
 typedef
Structures

 A structure is a collection of variables referenced under one name,


providing a convenient means of keeping related information together.
 The keyword struct tells the compiler that a structure is being declared.
 Structure declaration is terminated by a semicolon because
declaration of structure is a statement.
 From the code on right side, only a structure has been
declared, no variable has been created yet. The form of
data has been defined only.
Structures (declaring variables)

 To declare a variable, of type addr,

 This declares a variable(object) of type addr named addr_info.


 You can also declare one or more objects when you declare a structure.
Structures (declaring variable)

addr_info structure in memory


Structures (declaring variable)

 General form of a structure declaration

 If you need only one variable you can skip the stuct tag.
Stuctures (How to use)

 Individual members of a structure are accessed through the use of the .


operator (usually called the dot operator)
 The following statement assigns the ZIP code 12345 to the zip field of
variable addr_info

 To print the ZIP code on the screen


Array of Structures

 To declare an array of structures, first define a structure and then declare


an array variable of that type. For example, to declare a 100-element
array of structures of type addr defined earlier, write

 To access a specific structure, index the array name. For example, to print
the ZIP code of structure 3, write
Passing structures to functions

 Any member of a structure can be passed to a function

 Entire structure can be passed to a function


Structure Pointers

 C allows pointers to structures just like pointer to any other type of object.
 Structure pointers are declared by placing * in front of a structure
variable's name

 For example,
To place the address of person
into pointer p
Structure pointer

 To access the members of a structure using a pointer to that structure, you


must use the –> operator. For example, this references the balance field:
p–>balance

 The –>, usually called the arrow operator, consists of the minus sign
followed by a greater than sign.
 The arrow is used in place of the dot operator when you are accessing a
structure member through a pointer to the structure.
Unions

 A union is a memory location that is shared by two or more different types


of variables.
 A union provides a way of interpreting the same bit pattern in two or more
different ways.
 General form is like structure
Bit-Fields

 C has a built-in feature, called a bit-field, that allows you to access a single
bit
 Why to use?
 If storage is limited, you can store several Boolean (true/false) variables in one
byte
 Certain devices transmit status information encoded into one or more bits within
a byte
 Certain encryption routines need to access the bits within a byte
Bit-Fields
Enumeration

 An enumeration is a set of named integer constants

 For example, here penny =0, nickel =1, dime =2 …

 You can also change the values, here penny=0, nickel=1, dime =2, quarter
= 100, half_dollar = 101, dollar = 102
Typedef

 You can define new data type names by using the keyword typedef

 For example, to give float a new name balance use this

 Following statement will create a float variable named over_due

 The following will set a new name for balance which is another name for float
Thank you!

You might also like