Monday, April 11, 2011

Basic OOP


Fundamental Topics of OOP:

1.      Object-Oriented Programming vs. Procedural Programming/Structured Programming
o   OOA
o   OOD
o    OOP
2.      Objects
3.      Instances
4.      Classes
5.      Constructors
o   Default Constructor
o   User Defined Constructor
6.      Inheritance
7.      Relationships
o   IS-A Relation
o   Kind-A Relation
o   Part-Of Relation
o   HAS-A Relation
8.      Composition
9.      Aggregation
10. Access Specifiers
11. Polymorphism
12. Singleton Class
13. Encapsulation
14. Abstract Classes
15. Interfaces
16. Late Binding/Dynamic Binding
17. Early Binding/Static Binding
18. Virtual Constructors/Destructor
19. Structures vs. Classes
20. Three Principles of OOP




















1.     Object-Oriented Programming vs. Procedural Programming:

Object Oriented Programming (OOP)
Procedural Programming/Structured Programming
·         A way of modeling objects as they are perceived in real life
·         Focuses on representing real world objects in software programs
·         Focuses on defining state, properties and operations on entities
·         Emphasizes on structuring entities as they are used in real life rather how they can be used algorithmically
·         Focuses on the algorithm that is to be implemented
·         Focuses on how to accomplish the goal by breaking down the algorithm into small functions
·         Doesn’t focus on how to associate the data and the functions and procedures that work on that data
·         Data and functions are tightly coupled thus increasing complexity and decreasing reusability of the code

o   OOA (Object Oriented Analysis):

It’s the process of analyzing the system and its needs so as to identify the separate entities/Classes in the system and the relationships between them.

o   OOD (Object Oriented Design):

It’s the process of identifying the attributes and operations of each entity and to break them up into appropriate classes and to decide the types and purpose of the attributes and operations

o   OOP (Object Oriented Programming):

Defined Above

2.     Objects:

Objects are the physical and conceptual things we find in the universe around us. Object is the basic unit of object-oriented programming. Objects are identified by its unique name. An object represents a particular entity. An Object is a collection of data members and associated member functions also known as methods. The class of Dog defines all possible dogs by listing the characteristics and behaviors they can have; the object Lassie is one particular dog, with particular versions of the characteristics. A Dog has fur; Lassie has brown-and-white fur.

3.     Instances:

One can have an instance of a class or a particular object. The instance is the actual object created at runtime. In programmer terminology, the Lassie object is an instance of the Dog class. The set of values of the attributes of a particular object is called its state. The object consists of state and the behavior that's defined in the object's class.

4.     Classes:

Real world entities are modeled in OO through classes. A class is defined through its properties (adjectives) and operations/functions (verbs). A class is a pattern, template, or blueprint for a category of structurally identical items. It’s a user defined data type.

5.     Constructors:

Constructors are special functions used to perform initialization on an object. They are declared and defined like any other member function of a class. They have the same name as the class. It can have any number of arguments but no return type.



o   Default Constructor:
A constructor without any parameter is called a Default Constructor. If no arguments are given while creating object, then default constructor is called. If no constructor is defined, then compiler auto-generates a default constructor.
o   User Defined Constructor:
Constructors that have input parameters are called User Defined Constructors. They are generally used to initialize private member variables to some user defined value. Classes that have user defined constructors must also have default constructors.

6.     Inheritance:

Inheritance is the process of forming a new class from an existing class or base class. The base class is also known as parent class or super class, the new class that is formed is called derived class. Derived class is also known as a child class or sub class. Inheritance helps in reducing the overall code size of the program, which is an important concept in object-oriented programming.
NOTE: IN C# 1 CLASS CAN INHERIT FROM 1 CLASS ONLY MEANING CHLD CLASS CAN HAVE ONLY 1 PARENT CLASS WHEREAS IN C++ 1 CLASS CAN HAVE MORE THAN 1 PARENT

7.     Relationships:

o   IS-A Relation:
This type of relationship is used to describe when we talk about objects. E.g. Circle IS-A Point.
o   KIND-A Relation:
This relationship is used at the class level (Attributes & Functionality) to describe relationships between two similar classes. E.g. Circle is KIND-A Point.
o   Part-Of Relation:
You sometimes need to be able to build objects by combining them out of others.
E.g.:
class Logo {
  attributes:
    Circle circle
    Triangle triangle

  methods:
    set(Point where)
  }
Illustration of ``part-of'' relationship.

o   HAS-A Relation:
This relationship is just the inverse version of the part-of relationship.
Illustration of ``has-a'' relationship.

8.     Composition:

Composition is a strong ownership between Class A, the whole, and Class B, its part. Illustrate composition with a filled diamond.

9.     Aggregation:

When one class can survive without other class is called aggregation. Illustrate composition with an empty diamond.

10.     Access Specifier:

Specify the access rules for member functions and member variables
a.      Public:
A public access specifier provides the least protection to the internal (Class’s) data members and member functions. A public access specifier allows the outside world to access/modify the data members directly. 
b.      Private:
A private access specifier is used to hide the data member or member function to the outside world. This means that only the class that defines such data member and member functions has access to them. 
c.       Protected:
A protected access specifier is mainly used with inheritance. A data member or member function declared as protected will be accessed by its class and its child class but not from the outside world. We can also say that a protected data member is public for the class that declares it and it’s child class; but is private for the rest of the program (outside world). 

11.     Polymorphism:

 Polymorphism allows an entity (for example, variable, function or object) to take a variety of representations during run time.
OR
The ability of objects of different classes related by inheritance to respond differently to same input is called polymorphism. This is implemented through VIRTUAL FUNCTIONS.

a.      With Respect to Variable:
The concept of dynamic binding allows a variable to take different types dependent on the content at a particular time. In short when value of variable changes at run time.

E.g. :
boolean isNull(int i) {
    if (i == 0) then
      return TRUE
    else
      return FALSE
    endif
  }
 
b.      With Respect to Function:
When function definition changes during run time. It’s also called function overloading.

E. g. :
Circle acircle
Point apoint
Rectangle arectangle
 
display(apoint)     
display(acircle)    
display(arectangle) 
 
c.       With Respect to Class Object:
Objects of super-classes can be filled with objects of their subclasses. Operators and methods of subclasses can be defined to be evaluated in two contexts:
o Based on object type, leading to an evaluation within the scope of the super-class.
o Based on object content, leading to an evaluation within the scope of the contained subclass.

E. g. :
class Base {
  attributes:
 
  methods:
    virtual foo()
    bar()
  }
 
  class Derived inherits from Base {
  attributes:
 
  methods:
    virtual foo()
    bar()
  }
 
  demo(Base o) {
    o.foo()
    o.bar()
  }
 
 
  Base abase
  Derived aderived
 
  demo(abase)
  demo(aderived)
Output:
Suppose, that foo() and bar() are defined to just print out their name and the class in which they are defined. Then the output is as follows:
foo() of Base called.
bar() of Base called.
foo() of Derived called.
bar() of Base called.

12.     Singleton Class:

A class of which only one instance can be created is called singleton class. Its constructor is private. It has only one object.
HOW TO IMPLEMENT SINGLETON
·         Create private static object of class through which all functionality of class will be performed.
·         Create private constructor.
·         Create Getter Function to access the only object of the class created above.

13.     Encapsulation:

Encapsulation is grouping data and functions together and keeping their implementation details private. Greatly restricting access to functions and data reduces coupling, which increases the ability to create large programs.

14.     Abstract Classes:

·         Class which cannot be instantiated (Object of class cannot be created).
·         It contains some abstract methods, meaning function doesn’t have definition at the time of class definition.
·         Keyword ABSTRACT is used to define the class.

15.     Interfaces:

·         Type of abstract class.
·         All the functions have no definition at the time of class definition.
·         Keyword INTERFACE is used to define the class.

16.     Late Binding/Dynamic Binding:

When a value is assigned to variable at run time is called late binding/Dynamic binding.

17.     Early Binding/Static Binding:

When a value is assigned to variable at compile time is called early binding/Static binding.

18.     Virtual Constructor/Destructor:

Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error.
Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object. 
 There is a simple solution to this problem – declare a virtual base-class destructor. This makes all derived-class destructors virtual even though they don’t have the same name as the base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called.

19.     Structures v/s Classes

Basically, the purpose of both structures and classes is to group similar data into one entity. Structures were designed for C while classes for C++. In Theory, both can be used interchangeably. The only difference is that by default, data members in structures are public while that in classes are private. Note that Structures in C cannot be extended like structures in C++. A Structure
in C cannot contain functions like that in C++ can. Programmers generally use structures to group data together and classes to group data and functions together.

Q1. When should destructor be virtual?

A1.  When someone will delete a derived-class object via a base-class pointer. Make your destructor virtual if your class has any virtual functions.
Note: if your base class has a virtual destructor, then your  child class destructor is automatically virtual. You might need an explicit destructor for other reasons, but there's no need to re-declare a destructor simply to make sure it is virtual. No matter whether you declare it with the virtual keyword, declare it without the virtual keyword, or don't declare it at all, it's still virtual.

Q2. Why can’t Constructor be virtual but Destructor be?

A2.  When you create some virtual function it must go into a VTable that keeps track of these virtual functions, since it’s the constructor we don’t have the table created at that time so declaring a constructor virtual can throw an exception or error...

the prime reason to declare the destructors virtual is that in large scale programs we may forget to de-allocate all the virtual functions we declared and declaring the destructor virtual can ensure that all objects created get de-allocated at the termination of the program.

20.     Three Principles of OOP

1.       Encapsulation
2.       Inheritance
3.       Polymorphism

No comments:

Post a Comment