2. Important Features of OOP
There are three major features of
Object Oriented Programming
1. Encapsulation: Achieved by dividing
a program into classes (similar to
structures)
2. Inheritance: Classes are derived
from other classes
3. Polymorphism: Achieved by use of
virtual functions and pointers
3. Structures …
A collection of related data items is
called structure
In array all elements are of same type,
unlike array the elements of structure
can be of different types
Structure is also known as record
(collection of related fields of different or
same type)
Structures are used in files processing
as a file is the collection of related
records (structures)
4. Defining Structure …
Defining names, types and number of data
items inside structure is called defining
structures
The struct keyword is used for defining a
structure
struct structurename
{
type a;
type b;
type c;
};
5. Defining Structures …
Example of structure
struct address
{
char name [20];
char city [15];
float age;
};
6. Structure Variable
Variables of structure data type are
declared for defined structures called
structure variables
Memory is reserved in computer
memory after declaration of structure
variable
The size of memory reserved is equal
to the size of each data member of
each variable
8. Accessing Members of a
Structure
The members of a structure are
accessed using dot operator (.)
Strucvariable.structelement;
struct address
{
char name[20];
char city[15];
float age;
};
main()
{
address var1;
cout<< “enter name, city and age”;
cin>>var1.name;
cin>>var1.city;
cin>>var1.age;
}
Refer to program struct.cpp
9. Structure …
Lab Assignment
Write a program to copy one structure
variable to another variable and then
print it on the screen without using
built-in function
10. Initialization of Structure
Variables
The values to a structure variable can be
assigned during its declaration time as
well (same like variable or array
initialization)
Actually, data members (usually
variables) are initialized that are used
inside structure
The order of data members and type
must be kept same in initialization with
the order and type as used inside
structure during definition
11. Initialization of Structure
Variables
Example of Structure variable initialization
struct address
{
char name [20];
float age;
};
address var1 = { “M. Ali”, 22};
The values to a structure variable are
assigned in curly braces separated by
comma
12. Structure Variables as Arrays
Structure variable can be also
declared as of an array type to
manage large number of records
(each element of the array represents
a complete record)Example of array structure variable
struct address
{
char name [20];
float age;
};
Address var[4]; //address var1, var2, var3, var4;
13. Initialization of Array of Structure
The data into an array of structure can
be initialized at the time of declaration
same like two dimensional array is
initialized
Example of initializing array structure variable
struct address
{
char name [20];
float age;
};
Address var[4] = {{ “M. Ali”, 22}, { “Saad”, 21}
{ “Kashif”, 23}, { “Rizwan”, 19.5}}
14. Structure …
Lab Assignment
Write a program to initialize a structure
array variable of size 4 with 3
members and then display all the
records