Data Structure and Algorithm: CIT231 Lecture 4: ADT and Introduction to Object Oriented Programming (OOP) DeSiaMore www.desiamore.com/ifm DeSiaMore www.desiamore.com/ifm 1
Dec 29, 2015
Data Structure and Algorithm: CIT231
Lecture 4: ADT and Introduction to Object Oriented Programming (OOP)
DeSiaMorewww.desiamore.com/ifm
DeSiaMore www.desiamore.com/ifm 1
Introduction to Object Oriented Programming
In this lecture you will learn the general concepts of Object Oriented Programming (OOP). Inheritance Encapsulation Polymorphism
Object Oriented Programming is very powerful and important programming paradigm.
DeSiaMore www.desiamore.com/ifm 2
Introduction to Object Oriented Programming
Most of the nowadays sophisticated application software have been developed in Objected Oriented languages and mostly in C++.
E.g. Word processors, spreadsheets, and graphics applications.
Also some Operating Systems are written in OO languages.
DeSiaMore www.desiamore.com/ifm 3
Fundamental terms and Principles of the OOP
Attribute – The data for a class that maintains the current state of an object. The state of an object is determined by the current contents of all the attributes.
Object – An object is a something that exists and is identifiable. An object exhibits behaviour and maintains state. Example of objects are telephones, automobiles, buildings, animals, etc.
DeSiaMore www.desiamore.com/ifm 4
Fundamental terms and Principles of the OOP
Class – Class is a synonymous with type. A class specifies a traits (data) and behaviour that an object can exhibit. A class itself does not exists. It is only a
description of an object. A blueprint is analogous to a class and building itself is the object.
A class can be considered a template for the creation of objects.
DeSiaMore www.desiamore.com/ifm 5
Fundamental terms and Principles of the OOP
Inheritance – This is the relationship of classes. There is an explicit is-a relationship between classes. For example an automobile is-a vehicle, a zebra is-a mammal, a flower is-a plant, and so on.
Interface – The visible functionality of a class. Is the contract an object makes with users of an object. Users manipulate an object through its interface.
DeSiaMore www.desiamore.com/ifm 6
Fundamental terms and Principles of the OOP
- Interface of a class is the part the clients see. An interface is considered a contract the class has with its client.
- For example an automobile has an interface (contract) with its driver. An automobile’s interface includes a steering wheel, brake pedals, speedometer, and a clutch.
- This interface provides functionality to the driver without the need to know the inner workings of the automobile.
DeSiaMore www.desiamore.com/ifm 7
Fundamental terms and Principles of the OOP
Implementation – The internal functionality and attributes of a class. A class’s implementation is hidden from users of the class.
Encapsulation – Encompasses the interfaces and abstraction of a class.
Abstraction – The generalisation of a class that distinguish it from other classes
DeSiaMore www.desiamore.com/ifm 8
Fundamental terms and Principles of the OOP
Understanding inheritance is a good starting point of learning object orientation.
The concept of inheritance can be understood in analog to your family tree.
The procedure to describe your family tree is the same to describe class inheritance in C++.
DeSiaMore www.desiamore.com/ifm 9
Fundamental terms and Principles of the OOP
Inheritance is known as an is-a relationship. Example a toyota is-a vehicle, a snake is-a reptile, and a flower is-a plant.
To begin applying inheritance, you must decide a base class that must reside within your application. It provides basic, default functionality to users of the class.
DeSiaMore www.desiamore.com/ifm 10
Access Specifier of the class
The class in C++ contains three important access specifiers which provide conditions to access to the data members and functions (methods) of the class.
These are Public Private Protected
DeSiaMore www.desiamore.com/ifm 11
Access Specifier of the classPublic – A class member with public
visibility is accessible outside of the class.Private – A class member with private
visibility can only be accessible by member functions of the class.
Protected – A class member with protected visibility is accessible by member functions of the class and its descendents.
DeSiaMore www.desiamore.com/ifm 12
MATRIX Operation with the use of Class #include <iostream.h> Const int MAX = 3; Class Matrix { Private:
Int mat[MAX][MAX]; Public: Matrix(); void create (); void display (); };
DeSiaMore www.desiamore.com/ifm 13
Initialise Matrix()
Matrix::Matrix()
{
for(int i=0; i<MAX; i++)
{
for (int j=0; j<MAX; j++)
mat[i][j]=0;
}
}DeSiaMore www.desiamore.com/ifm 14
Create matrix()//Create matrix matVoid Matrix::create(){
int n; for (int i=0; i<MAX; i++)
{ for (int j = 0; j<MAX; j++) { cout<<“Enter the element: ”;
cin>>n; mat[i][j] = n; } }}
DeSiaMore www.desiamore.com/ifm 15
Display matrix()
//displays the contents of the matrixVoid Matrix::display();{ for (int i=0; i<MAX; i++) { for (int j=0; j<MAX; j++) cout<<mat[i][j]<<“ ”;
cout<<endl; }}DeSiaMore www.desiamore.com/ifm 16
Create the object of Matrix in the Main program
void main()
{
matrix mat1;
cout<<"\nEnter the elements of the array: \n";
mat1.create();
cout<<"Your array is:\n";
mat1.display();
}
DeSiaMore www.desiamore.com/ifm 17
Pointers A pointer is a variable which stores the
address of another variable. There are two important operators when
working with pointers in C++: the address of (&) operator the value of (*) operator
How much storage space does a pointer consume? Use sizeof(ptr) without the '*‘ operator to determine the memory utilised on your system
DeSiaMore www.desiamore.com/ifm 18
Pointers
Irrespective of datatype or whether the pointer points to a single variable or array, as a general rule, pointers must use the same amount of memory space.
The & operator gives us the address of a variable and * gives us the value of a variable at a specified address. Example:-
DeSiaMore www.desiamore.com/ifm 19
Example of the use of * and & #include <iostream.h> #include <stdlib.h>
int main() { int i = 10;
cout << "The value of variable i is " << i << "\n";
cout << "The memory address of i is " << &i << "\n"; /* Prints the memory address in hexadecimal format
*/
cout << "The value of variable i using * operator is "<< *(&i) <<"\n"; /* The * operator gives the value when provided with a memory address. Note that the dsta type is inferred from the variable name. */ system("PAUSE"); return 0; }
DeSiaMore www.desiamore.com/ifm 20
Pointer Example #include <iostream.h> #include <stdlib.h>
int main() { int i = 10; int *x = &i; int *y; /* x stores the address of variable i. Pointer assignment could also be done as */ y = &i;
cout <<"The pointer x is stored at the mempory address "<< &x << "\n"; cout <<"The pointer x stores the memory address of i: " << y << "\n";
/* Contrast the difference between the memory address of the pointer and the memory address it stores. */
cout<< "The value of i accessed through pointer x is " << *x << "\n";
/* Now we manipulate the value of i using pointer x; */
*x = *x + 1; // increment i by 1
cout<< "i (through pointer) = " << *x << " which equals i (direct access) " << i << "\n"; /* A pointer does not create a copy of the variable it points to. */
cout<<"The memory allocated to the pointer x is " << sizeof(x) << " bytes. "; system("pause"); return 0;
}
DeSiaMore www.desiamore.com/ifm 21
Array as Pointers
In C++ array starts at position 0. The elements of the array occupy adjacent
locations in memory. C++ treats the name of the array as if it was
the pointer to the first element. If v is an array, *v is the same thing as v[0],
*(v+1) is the same thing as v[1] as diagram below shows:
DeSiaMore www.desiamore.com/ifm 22
Array as Pointers
Pointer use for an array
DeSiaMore www.desiamore.com/ifm 23
Exercise
Using matrix arrays, write a program to create two matrices and then create methods of adding and subtracting those matrices.
DeSiaMore www.desiamore.com/ifm 24
Next Topic
String ADT and array of characters
DeSiaMore www.desiamore.com/ifm 25