What makes the OOP better than POP? Explain with features of OOP.
Solution:
POP stands for 'Procedural Oriented Programming' which is the conventional programming approach. In POP, the entire program is viewed as a sequence of things to be done such as taking input, calculating and printing and the entire program is divided into smaller programs known as function and it gives more emphasis to function.
Object-oriented programming is the most recent concept in computer programming paradigms and it has been defined as "an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand".
Being the recent programming approach, OOP offers several benefits than POP approach and some of the most important benefits are as follows:
- In OOP, through inheritance, we can eliminate redundant code and extend the use of existing classes.
- Due to the uses of class, encapsulation is possible so that the necessary data members and the necessary member function for these data members can be wrapped inside the single unit called 'class'
- By using the proper access specifier like 'private' or 'protected', data can be hidden so that only the required member function can access them.
- In OOP, polymorphism is possible so that we can use the same operator or function to perform the various types of operations. For example, the operator can be used to find the sum of any two numeric values as well as to combine two string, Like 10+30 or "wel"+"come".
- We can build programs from the standard working modules that communicate with one another, rather than having to start writing the code from scratch. This leads to saving of development time and higher productivity.
- The principle of data ding helps the programmer to build secure programs that cannot be invaded by code in offer parts of the program.
- It is easy to partition the work in a project based on objects.
- Objected oriented system can be easily upgraded from small to large system.
- Message passing technique for communication between objects makes the interface description with external systems much simpler.
- Software complexity can be easily managed.
solution:
The virtual keyword is used in C++ when we need dynamic behavior. It will work only when objects exists where constructors are used to create objects. Constructors will be call at the time of object creation.So if we define the constructor as virtual,it should have existing object to use. But the object is created at the time of constructor call. Thus this is contradictory.
Again we can't override the constructor,when any method is declared as virtual,it should be overridden in its derived class. Since constructor is used for initializing variables of a class and it can be defined only within the class ,it can't be overridden. Thus the constructor can't be declared as virtual. We can use virtual destructor because, at the time of calling destructor,we have the objects .So we can use virtual keyword for the destructors.
We know that structure is a collection of heterogeneous data elements under a common name. It is a convenient tool for handling a group of logically related data items. It is a user-defined data type with a template that serves to define its data properties. Although structure is a user-defined data type, but it has some limitations.
Class is a collection of data members and member function bundled together as a unit. Using class, encapsulation as well as data hiding has been possible. Similarly, inheritance and polymorphism has been possible due to class. So, it is the more advanced data structure than the structure of C and it has been removed the drawbacks of the structure. The main drawbacks of structure of Care as follows:
- The standard C does not allow the struct data type to be treated like built-in data types. For example
{
float real;
float imaginary;
};
struct c1,c2,c3;
c3=c1+c2;
The member variable of structure 'complex' can easily be assigned values using the dot(.) operator, but we cannot add two complex numbers or subtract one from the other. So, c3=c1+c2 or c3=c1-c2 both are illegal in C.
But, in C++, we can design a complex class and add or subtract them using + or- operator by the help of polymorphism. For example:
class complex
{
private:
float real;
float imaginary;
public:
complex operator+(operator);
complex(float, float);
}
Complex c1.c2,c3;
C3=c1+c2;
- Another important limitation of C structure is that they do not permit data hiding. Structure members can be directly accessed by the structure variables by any function anywhere in their scope. So, structure members are public members.
For example:
The data members of the structure can be easily accessed by the structure variable in any part of the program such as c1.real, c1.imaginary even from the 'main()' function but the data members of the class cannot be accessed directly outside their member function. So, c1.real or c1.imaginary is not allowed directly in the 'main()' function of the program.
- Another limitation of structure is that the member of one structure cannot be transferred to the another structure but 'class' of COP has solved this limitation so that the features of one class can be easily transferred to another class by means of inheritance.
{
public:
void area(int l, int b)
{
cout<<"area="<< a;
}
};
void main()
{
rectangle r1;
r1.area(10,5); //example of message passing
}
solution:
The virtual keyword is used in C++ when we need dynamic behavior. It will work only when objects exists where constructors are used to create objects. Constructors will be call at the time of object creation.So if we define the constructor as virtual,it should have existing object to use. But the object is created at the time of constructor call. Thus this is contradictory.
Again we can't override the constructor,when any method is declared as virtual,it should be overridden in its derived class. Since constructor is used for initializing variables of a class and it can be defined only within the class ,it can't be overridden. Thus the constructor can't be declared as virtual. We can use virtual destructor because, at the time of calling destructor,we have the objects .So we can use virtual keyword for the destructors.
3 comments:
It helped in my final exam preparation. Thanks for this post
Thanks
The way you break down complex topics into easily understandable explanations made a huge difference in how I approached my studies. I truly appreciate the effort, time, and passion you put into curating such high-quality and reliable content.
Post a Comment