1 Introduction to Object Oriented Programming Indriani Noor

1 -Introduction to Object Oriented Programming Indriani Noor Hapsari, ST, MT Sumber: - Walter Savitch, Problem Solving with C++, Pearson International Edition, 2006. - http: //www. mu. ac. in/myweb_test/MCA study material/ - http: //www. cs. fsu. edu/~xyuan/cop 3330/ - https: //www. programiz. com/cpp-programming

Object Oriented Programming Syllabus Application of object oriented paradigm to programming. Main features on OOP: Encapsulation (Accessibility), Inheritance, Polymorphism (Overriding vs Overloading)

Prerequisites Understand procedural programming using C/C++ Variables and arrays Expression and assignment Various control flows (conditions, loops) Basic IO mechanisms.

A Simple C Program #include <stdio. h> int main() { int counter = 1; printf("Hello World %d“, counter); return 0; } A Simple C++ Program #include <iostream> using namespace std; int main() { int counter = 1; cout<<"Hello World " << counter; return 0; }

Structural vs Object-Oriented Programming q Structural (procedural) programming v Programming using well defined control structures Conditionals, loops, sequence, expression and assignments Data (variables, arrays, structures) are separated from their operations It provides an abstraction of the hardware. q Object-oriented programming v Built on top of structural (procedural) programming v Programming based on the concept of object. ‒ Objects bundle data with their operations. ‒ Enables information hiding, which allow us to organize the program in a more manageable way.

Structural Programing vs Object-Oriented Programming (2) q Structural (procedural) programming v Programs are typically organized around code. v This approach can be thought as “code acting on data”. q Object-oriented programming v Programs are organized around data. v “Data controlling access to code”: v You define the data and the routines that are permitted to act on that data.

Object-Oriented basics A fundamental concept in an object-oriented language is the encapsulation of data and procedures (functions) together into units called objects. q An object consists of: Name – a way of referring to an object inside a program (eg. A Fraction object might be called F 1). Member Data – data contained within an object (eg. Fraction has an integer numerator and denominator). Member Functions – routines that act upon the data within an object (eg. The fraction object might have a function that returns the decimal representation of the fraction stored). Interface – defines the ways a programmer may directly access the member data/functions of an object (more on this next lecture).

Characteristics of Object Oriented Programming (OOP) Emphasis is on data rather than procedure Programs are divided into objects. Data structures are designed such they characterize the objects. Functions that operate on the data of an object are tied together in the data structure. Data is hidden and cannot be accessed by external functions. Objects may communicate with each other through functions.

OO features Natural: mengikuti cara berpikir manusia (manusia memandang dunianya sebagai kumpulan object yang berinteraksi) Abstraction: menjelaskan makna sebuah entitas secara cepat dan mudah Encapsulation: bundling the data (fields) and behavior (methods) together in classes Information Hiding: dapat menyembunyikan detil yang tidak perlu Modular: object adalah entitas yang independen

Classes q A class is another fundamental concept in an objectoriented language that provides a blueprint for a new type ('classification') of object. v A class outlines the data, functions and the interface objects of that class will receive. v A class also defines how objects of that class behave by providing code that implements the functions associated with the class. v A programmer can create one or more objects from a class ─ Similar to building multiple houses from one set of blueprints.

Example: Rectangle Class class Rectangle { private: int x, y; Class Name Interface Member Data public: Rectangle(int p. Long, int p. Width); void set_values(int p. Long, int p. Width); int area (void); }; Member Functions

End.
- Slides: 12