OBJECT ORIENTED PROGRAMMING INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING

INTRODUCTION TO OBJECT ORIENTED PROGRAMMING AND C++ • many programming approaches have been tried. These included techniques such as Ø modular programming, Ø top-down programming, Ø bottom-up programming and Ø structured programming. • The primary motivation reliable and maintainable

PROCEDURE-ORIENTED PROGRAMMING • Conventional programming using high level languages such as COBOL, FORTRON, and C is commonly known as procedureoriented programming. • The problem is viewed as a sequence of things to be done, such as reading, calculating and printing. • The primary focus is on functions. • Disadvantage : Very little attention is given to the data that are being used by various functions.

BASIC CONCEPTS OF OBJECT-ORIENTED PROGRAMMING • The Focus is on the objects that compose the problem domain, their behavior and communication between the objects. • General concepts Used extensively in object-oriented programming are: ▫ ▫ ▫ ▫ Objects Classes Data encapsulation Data abstraction Inheritance Polymorphism Dynamic binding Message Passing

What is an Object? • Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors -wagging, barking, eating. • An object is an instance of a class. • An object can be considered a "thing" that can perform a set of related activities. • Class - A class can be defined as a template/blue print that describes the behaviors/states that object of its type support.

public class Dog{ String breed; int age; String color; void barking(){ } void hungry(){ } void sleeping(){ } } Object creation Dog Boxer=new Dog();

Data Encapsulation • C++ provides the facility to encapsulate data and the relevant functions together in the same object. Example: • Customer uses the swipe interface provided by the machine to swipe the card. • The swipe Machine encapsulates/hides the internal circulatory from all the users and provides simple interface for access by every user. A Mechanism of hiding the internal details and allowing a simple interface which ensures that the object can be used without having to know how it works. Data Hiding Related to the idea of encapsulation is the concept of data hiding. Encapsulation hides the data from other classes and functions in other classes. • by the use of keywords private, protected, and public

class Box { public: double get. Volume(void) { return length * breadth * height; } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box };

• Abstraction: Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i. e. , to represent the needed information in program without presenting the details. Example: Customers need to know only what to purchase and the mode of payment. • Inheritance: One of the most useful aspects of object-oriented programming is code reusability. As the name suggests Inheritance is the process of forming a new class from an existing class that is from the existing class called as base class, new class is formed called as derived class.

• Polymorphism: The ability to use an operator or function in different ways in other words giving different meaning or functions to the operators or functions is called polymorphism. • Poly refers to many. That is a single function or an operator functioning in many ways different upon the usage is called polymorphism. Overloading: • The concept of overloading is also a branch of polymorphism. When the exiting operator or function is made to operate on new data type, it is said to be overloaded. • Example: Payment mode: cash or credit card.

#include <iostream> using namespace std; class Shape { protected: int width, height; public: Shape( int a=0, int b=0) { width = a; height = b; } int area() { cout << "Parent class area : " <<endl; return 0; } }; class Rectangle: public Shape{ public: Rectangle( int a=0, int b=0): Shape(a, b) { } int area () { cout << "Rectangle class area : " <<endl; return (width * height); } }; class Triangle: public Shape{ public: Triangle( int a=0, int b=0): Shape(a, b) { } int area () { cout << "Triangle class area : " <<endl; return (width * height / 2); } };

• Dynamic Binding refers to linking a procedure call to the code that will be executed only at run time. The code associated with the procedure in not known until the program is executed, which is also known as late binding. Example: complier comes to know at runtime that which function of sum will be call either with two arguments or with three arguments • It is generally use with polymorphism and inheritance.

• Message Passing: Objects can communicate with each others by passing message same as people passing message with each other. • Objects can send or receive message or information. • Message passing involves name of object, name of function (message) and information to be send. • For example, student. mark(name). Here student is object, mark is message, name is information.

OOPs Summary

BENEFITS OF OOP • • Quality of software products. Lesser maintenance cost Eliminate redundant code Extend the use of existing classes Build programs from the standard working modules Build secure programs Multiple instances of an object to co-exist without any interference. • Interface with external systems • Software complexity can be easily managed

Object Oriented Languages • • C++ Java MATLAB Perl Python Ruby Visual Basic (. NET)

Applications of using OOP: Main application areas of OOP are • User interface design such as windows, menu , … • Real Time Systems • Simulation and Modeling • Object oriented databases • AI and Expert System • Neural Networks and parallel programming • Decision support and office automation system etc
- Slides: 17