Lecture 2

15,976
-1

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
15,976
On Slideshare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
150
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Lecture 2

  1. 1. Basic Concepts of OOPs Lecture 2 Object Oriented Programming with C++
  2. 2. Basic Concepts of Object-Oriented Programming <ul><li>Objects </li></ul><ul><li>Classes </li></ul><ul><li>Data abstraction and encapsulation </li></ul><ul><li>Inheritance </li></ul><ul><li>Polymorphism </li></ul><ul><li>Dynamic binding </li></ul><ul><li>Message passing </li></ul>
  3. 3. Basic Concepts of Object-Oriented Programming <ul><li>Objects </li></ul><ul><li>Classes </li></ul><ul><li>Data abstraction and encapsulation </li></ul><ul><li>Inheritance </li></ul><ul><li>Polymorphism </li></ul><ul><li>Dynamic binding </li></ul><ul><li>Message passing </li></ul>
  4. 4. Starting with C++ classes <ul><li>C with Classes </li></ul>
  5. 5. Classes and Objects <ul><li>A class is a definition of an object. </li></ul><ul><li>It's a type just like int. </li></ul><ul><li>A class resembles a struct with just one difference : </li></ul><ul><ul><li>All struct members are public by default. </li></ul></ul><ul><ul><li>All classes members are private by default. </li></ul></ul>
  6. 6. Remember : <ul><li>A class is a type </li></ul><ul><li>object of this class is just a variable </li></ul>
  7. 7. Definition of a class <ul><li>class name { // members } </li></ul>
  8. 8. A Simple Class <ul><li>class item </li></ul><ul><li>{ </li></ul><ul><li>int number; // variables declaration </li></ul><ul><li>float cost; // private by default </li></ul><ul><li>Public: </li></ul><ul><li>void getdata(int a, float b); // functions declaration </li></ul><ul><li>void putdata(void); // using prototype </li></ul><ul><li>}; // ends with semicolon </li></ul>
  9. 9. What is Object Oriented Programming? <ul><li>An object is like a black box. </li></ul><ul><li>The internal details are hidden. </li></ul><ul><li>Identifying objects and assigning responsibilities to these objects. </li></ul><ul><li>Objects communicate to other objects by sending messages . </li></ul><ul><li>Messages are received by the methods of an object </li></ul>
  10. 10. What is an object? <ul><li>Tangible Things as a car, printer, ... </li></ul><ul><li>Roles as employee, boss, ... </li></ul><ul><li>Incidents as flight, overflow, ... </li></ul><ul><li>Interactions as contract, sale, ... </li></ul><ul><li>Specifications as colour, shape, … </li></ul>
  11. 11. So, what are objects? <ul><li>an object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain. </li></ul><ul><li>Or </li></ul><ul><li>An &quot;object&quot; is anything to which a concept applies. </li></ul><ul><li>Etc. </li></ul>
  12. 12. Why do we care about objects? <ul><li>Modularity - large software projects can be split up in smaller pieces. </li></ul><ul><li>Reuseability - Programs can be assembled from pre-written software components. </li></ul><ul><li>Extensibility - New software components can be written or developed from existing ones. </li></ul>
  13. 13. Example: The Person class <ul><li>#include<string> </li></ul><ul><li>#include<iostream> </li></ul><ul><li>class Person{ </li></ul><ul><li>char name[20]; </li></ul><ul><li>int yearOfBirth; </li></ul><ul><li>public: </li></ul><ul><li>void displayDetails() { </li></ul><ul><li>cout << name << &quot; born in &quot; </li></ul><ul><li><< yearOfBirth << endl; </li></ul><ul><li>} </li></ul><ul><li>//... </li></ul><ul><li>}; </li></ul>private data public processes
  14. 14. The two parts of an object <ul><li>Object = Data + Methods </li></ul><ul><li>or to say the same differently: </li></ul><ul><li>An object has the responsibility to know and the responsibility to do. </li></ul>= +
  15. 15. Data Functions Object A Data Functions Object B Data Functions Object C Communication Organization of data and functions in OOP Private Public
  16. 16. Class <ul><li>User-defined data type </li></ul><ul><li>Objects are variables of the type class </li></ul><ul><li>Any number of objects can be created from one class </li></ul><ul><li>Collection of objects of similar user-defined data types </li></ul><ul><ul><li>fruit mango; </li></ul></ul><ul><ul><li>Will create an object mango belonging to the class fruit . </li></ul></ul>
  17. 17. Data Encapsulation <ul><li>The wrapping up of data and functions into a single unit ( called class) is known as encapsulation . </li></ul><ul><li>Class is not accessible by outside world </li></ul><ul><li>Only its own functions can access it </li></ul><ul><li>The insulation of the data from direct access by the program is called data hiding or information hiding . </li></ul>
  18. 18. Data Abstraction <ul><li>Abstraction refers to the act of representing essential features without including the background details or explanations. </li></ul><ul><li>Classes use the concept of abstraction </li></ul><ul><li>They are defined as a list of abstract attribute such as size, weight, cost and functions to operate on these attributes. </li></ul>
  19. 19. Trivia (something of small importance) <ul><li>Attributes also called data members </li></ul><ul><ul><li>Because the hold information. </li></ul></ul><ul><li>Functions that operate on these data are called methods or member functions . </li></ul>
  20. 20. Inheritance <ul><li>Inheritance is the process by which objects of one class acquire the properties of objects of another class. </li></ul><ul><li>It support the concept of hierarchical classification . </li></ul><ul><li>Bird robin is a part of the class flying bird which is again a part of the class bird . </li></ul>
  21. 21. Bird Attributes Feathers Lay eggs Flying Bird Attributes … . … . Nonflying Bird Attributes … … Robin Attributes … . … . Swallow Attributes … … Penguin Attributes … … Kiwi Attributes … …
  22. 22. Polymorphism <ul><li>Polymorphism is another important OOP concept. </li></ul><ul><li>Polymorphism, a Greek term, means the ability to take more than one form. </li></ul>
  23. 23. Shape Draw() Circle Object Draw(circle) Box object Draw(box) Triangle object Draw(tringle)
  24. 24. Dynamic Binding <ul><li>Binding refers to the linking of a procedure call to the code to be executed in response to the call. </li></ul><ul><li>Dynamic binding also know as late binding means the code associated with a given procedure call is not known until the time of the call at run-time. </li></ul>
  25. 25. Message Passing <ul><li>An object oriented program consists of a set of objects that communicate with each other. </li></ul><ul><li>Employee.salary (name); </li></ul><ul><li>Object message information </li></ul>
  26. 26. Thanks
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×