Object-Oriented programming or OOPs

 OOPs Definition

Object-Oriented Programming is basically a programming style that we used to follow in modern programming. 

It primarily revolves around classes and objects. Object-Oriented programming or OOPs refers to the language that uses the concept of class and object in programming.

 The popular object-oriented programming languages are c++, java, python, PHP, c#, etc

 The main objective of OOPs is to implement real-world entities such as polymorphism, inheritance, encapsulation, abstraction, etc.

 The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.


Class

A class is a logical entity used to define a new data type. A class is a user-defined type that describes what a particular kind of object will look like. Thus, a class is a template or blueprint for an object. A class contains variables, methods, and constructors. 

Classes are very useful in programming. Consider the example of where you don't want to use just one car but 100 cars. Rather than describing each one in detail from scratch, you can use the same car class to create 100 objects of the type 'car'. You still have to give each one a name and other properties, but the basic structure of what a car looks like is the same. 

The car class would allow the programmer to store similar information unique to each car (different models, maybe different colors, etc.) and associate the appropriate information with each car.

Syntax to define a class:

class class_name{ 

// class body

 //properties

//methods 

};

Here,

❖ class: class keyword is used to create a class in C++. 

❖ class_Name: The name of the class. 

❖ class body: Curly braces surround the class body. 

❖ After closing curly braces, a semicolon(;) is used.


Object 

An object is an instance of a Class. It is an identifiable entity with some characteristics and behavior. Objects are the basic units of object-oriented programming.  It may be any real-world object like a person, chair, table, pen, animal, car, etc. A simple example of an object would be a car. Logically, you would expect a car to have a model number or name. This would be considered the property of the car. You could also expect a car to be able to do something, such as starting or moving. This would be considered a method of the car. 

Code in object-oriented programming is organized around objects. Once you have your objects, they can interact with each other to make something happen.

 You need to have a class before you can create an object. When a class is defined, no memory is allocated, but memory is allocated when it is instantiated (i.e., an object is created). 

Syntax to create an object in C++:

class_name objectName;

Syntax to create an object dynamically in C++: class_name * objectName = new class_name(); 

Here, 

❖ objectName: It is the name of the object created by class_name. 

The class’s default constructor is called, and it dynamically allocates memory for one object of the class. The address of the memory allocated is assigned to the pointer, i.e., objectName. 

Features of OOPs:

Four major object-oriented programming features make them different from non-OOP languages: 

● Abstraction is the property by virtue of which only the essential details are displayed to the user.

 ● Inheritance allows you to create class hierarchies, where a base class gives its behavior and attributes to a derived class. 

● Polymorphism ensures that it will execute the proper method based on the calling object’s type.

 ● Encapsulation allows you to control access to your object’s state while making it easier to maintain or change your implementation at a later date.


Real-world class modeling example

Let’s take a real-world example of Animal as a class to understand concepts better. And Mammals, birds, and amphibians as objects of the class. 

Creating a class Animal and objects mammal, amphibian and bird:

#include <iostream> Using namespace std; 

// creating Animal class class Animal

bool gives_birth; 

bool lay_egg; 

boollive_in_ground;

 boollive_in_water;

boolhave_wings;

 };

 intmain()

{

 // creating an object of animal class Animal mammal; //assign values to instance variables mammal.gives_birth =true;

 mammal.lay_egg =false;

 mammal.live_in_ground =true; 

mammal.live_in_water =false; 

mammal.have_wings =false; 

Animal amphibian; 

amphibian.gives_birth =false; 

amphibian.lay_egg =true; 

amphibian.live_in_ground =true;

 amphibian.live_in_water =true; 

amphibian.have_wings =false; 

Animal bird; bird.gives_birth =false; 

bird.lay_egg =true; 

bird.live_in_ground =true; 

bird.live_in_water =false; 

bird.have_wings =true;

 }

 We all know animals are a creature of God, every animal is different from each other, but they also possess some unique properties. Here we create a class Animal and define some animal characters (properties) that may be shared for different kinds of animals. We defined all the properties for each object, like whether they give birth or not, whether they live in water, etc.

 Here, Animal class provides a template or blueprint for creating objects (mammal, bird, and amphibian). 


Why do we need object-oriented programming? 

❖ To make the development and maintenance of projects more effortless.

❖ To provide the feature of data hiding that is good for security concerns. 

❖ We can solve real-world problems if we are using object-oriented programming. 

❖ It ensures code reusability. 

❖ It lets us write generic code: which will work with a range of data, so we don't have to write basic stuff over and over again.


More About Class

Class is a blueprint or a set of instructions to build a specific type of object. It is a fundamental concept of Object-Oriented Programming which revolves around real-life entities. 

Class determines how an object will behave and what the object will contain. Data encapsulation is supported with “class”. 

The class consists of both data and functions. The data in a class is called a member, while functions in the class are called methods.

 Data Members:- 

The variables which are declared in any class by using any fundamental data types (like int, char, float, etc.) or derived data types (like class, structure, pointer, etc.) are known as Data Members.

 Methods:-

 A method is the equivalent of a function in object-oriented programming that is used inside classes. The methods are the actions that perform operations. A method accepts parameters as arguments, manipulates these, and then produces an output when the method is called on an object. 

Constructor:- 

Constructors are special class functions that perform the initialization of every object. In C++, the constructor is automatically called when an object is created. It is a special method of the class because it does not have any return type. It has the same name as the class itself.

Example of smartphone class:

classsmartphone

{ //class body 

//Data Members(Properties) 

string model; 

int year_of_manufacture; 

bool_5g_supported; 

//Constructor

 smartphone(string mod,int manu,bool _5g_supp)

 //initialzing  data members 


model = mod; 

year_of_manufacture = manu; 

_5g_supported = _5g_supp;

 } 

//methods 

void print_details()

cout << "Model : " << model << endl;

 cout << "Year of Manufacture : " << year_of_manufacture << endl; 

cout << "5g Supported : " << _5g_supported << endl; } 

};

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence