Concepts of OOP with features

1. W hat is OOP?

Object-oriented programming (OOP) is a programming paradigm that uses "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance. It was not commonly used in mainstream software application development until the early 1990s.[citation needed] Many modern programming languages now support OOP.

An object-oriented program may thus be viewed as a collection of interacting objects, as opposed to the conventional model, in which a program is seen as a list of tasks (subroutines) to perform. In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects and can be viewed as an independent 'machine' with a distinct role or responsibility. The actions (or "operators") on these objects are closely associated with the object. For example, the data structures tend to 'carry their own operators around with them' (or at least "inherit" them from a similar object or class).

One of the principal advantages of object-oriented programming techniques over procedural programming techniques is that they enable programmers to create modules that do not need to be changed when a new type of object is added. A programmer can simply create a new object that inherits many of its features from existing objects. This makes object-oriented programs easier to modify.


2. Explain objects and class with an example.

Objects:
Object is the basic unit of object-oriented programming. Objects are identified by its unique name. An object represents a particular instance of a class. There can be more than one instance of an object. Each instance of an object can hold its own relevant data.
An Object is a collection of data members and associated member functions also known as methods.
Classes:
Classes are data types based on which objects are created. Objects with similar properties and methods are grouped together to form a Class. Thus a Class represents a set of individual objects. Characteristics of an object are represented in a class as Properties. The actions that can be performed by objects become functions of the class and is referred to as Methods.
For example consider we have a Class of Cars under which Santro Xing, Alto and WaganR represents individual Objects. In this context each Car Object will have its own, Model, Year of Manufacture, Colour, Top Speed, Engine Power etc., which form Properties of the Car class and the associated actions i.e., object functions like Start, Move, Stop form the Methods of Car Class.
No memory is allocated when a class is created. Memory is allocated only when an object is created, i.e., when an instance of a class is created.
Differences between struct in C and classes in C++
In C++, a structure is a class defined with the struct keyword.[1] Its members and base classes are public by default. A class defined with the class keyword has private members and base classes by default. This is the only difference between structs and classes in C++.
Classes are declared with the class or struct keyword. Declaration of members are placed within this declaration.
struct person
{
  string name;
  int age;
};
class person
{
public:
  string name;
  int age;
};

Example of class and object:-
#include <iostream>
#include <string>
class person
{
public:
  string name;
  int age;
};
 int main ()
{
  person a, b;
  a.name = "Calvin";
  b.name = "Hobbes";
  a.age = 30;
  b.age = 20;
  cout << a.name << ": " << a.age << endl;
  cout << b.name << ": " << b.age << endl;
  return 0;
}
Executing the above code will output
Calvin: 30
Hobbes: 20


3. Explain following
1.)             Encapsulation:-  Encapsulation is the process of combining data and functions into a single unit called class. Using the method of encapsulation, the programmer cannot directly access the data. Data is only accessible through the functions present inside the class. Data encapsulation led to the important concept of data hiding. Data hiding is the implementation details of a class that are hidden from the user. The concept of restricted access led programmers to write specialized functions or methods for performing the operations on hidden members of the class. Attention must be paid to ensure that the class is designed properly.

2.)             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. This makes programs more reliable, since publishing a specific interface to an object prevents inadvertent access to data in ways that were not designed or accounted for. In C++, the access to an object, and its encapsulated data and functions is treated very carefully, by the use of keywords private, protected, and public.

3.)             Data Abstraction:- Data abstraction is the result of defining classes with emphasis on the similarities of its objects and ignoring their differences. A good abstraction is one that emphasizes details that are significant to us and suppresses details that are not important. A class is an abstraction. Thus, an abstraction focuses on the outside view of the object and clearly separates its essential behavior from the internal implementation details.

1.Emphasizes similarity of objects and ignore their differences
2.Good abstraction emphasizes significant details and ignores unimportant details.
3.Class is abstraction
4. Focuses on outside view of object only.


4.)             Reusability :-      This term refers to the ability for multiple programmers to use the same written and debugged existing class of data. This is a time saving device and adds code efficiency to the language. Additionally, the programmer can incorporate new features to the existing class, further developing the application and allowing users to achieve increased performance. This time saving feature optimizes code, helps in gaining secured applications and facilitates easier maintenance on the application.

The implementation of each of the above object-oriented programming features for C++ will be highlighted in later sections.

5.)             Dynamic Binding :- Binding refers to the linking of a function call to the code to be executed in response to the call. Dynamic Binding means that the code associated with the given function call is not known until the time of call at runtime. A Function call associated with a polymorphic reference depends on dynamic type of that reference. Dynamic binding has two forms, static and dynamic.  Statically-typed dynamic binding is found in languages such as C++ (virtual functions) .  It is not known which function will be called for a virtual function at run-time because a derived class may override the function, in which case the overriding function must be called.  Statically determining all possibilities of usage is undecidable.  When the complete program is compiled, all such functions are resolved (statically) for actual objects.

4. What is inheritance? Explain with example.
Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class.

For example, a programmer can create a base class named fruit and define derived classes as mango, orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of the base class (fruit) with additional attributes or features specific to these newly created derived classes. Mango would have its own defined features, orange would have its own defined features, banana would have its own defined features, etc.



#include <iostream.h>
class fruit
{
   public:
  void fruit_color(char *color)
{
     Cout<<”Fruit color is:”<<color;
}    
};
class mango : public fruit
{
};
int main()
{
   Clrscr();
   mango obj;
   obj.fruit_color(“yellow”);
   getch();
}
This concept of Inheritance leads to the concept of polymorphism.
Features or Advantages of Inheritance:
·       Reusability
·       Saves Time and Effort
·       Increases Program Structure which results in greater reliability. Polymorphism 
5. What is Polymorphism? Explain Operator Overloading and function overloading.
            Polymorphism:-  Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.
Features and Advantages of the concept of Polymorphism:
·       Applications are Easily Extendable
·       Helps in reusability of code.
·       Provides easier maintenance of applications.
·       Helps in achieving robustness in applications.
Types of Polymorphism:
C++ provides three different types of polymorphism.
·       Virtual functions
·       Function name overloading
·       Operator overloading

Operator Overloading:-      One of the nice features of C++ is that you can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading. You can implement C++ operator overloads by providing special member-functions on your classes that follow a particular naming convention. For example, to overload the + operator for your class, you would provide a member-function named operator+ on your class.
            The following set of operators is commonly overloaded for user-defined classes:
·       = (assignment operator)
·       + - * (binary arithmetic operators)
·       += -= *= (compound assignment operators)
·       == != (comparison operators)

Function Overloading: In function overloading, the function is said to be overloaded when same name is given to different functions. However, the functions will differ at least in any one of the these. The number of parameters, the data type of parameters, the order of appearance these three together are referred to as the function signature. While overloading a function, the return types of the function need not differ.
6.Give the benefits of Object Oriented Programming.
1.     Testability/Increased Quality (automated testing can increase speed of testing and increase quality)
2.     Code re-use (Polymorphism, Generics, Interfaces)
3.     Code extensibility
4.     Catch errors at compile time rather than at runtime.
5.     Maintainability: If designed correctly, any tier of the application can be replaced by another that implements the correct interface(s), and the application will still work (can use multiple user interfaces, can swap out data providers, etc.).
6.     Reduces large problems to smaller, more manageable ones.
7.     Fits the way the real world works. It is easy to map a real world problem to a solution in OO code.
7.Give the features of Object Oriented Programming language.
·       Data encapsulation
·       Data abstraction
·       Inheritance
·       Polymorphism
·       Dynamic or runtime binding
·       Reusability
·       Information hiding.