1 / 25

Data Structure and Algorithm: CIT231

Data Structure and Algorithm: CIT231. Lecture 4: ADT and Introduction to Object Oriented Programming (OOP) DeSiaMore www.desiamore.com/ifm. Introduction to Object Oriented Programming. In this lecture you will learn the general concepts of Object Oriented Programming (OOP). Inheritance

lamont
Download Presentation

Data Structure and Algorithm: CIT231

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structure and Algorithm: CIT231 Lecture 4: ADT and Introduction to Object Oriented Programming (OOP) DeSiaMore www.desiamore.com/ifm www.desiamore.com/ifm

  2. 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. www.desiamore.com/ifm

  3. 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. www.desiamore.com/ifm

  4. 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. www.desiamore.com/ifm

  5. 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. www.desiamore.com/ifm

  6. 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. www.desiamore.com/ifm

  7. 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. www.desiamore.com/ifm

  8. 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 www.desiamore.com/ifm

  9. 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++. www.desiamore.com/ifm

  10. 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. www.desiamore.com/ifm

  11. 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 www.desiamore.com/ifm

  12. Access Specifier of the class • Public – 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. www.desiamore.com/ifm

  13. 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 (); • }; www.desiamore.com/ifm

  14. Initialise Matrix() Matrix::Matrix() { for(int i=0; i<MAX; i++) { for (int j=0; j<MAX; j++) mat[i][j]=0; } } www.desiamore.com/ifm

  15. Create matrix() //Create matrix mat Void 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; } } } www.desiamore.com/ifm

  16. Display matrix() //displays the contents of the matrix Void Matrix::display(); { for (int i=0; i<MAX; i++) { for (int j=0; j<MAX; j++) cout<<mat[i][j]<<“ ”; cout<<endl; } } www.desiamore.com/ifm

  17. 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(); } www.desiamore.com/ifm

  18. 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 www.desiamore.com/ifm

  19. 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:- www.desiamore.com/ifm

  20. 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; • } www.desiamore.com/ifm

  21. 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; • } www.desiamore.com/ifm

  22. 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: www.desiamore.com/ifm

  23. Array as Pointers • Pointer use for an array www.desiamore.com/ifm

  24. Exercise • Using matrix arrays, write a program to create two matrices and then create methods of adding and subtracting those matrices. www.desiamore.com/ifm

  25. Next Topic • String ADT and array of characters www.desiamore.com/ifm

More Related