Define OOP and explain it's features.
Object-oriented programming (OOP) is a programming paradigm focused on objects rather than actions. It is a method of programming in which data and functions are organized into classes and objects that interact with one another. OOP is an effective way to organize code, making it more modular, reusable, and extensible.
OOP has four main features: classes, objects, inheritance, and polymorphism.
Classes are templates that define the characteristics of a particular type of object. They specify the data and the functions that will be associated with the objects of that class.
Objects are instances of classes. They are the basic units of OOP, and are used to store data and functions.
Inheritance is the process of creating new classes from existing classes. Inheritance allows existing classes to be extended, and new classes to be created that have the same characteristics as the existing classes.
Polymorphism is the process of using the same code for objects of different types. It allows code to be written in a more generic way, so that it can be used for objects of different types.
Object-oriented programming features:
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 a user-defined data-type which has data members and member functions.
Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions define the properties and behavior of the objects in a Class.
In the above example of class Car, the data member will be speed limit, mileage etc and member functions can apply brakes, increase speed etc.
Object: An Object is an identifiable entity with some characteristics and behaviour. 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.
Encapsulation: In normal terms, Encapsulation is defined as wrapping up of data and information under a single unit. In Object-Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them.
Abstraction: Data abstraction is one of the most essential and important features of object-oriented programming in C++. Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.
Polymorphism: The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.
Explain the benefits of OOP
Benefits of OOP
- We can build the programs from standard working modules that communicate with one another, rather than having to start writing the code from scratch which leads to saving of development time and higher productivity,
- OOP language allows to break the program into the bit-sized problems that can be solved easily (one object at a time).
- The new technology promises greater programmer productivity, better quality of software and lesser maintenance cost.
- OOP systems can be easily upgraded from small to large systems.
- It is possible that multiple instances of objects co-exist without any interference,
- It is very easy to partition the work in a project based on objects.
- It is possible to map the objects in problem domain to those in the program.
- The principle of data hiding helps the programmer to build secure programs which cannot be invaded by the code in other parts of the program.
- By using inheritance, we can eliminate redundant code and extend the use of existing classes.
- Message passing techniques is used for communication between objects which makes the interface descriptions with external systems much simpler.
- The data-centered design approach enables us to capture more details of model in an implementable form.
Basic concept of OOP
Four fundamental concepts of Object-oriented programming – Inheritance, Encapsulation, Polymorphism, and Data abstraction.
Inheritance
Inheritance is the process of creating a new class from an existing class. The new class inherits all the features of the existing class and can have additional features of its own. This helps in reducing the amount of code and improving the maintainability of the code.
Encapsulation
Encapsulation is the process of wrapping up of data and functions into a single unit. This helps in protecting the data from outside interference and misuse.
Polymorphism
Polymorphism is the process of using the same code for objects of different types. This helps in having a single code which can be used for different types of objects.
Data abstraction
Data abstraction is the process of hiding the internal details of an object and showing only the relevant details to the outside world. This helps in making the system more secure and also improves the maintainability of the code.