Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Data Structure and Algorithm: CIT231 Lecture 4: ADT and Introduction to Object Oriented Programming (OOP) DeSiaMore www.desiamore.com/ifm DeSiaMorewww.desiamore.com/ifm1."— Presentation transcript:

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

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. DeSiaMorewww.desiamore.com/ifm2

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. DeSiaMorewww.desiamore.com/ifm3

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. DeSiaMorewww.desiamore.com/ifm4

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. DeSiaMorewww.desiamore.com/ifm5

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. DeSiaMorewww.desiamore.com/ifm6

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. DeSiaMorewww.desiamore.com/ifm7

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 DeSiaMorewww.desiamore.com/ifm8

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++. DeSiaMorewww.desiamore.com/ifm9

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. DeSiaMorewww.desiamore.com/ifm10

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 DeSiaMorewww.desiamore.com/ifm11

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. DeSiaMorewww.desiamore.com/ifm12

13 MATRIX Operation with the use of Class #include Const int MAX = 3; Class Matrix { Private: Int mat[MAX][MAX]; Public: Matrix(); void create (); void display (); }; DeSiaMorewww.desiamore.com/ifm13

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

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; } DeSiaMorewww.desiamore.com/ifm15

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; } DeSiaMorewww.desiamore.com/ifm16

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(); } DeSiaMorewww.desiamore.com/ifm17

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 DeSiaMorewww.desiamore.com/ifm18

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:- DeSiaMorewww.desiamore.com/ifm19

20 Example of the use of * and & #include 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; } DeSiaMorewww.desiamore.com/ifm20

21 Pointer Example #include 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; } DeSiaMorewww.desiamore.com/ifm21

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: DeSiaMorewww.desiamore.com/ifm22

23 Array as Pointers Pointer use for an array DeSiaMorewww.desiamore.com/ifm23

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

25 Next Topic String ADT and array of characters DeSiaMorewww.desiamore.com/ifm25


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

Similar presentations


Ads by Google