Unit 1 OOP PDF
Document Details
Uploaded by SelfSufficientCourage
Tags
Summary
This document provides an overview of object-oriented programming (OOP) concepts. It covers topics like what OOP is, its characteristics, features, and differences from procedural programming (POP). The document delves into fundamental OOP concepts such as objects, classes, data abstraction, inheritance, polymorphism, and dynamic binding.
Full Transcript
Unit 01: Basic Concepts of OOP, Classes and objects Contents: What is object oriented programming? Why do we need object-oriented Programming? Application and Characteristics of object oriented languages. Basic concepts of Object Oriented Programming: Encapsulation, Abstraction, Inh...
Unit 01: Basic Concepts of OOP, Classes and objects Contents: What is object oriented programming? Why do we need object-oriented Programming? Application and Characteristics of object oriented languages. Basic concepts of Object Oriented Programming: Encapsulation, Abstraction, Inheritance, Polymorphism, Dynamic Binding, Message Passing, class and object. Structure of C++ program, Compiling and Executing C++ Programs. Classes: Specifying Class, Access Specifies, Defining Data member and member function. Objects: Creating Object, memory allocation for object, Operators in C++: The Scope resolution operator, The arrow operator, memory management operator, type casting, Pointer to object, The 'this' pointer, Objects and Arrays: Array of Objects, Arrays inside objects. Namespaces, Nested/Inner Classes. Object Oriented Programming: Object oriented programming can be defined as a programming model which is based upon the concept of objects. Objects contain data in the form of attributes and code in the form of methods. In object oriented programming, computer programs are designed using the concept of objects that interact with real world. Object oriented programming languages are various but the most popular ones are class-based, meaning that objects are instances of classes, which also determine their types. Languages used as Object Oriented Programming Java, C++, C#, Python, PHP, JavaScript, Ruby, Perl, Objective-C, Dart, Swift, Scala. Characteristics of POP: 1) It emphasis on doing things i.e. Algorithm. 2) Large programs are divided into smaller program known as function. 3) Most of the function shares global data. 4) Data move openly around the system from function to function. 5) Function transforms data from one form to another. 6) It follows top down approach in program design. Features of OOP: 1) It emphasis on data rather than procedure. 2) Programs are divided into objects. 3) Objects are characterized by functions that operate on data. 4) Functions that operate on data of the object are tied together in data structure. 5) Data is hidden and cannot be access by external function. 6) Object communicates with each other through function. 7) New data and function can be easily added whenever necessary. 8) It follows bottom up approach in program design. Differences Between POP And OOP: To summarize, few important differences between POP and OOP are as follows: POP : 1) It is procedure or action oriented. 2) Unit of program is function. 3) Group of function is a program. 4) It emphasis on algorithm. 5) It follows top down approach. OOP 1) It is object oriented 2) Unit of program is object. 3) User-define type of classes and objects to form program. 4) It emphasis on data. 5) It follows bottom up approach. Sr. Key OOP POP No. 1 Definition OOP stands for Object Oriented POP stands for Procedural Programming. Oriented Programming. 2 Approach OOP follows bottom up approach. POP follows top down approach. 3 Division A program is divided to objects A program is divided into and their interactions. functions and they interacts. 4 Inheritance Inheritance is supported. Inheritance is not supported. supported 5 Access control Access control is supported via No access modifiers are supported. access modifiers. 6 Data Hiding Encapsulation is used to hide No data hiding present. Data is data. globally accessible. 7 Example C++, Java C, Pascal Basic Concepts of OOP: 1) Object 2) Class 3) Data abstraction and encapsulation 4) Inheritance 5) Polymorphism 6) Dynamic binding 7) Message passing Let’s see these one by one. 1) Objects : Objects are basic run time entities and form a distinguishable real time entity. e.g. object may represent a person, place, bank account, Roll Number of class etc. Program object should be chosen such that they match closely with real world objects. Each object contains data and code to manipulate the data. They take up a space in memory and have associated address. An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated. 2) Class : The building block of C++ that leads to Object-Oriented programming is a Class. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object. For Example: Consider the Class of Cars. There may be many cars with different names and brand but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class and wheels, speed limits, mileage are their properties. A class is group of objects with similar attributes and common relationship to other objects i.e. class is collection of objects of similar type. Objects are variables of type class. Classes are user define data types and behaves like any other built-in type. Once a class is defined we can create any number of objects belonging to that class. 3) Data Abstraction and Encapsulation : The wrapping of data and functions into a single unit (class) is called as encapsulation. The data is not accessible to the outside world and only the function of that class can access its data. This insulation of data from direct access by a program is called data hiding or information hiding. Abstraction refers to the act of representing essential features without including the background details. Classes use the concept of abstraction and encapsulate all essential properties of objects that are to be created. The attributes are sometime called as data members and the functions that operate on these data are called as methods or member function. Classes are also known as Abstract Data Types (ABT) as they uses concept of data abstraction. 4) Inheritance : It is the process by which objects of one class acquire the properties of objects of another class. It also means defining a new objects in terms of old one. It gives the idea of reusability. One class of object inherits (reuse) data and behavior from another class, and thus form hierarchy among classes in which child class is inheritaed from its parent class. Parent class is called as base class and child class is called as derived class. Base class also known as super class and derive class as sub class. Thus we can add additional features to an existing class without modifying it. OOP’s also support multiple inheritance which means class can be derived from more than one base class. In fig. 1.4 Class Bird is Base class. Flying Birds and Non-flying Birds are child classes. 5) Polymorphism : It means ability to take more than one form. It refers to single operation can have different behavior in different object i.e. different object react differently to same message where behavior depends upon types of data used for operation. e.g. Operation of addition : It will generate a sum if data is two number. If data is string, operation will produce a third string by concatenation operation. x + y = Sum, if x and y are nos. x + y = String, if x and y are strings The process of making an operator to perform different functions in different instances is known as operator overloading. Using a single function name to perform different types of task is called as function overloading. 6) Dynamic binding : It is also called as late binding which means that code associated with a given procedure call is not known until time of the call at run time. It is associated with polymorphism and inheritance. 7) Message passing : OOPs consists of objects and classes. It consists of set of objects, which communicate with each other by sending and receiving information. A message for object is request for execution of function and that function will generate result in receiving object. Message passing envolves specifying name of object, name of function (message) and the information to be send. e.g. a function call is: employee.salary (name); where, employee is object, salary is message or function and name is information. Advantages of OOP: OOP focuses on identifying objects from application and writing procedures around those objects. Following are advantages of OOPs. 1) By using inheritance we can eliminate a redundant code and extend use of existing class. 2) Encapsulations helps to built a protected program that cannot be access by other parts of program. 3) One can have multiple objects without any interference. 4) We can divide program classes and objects. 5) Interference with external system is simple by using message passing. 6) It supports reusability, which reduces design, coding and testing cost of software. 7) OOPs system can be easily upgraded from small to large system. 8) Software complexity can be easily managed. Application of C++: 1) Since C++ allow us to create hierarchy related objects, we can build special object- oriented libraries which can be used later by many programmers. 2) While C++ is able to map the real-world problem properly, the C part of C++ gives the language the ability to get closed to the machine-level details. 3) C++ programs are easily maintainable and expandable. When a new feature needs to be implemented, it is very easy to add to the existing structure of an object. Application of OOP: The promising areas of application of OOP include: 1) Real-time system 2) Simulation and modeling 3) Object-oriented data bases 4) Hypertext, Hypermedia, and expertext 5) AI and expert systems 6) Neural networks and parallel programming 7) Decision support and office automation systems 8) CIM/CAM/CAD systems A Simple C++ Program: // My first C++ program # include void main () { cout is called as extraction or ‘get from’ operator. It extracts (takes) value from keyboard and assigns it to the variable on its right. Fig: Input using Extraction operator 7) Cascading of I/O operators e.g. cout > b; Here the values accepted are assigned to the variables from left to right. i.e. first value entered will be stored in a and second value in b. Sample Program 1 : Write a program to accept radius from user and find out area of circle. # include void main () { float r, area, pi; clrscr ( ); cout > r; pi = 3.14; area = pi * r * r; cout