Published on

Published in: Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Lecture 02Fundamentals ofObject-OrientedProgramming
  2. 2. What Are Objects?Software objects model real-world objects or abstract concepts. dog, bicycle, queueReal-world objects have states and behaviors. Dogs states: name, color, breed, hungry Dogs behaviors: barking, fetchingHow do software objects implement real-world objects? Use variables/data to implement states. Use methods/functions to implement behaviors.An object is a software bundle of variables and related methods.
  3. 3. Software Object Visual RepresentationInstance Variables Vs Instance Methods
  4. 4. Software BicycleVisual Representation
  5. 5. What Are Classes?• A class is a blueprint or prototype defining the variables and methods common to all objects of a certain kind.• An object is an instance of a certain class.• After you have created a class, you must create an instance of it before you can use.• Class variables and class methods.
  6. 6. What Are Messages?Software objects interact and communicate witheach other by sending messages to each other.
  7. 7. Components Of A Message The object to whom the message is addressed(YourBicycle)The name of the method to perform (changeGears)Any parameters needed by the method (lowerGear)
  8. 8. O-O Principles• Abstraction - take only important information• Encapsulation - hiding or combine data and operations on data in a single unit.• Inheritance - create new objects from existing objects.• Polymorphism-the ability to use the same expression to denote different operations.
  9. 9. Encapsulation• The objects variables make up the center of the object.• Methods surround and hide the objects center from other objects.• Benefit of encapsulation: Modularity & Information Hiding.• For implementation or efficiency reasons, an object may wish to expose some of its variables or hide some of its methods.
  10. 10. Inheritance• Inheritance allows classes to be defined in terms of other classes.• Each subclass inherits variables and methods from its superclass.• Subclasses can add variables and methods to the ones they inherit from the superclass.• Subclasses can also override inherited methods and provide specialized implementations for those methods.
  11. 11. Class Hierarchy (Inheritance Hierarchy)Superclass vs Subclass.Base Class vs Derived Class.
  12. 12. Polymorphism and Overloading• Polymorphism allows the use of operators or functions in different ways, depending on the data they are operating on.• When an existing operator (eg. + or =) is given the capability to operate on a new data type, it is said to be overloaded.• Overloading is a kind of polymorphism; it is also an important feature of OOP.• Example of operator overloading and polymorphism)• the + operator know how to treat an integer and a float differently.
  13. 13. Benefits of OOP Reusability• Once written, created, and debugged, a class can be distributed to other programmers for use in their own programs.• Reusability facilitates component-based software design and implementation.• A programmer can also take an existing class and, without modifying it, add additional features and capabilities to it.
  14. 14. Benefits of OOP Creating New Data Types• Objects and classes give the programmer a convenient way to construct new data types.• Many features of C++ are intended to facilitate the creation of new data types.• The ability to create new data types leads to extensible languages and programs.• In C++, new data types can be built on top of existing (system or user-defined) data types.
  15. 15. C++ Program Structure #include <iostream.h>Header filespreprocessor statementsclass Data int main( ) // VOID Members{ ….. and Member {} Functionsmain( ) Variable cout << “Welcome to C++”;{ …… declarations & return 0; valid C++} statements }
  16. 16. Example 2#include <iostream.h> Sum = a + b + c;int main ( ) cout << “ the sum is “<< sum;{ int a , b, c, sum =0; cout << “ enter a value “<<endl; OTHER DATA TYPES cin >> a; Char int cout << “ enter b value “<<endl; long int float cin >> b; double cout << “ enter c value “<<endl; cin >> c;
  17. 17. Input / OutputCout - used for printing out the outputSyntax : Cout << “Message if needed”<< variablename; << is called the insertion operator.Cin - used for getting a input from the keyboardSyntax: Cin >> variable name; >> is called the extraction operator
  18. 18. Other PointsRemark statement - it is a non executable statement used for documentation // or /* statements */escape sequences: n - new line t - horizontal tab r - carriage return a - bell - back slash ” - double quotations