Monday, February 19, 2024

OOPS CONCEPT IN C++

Object-oriented programming in C++


OOPS CONCEPT

ADVANTAGES

WHY DO WE NEED OOPS IN C++


The main goal of C++ programming is to introduce the concept of object orientation in C

programming language.

Object-oriented programming is a paradigm that offers many concepts such as inheritance,

data binding, polymorphism, etc.

A programming paradigm where everything is represented as an object is known as an object-

oriented programming language. Smalltalk is considered the first truly object-oriented

programming language.

 

Object-oriented programming aims to implement real units in programming such as

inheritance, hiding, polymorphism, etc. The main goal of OOP is to tie data and the functions

that operate on them together so that neither of them happens. other parts of the code can

access that data besides the function in question.

 

Object means a real set of
words, such as pen, chair, table, etc. Object-oriented programming is a
methodology or paradigm for designing a program using classes and objects. It
simplifies software development and maintenance by providing a few concepts: -

 

  • ·      Class
  • ·      Objects
  • ·      Encapsulation
  • ·      Abstraction
  • ·      Polymorphism
  • ·      Inheritance
  • ·      Dynamic Binding
  • ·      Message Passing


 Advantage of the OOPS concept over procedural-oriented programming

 

OOPs make development and maintenance easier, while in a procedurally oriented programming language, it
is not easy to manage when the code grows as the project size increases.

 

OOPs provide a data cache, while in a procedural programming language, global data can be accessed from
anywhere.

 OOPs offer the ability to simulate real events much more efficiently. We can give a solution to a real

text problem if we use an object-oriented programming language.

 Why do we need OOPS in C++?

 Early programming methods had several drawbacks and poor performance. That approach could not effectively

solve real-world problems because, like procedural programming, code could not
be reused within a program, there were difficulties with global data, etc.

 

By using classes and objects, object-oriented programming makes code easier. Because inheritance allows code
to be reused, the program is simpler by not having to write the same code multiple times. Information hiding is also provided by ideas such as encapsulation
and abstraction.

 

Let us understand OOPS concepts
in detail: -

 

Class

The class is the basic building block of C++ that leads to object-oriented programming. The user defines the
data type, and it has its own data members and member methods that can be used by making an instance of this class. There is a plan for an object in a class.
Such as: Think about the class of cars. While different cars may have different names and brands, they all share some things, like four wheels, a speed limit,
gas usage, and so on. So, this is the car's class, and its wheels, top speed, and gas usage are its features.

 

A class is a user-defined data type with data members and member functions. Data members are data variables
and member functions are operations used to manipulate these variables together. These data members and member functions define the properties and
behavior of the objects of the class.

 

In the above example, the data member of the Car class is speed limit, mileage, etc., and the member functions
can break, increase speed, etc.

A class in C++ can be said to be a plan representing a group. of objects that share some common properties and
behaviors.

 

Object

 An object is a recognizable entity with certain characteristics and behavior. An object is an instance of a

class. When a class is defined, memory is not allocated, but when it is instantiated (i.e., an object is created), memory is allocated.

 Objects occupy space in memory and have an address associated with them, such as a record in Pascal or a

structure or union. During the execution of a program, objects communicate by sending messages to each other. Each object contains data and code to manipulate
the data. Objects can communicate without having to know the details of each other's data or code, simply by knowing the type of message received and the
type of response returned by the objects.

For ex

syntax/working of Objects as a

#include <iostream>

using namespace std;

class person

{

             
char name[20];

             
int id;

public:

             
void getdetails() {}

};

int main()

{

             
person p1; // p1 is a object

             
return 0;

}


 

Encapsulation

 A definition of encapsulation is the process of combining multiple pieces of data or information into one.

Encapsulation, as used in object-oriented programming, it is the process of connecting together data and processing operations. Here's a real-life example
of encapsulation in action: a corporation has many departments, such as accounting, finance, sales, etc. All monetary transactions and associated
financial data are handled by the finance department.

 

Similarly, the sales department handles all sales functions and maintains records of all sales. Now, a
situation may arise where an officer in the financial department for some reason needs all the information about the sales of a certain month. In this case,
he does not have the right to directly access the information of the sales department. He must first contact another sales department employee and then
ask him to provide this information. This is encapsulation. Here, the information of the store and the employees who process it are wrapped up under
the single name "sales department".

 

Encapsulation also leads to data abstraction or data hiding. Using encapsulation also hides data. In the above
example, information from any section (such as Sales, Finance or Accounts) is hidden from all other sections.

 Abstraction

 Encapsulation also leads to data

abstraction or data hiding. Using encapsulation also hides data. The information of any department, including accounting, finance, or sales, is kept
secret from all other departments in the aforementioned example. In C++ object-oriented programming, data abstraction is among its most significant
characteristics.

Abstraction means showing only important information and hiding details. Abstracting data means conveying only

important information of data to the outside world, hiding background information or implementation. let's take a real-world example in this example
a man is driving a car. He only knows that pressing the gas pedal increases the speed and pressing the braking will stop the car, but he doesn't know the
mechanism behind how the car speed increases when he presses the gas pedal. In other words, he does no know the internal working of the car, or introducing the
gas pedal, brakes, etc. of the car.

This is what abstraction is:

 Abstraction using classes: We can implement abstraction in C++ using classes. A class helps us group data
members and member functions using available accessor attributes. A class can decide which data member is visible to the outside world and which is not.

 Abstraction in header files: Another type of abstraction in C++ can be header files. For example,
consider the pow() method in the (math. h) header file. Without understanding the underlying algorithm by which the pow() function in the
(math. h) header file computes the power of a number, we just call the function and supply the numbers as arguments.

Polymorphism

Many forms are the definition of polymorphism; that is, the capacity of a message to take on several forms. As a person may possess several skills
simultaneously. Like a man can be a father, a husband, and an employee at the same time. This indicates that a person can
have different responsibilities and behaviors in different situations. This is called polymorphism. A function may behave differently in different cases. The
behavior depends on the data types used in the operation. C++ supports operator overloading and function overloading.

Operator overloading: Making an operator behave differently in different cases is called operator overloading.

Function Overloading: Function overloading uses a single function name to perform different types of
operations. Polymorphism is widely used to implement inheritance.

Example: Suppose we have to write a function to add some integers, sometimes there are 2 integers and
sometimes there are 3 integers. We can write and add a method with the same name with different parameters, this method is named according to the parameters.

 

Int main()

Sum1 =sum(20,30);

Sum2=sum(20,30,40);


Int sum(int a, int b, int c)

{

Return (a+b+c)

}

 

Int sum (int a, int b)

{

Return(a+b)

}

 

Inheritance 

Inheritance is the capacity of a class to obtain features and traits from another class. Among the most significant
characteristics of object-oriented programming is inheritance.
Subclass: A subclass, sometimes referred to as a derived class, is one that
takes attributes from another class.
Superclass: A base class or superclass is a class that inherits properties from
a subclass.
Reusability: Inheritance facilitates the idea of "reusability," which is the ability to derive a new class from an existing class if we wish to
construct one and it already has part of the code we need. We repurpose the fields and methods of a current class in this way.

Example: Dog, cat, and cow can
be derived from the main animal category.

 

Dog class

 

Cow class

Cat class

 

Animal class

Dynamic binding

In dynamic binding, the code to be executed in response to a function call is decided at runtime. C++ has
virtual functions to support this. Because dynamic binding is flexible, it avoids the disadvantages of static binding, which combines function call and
definition during construction.

 

Let’s try an example to demonstrate
The concept of Dynamic binding with the help of virtual function

 #include <iostream>

using namespace std;

class sms

 {

public:

             
void call_Function() // function that call print

             
{

                            
print();

             
}

             
void print() // the display function

             
{

                            
cout << "Printing the Base class Content" << endl;

             
}

};

class sms2 : public sms // sms2
inherit a publicly

{

public:

             
void print() // sms2's display

             
{

                            
cout << "Printing the Derived class Content"

                                          
<< endl;

          
}

};

int main()

{

             
sms; // Creating cde's object

             
sms.call_Function(); // Calling call_Function

             
sms2; // creating sms2 object

             
sms2.call_Function(); // calling call_Function

                                                                                                                                 
// for sms2 object

             
return 0;

}

 

Output

 

Printing
The base class Content

Printing
The base class Content

As we can see, the print()
function of the base class is called even by derived class objects. To solve
this we use virtual functions.

 Message Passing

 Objects communicate with each other by sending and receiving information. The object's
message is a request to perform an action and therefore invokes an action on the receiving object that produces the desired results. Sending a message
contains the name of the object, the name of the function, and the definition of the information to be sent

 

No comments:

Post a Comment

Featured Post

ASSOCIATION RULE IN MACHINE LEARNING/PYTHON/ARTIFICIAL INTELLIGENCE

Association rule   Rule Evaluation Metrics Applications of Association Rule Learning Advantages of Association Rule Mining Disadvantages of ...

Popular