Docsity

Basic concepts of object oriented programming, Study notes of Object Oriented Programming

Toggle sidebar
bg1
OOP concepts
Basic concepts of object oriented programming are:
• Objects(
• Class(
• Abstraction(
• Encapsulation(
• Inheritance(
• Dynamic(binding(
• Message(passing(
• Polymorphism(
Object
Object is a real world entity with specific features and behaviour. Objects in a program are
generally used to model the real-world items.
• Object(can(be(tangible(like(person,(place,(car,(house(and(tree(etc.(
• It(can(be(logical+entity(such(as(date(of(birth,(job(profile,(bank(account(
etc.((((((((((((((((((((((((((((((((
An object is defined with two major items
o Attributes(of(object,(Examples(name,(colour,(shape,(size(or(other(features(
o Behaviour(of(object(defined(by(operations(or(functions,(Examples(read,(drive,(
display,(update_balance(etc.(
Examples of objects:
Name
Attributes/ Data
Behaviour / Functions
Person
Name, age, DOB, phone
Walk(), sleep(), work(), work()
Car
Type, make, model, gear
Start(), stop(), setspeed(), setgear()
Time
Hours, min, sec
Set_time(), current_time(), get_time()
Object from the point of view of a programmer
o Objects(are(the(basic(run(time(entities(in(object(oriented(environment.(
o Any(problem(is(analysed(in(terms(of(objects(and(the(kind(of(communication(between(
them.(
o Object(is(an(instance(of(a(class(which(contains(the(data((attributes)(and(functions((to(
provide(services).(
o Object(is(used(to(run(the(functions(of(a(class(
pf3
pf4
pf5

Partial preview of the text

Download Basic concepts of object oriented programming and more Study notes Object Oriented Programming in PDF only on Docsity!

OOP concepts Basic concepts of object oriented programming are:

  • Objects
  • Class
  • Abstraction
  • Encapsulation
  • Inheritance
  • Dynamic binding
  • Message passing
  • Polymorphism Object Object is a real world entity with specific features and behaviour. Objects in a program are generally used to model the real-world items.
  • Object can be tangible like person, place, car, house and tree etc.
  • It can be logical entity such as date of birth, job profile, bank account etc. An object is defined with two major items o Attributes of object, Examples name, colour, shape, size or other features o Behaviour of object defined by operations or functions, Examples read, drive, display, update_balance etc. Examples of objects: Name Attributes/ Data Behaviour / Functions Person Name, age, DOB, phone Walk(), sleep(), work(), work() Car Type, make, model, gear Start(), stop(), setspeed(), setgear() Time Hours, min, sec Set_time(), current_time(), get_time() Object from the point of view of a programmer o Objects are the basic run time entities in object oriented environment. o Any problem is analysed in terms of objects and the kind of communication between them. o Object is an instance of a class which contains the data (attributes) and functions (to provide services). o Object is used to run the functions of a class

o Objects communicate with each other through message passing. Class The real world objects, such as television have many features and properties. For example every television is a complete unit in itself even if it is attached to some other device like DVD player it remains a television. There is an interface like buttons and remote control to operate television. All televisions have some common properties and operations. Collection of similar objects is known as class. And instances of that class are called objects. Example : Name of class – television Data – brand, model, type, volume_level, channel_number etc. Functions – switch_on(), switch_off(), change_channel(), set_volume() etc. o Class is a collection of similar objects. o Class defines all the properties (data and functions) of an object. o Class provides a well-defined interface to use the functions of that class. o A class must be complete and well-documented. o The programming code of a class should be robust. Class Object user defined data type variable of type class group of similar objects Instance of a class Class defines properties i.e. data and function members Objects hold actual values Blue print of the object Number of objects are created using class Example class and objects: Class – television with attributes as brand_name, model, size and colour. Object1 - my_tv with values as Sony, 2015, 40”, black Object2- neighbour_tv with values as Samsung, 2016, 32”, white Object3 - office_tv with values as Sony, 2014, 22”, blue Data Abstraction Abstraction means providing only the essential or relevant information and hiding the background or technical implementation details of something. Example: to drive a car one only needs to know the driving process and not the mechanics of car engine.

o Reuse of functionality o Automatically inherits attributes and methods of existing base class o Only new attributes and methods are defined in derived class o Improved code readability o Extension and modification is easier o Fast development and implementation Base class: The class whose members are inherited is called base class or parent class. Derived class : the class which is derived from the base class is called derived class or the child class. Derived class extends the functionality of base class. OOP supports different types of inheritance and access controls. Details of inheritance types and their implementation will be discussed later. Polymorphism Polymorphism means many forms. In real life situation a single person behaves differently in different situations, just like a boy is a student in school, son at home, player in playground, passenger in a bus etc. In general as a person behaves differently with different persons around, there may exist multiple implementations for single interface. A function can show different behaviour with different types of data or an operator may operate on variety of items differently. Polymorphism in OOPs means that a function or a method can handle different types of inputs and behave differently for different objects. o It allows the methods to do different things for different objects. o Polymorphism defines one interface but multiple implementations. o It allows one interface to be used for a variety of similar tasks. o One operation may show different behaviour in different instances for example ‘+’ can be used to find sum of integers, floats and complex numbers (operator overloading) o It means OOPs program can have many functions with the same name but different set of statements (known as function overloading). Dynamic Binding Binding means linking of procedure call to the code to be executed. Binding can take place at o Compile time, which is called static binding o Execution time, which is called dynamic binding In dynamic binding code is linked to the procedure call at the run time. It is associated with polymorphism and inheritance. Dynamic binding helps in writing more flexible program as the compiler can link the procedure call to procedure code after taking input from the user. Message passing

An object oriented program consists of various objects which communicate with each other through a special mechanism known as message passing. Objects can communicate by sending and receiving messages. A message for an object is a request for execution of a function or procedure. It includes invoking of related function after receiving related information. The message passing involves: o Name of the object o Name of the function- the message o Information to be sent- attributes for the called function Example: employee.salary(name) Object – employee, Message – salary, Information – name of employee Test yourself

  1. What are the concepts of object oriented programming?
  2. What is relation between a class and an object?
  3. Why data hiding is important and how is this concept implemented in c++?
  4. What do you mean by abstraction and encapsulation?
  5. How are the abstraction and encapsulation related to each other?
  6. Which concept of OOP supports reusability of code?
  7. What do you mean by inheritance? Why is it needed?
  8. Discuss difference between early binding and late binding?
  9. What is polymorphism? How is it related to dynamic binding and function overloading?
  10. What do you mean by message passing? What are the components of a message?