In this article basic concepts of oop we give the information about in C++, Data Abstraction is a technique by which only necessary data is shown to the user and unnecessary data is hidden.
Basic Concepts of OOP:
Class in OOP:
Class is a user-defined data type. The classis the most important feature of oops. it is called a group of objects of the same type.
For example: Potato, tomato and onion are all vegetables, and all of them will be members of the vegetable class.
Once the class is defined in the program, the programmer can create objects as per his wish. Its behavior, properties or attributes are defined related to the object of the class.
If there is an Animal. So his behavior in class, his body parts and their number can be defined by them.
A class is a collection of data member and member functions, which can be of both data types (int, float, char etc.) and derived data types (array, function etc.). In which the members are logically related to each other. These data members can only be accessed by the function members of this class. That is, the data cannot be accessed from outside the class. This is feature of OOP that is data security is achieved.
Object in OOP:
The Object is a basic run-time entity. Object is a variable of a class, Its like:- mobile, person, bike, watch, TV etc. All Objects are same. A class can also contain one or more objects. After the object is created, it takes up the same space in the memory as the variables.
Data Abstraction in OOPs:
In C++, Data Abstraction is a technique by which only necessary data is shown to the user and unnecessary data is hidden.
In other words, “Data Abstraction is a process by which only necessary data is provided and the internal data which is there is hidden.”
This is a very important feature of object-oriented programming (OOP). Through which we show the necessary information of the program to the user and hide the unnecessary information which is there.
Real life example: – Whenever we drive a bike. So we only know that if we press the accelerator, then the speed of the bike will increase. And if you press the brake, the bike will stop. But we do not know how the speed increases by pressing the accelerator and how the bike stops by pressing the brake.
C++ provides us with a high level of abstraction such as – pow() function is used to calculate the power of a number. But we do not know which algorithm this function follows.
In a C++ program, if we define a class with public and private members, then it is an example of data abstraction.
We can implement data abstraction in two ways:-
- Using class
- Abstraction in header files
Using Class –
We can implement abstraction by using class. Through a class, we can keep data members and member functions in a group using access specifiers. The class decides which data member will be visible outside and which will not.
Abstraction in Header Files –
In C++, another type of abstraction is header files. For example – pow() function is a part of math.h header file. Whenever we need to calculate the power of a number, we call pow(). But we are not aware of its algorithm.
Example of Data abstraction in C++
#include<iostream.h>
#include<conio.h>
class sum
{
// hidden data from outside world
private: int a,b,c;
public:
void add()
{
clrscr();
cout<<“Enter any two numbers: “;
cin>>a>>b;
c=a+b;
cout<<“Sum: “<<c;
}
};
void main()
{
sum s;
s.add();
getch();
}
OUTPUT:
Enter two numbers:
7
8
Sum of two number is: 15
Basic Concepts of OOP:
Data Encapsulation in OOPs:
In C++, encapsulation is a process that combines data members and functions together into a single unit.
In other words, “putting data and functions together inside a class is called encapsulation.”
Due to encapsulation we cannot access the data directly and it can be accessed only through the functions of the class. This is the most important feature of OOPs (object-oriented programming). Due to encapsulation, the data remains safe and it is not misused.
#include<iostream>
using namespace std;
class test
{
private:
int x;
public:
test(int a)
{
x =a;
}
int get()
{
return x;
}
};
int main()
{
test a(19);
cout<<“The Number is: “<<a.get();
return 0;
}
OUTPUT:-
The Number is: 19
In the above program, we have a private member X. We cannot access it from main() function and to access it we have created object of class test.
Basic Concepts of OOP
Inheritance in OOPs:
This is an important concept of OOPs. As we know that we can reuse codes to perform the same task in C++. The same happens in C++ inheritance.
In inheritance, we add the property and behavior of one class to another class that too without modifying the lass. In such a situation, some of the second class already has its own property or behavior, while by inheritance the second class also gets the property and behavior of the first class. The class which is inherited is called base or parent class while the class which is inherited is called derive or child class.
Basic Concepts of OOP
Polymorphism in OOPs:-
In C++, polymorphism means many forms. polymorphism is made up of two words poly and forms. It’s a geek word.
Simply put, “polymorphism means having many forms.” Polymorphism is an ability by which a message is displayed in many forms.
In C++, Polymorphism occurs when we have multiple classes which are related to each other through inheritance.
Dynamic Binding in OOPs:
Dynamic Binding is also called late binding and runtime polymorphism. This binding occurs at runtime.
In this, matching of the function call with the correct function definition takes place at runtime. It is achieved using virtual functions.
Since this happens in the runtime, the execution of the code in this is a bit slow.
The main advantage of dynamic binding is that it is flexible. In this, only one function can handle different types of objects at runtime.
using namespace std;
class A
{
public:
virtual void display()
{
cout<<“Base”;
}
};
class B:public A
{
public:
void display()
{
cout<<“Derived”;
}
};
int main()
{
B b;
A *a=&b;
a->display();
return 0;
}
Message Passing in OOPs:
In C++, all objects communicate with each other through messages, that is, objects communicate among themselves by sending and receiving messages from each other. Just like we send and receive information, so do objects too.
To complete the message passing, we have to follow the following things:-
We have to create a class which defines the objects and its behavior. After that creating objects from class definitions. Establishing communication between objects. A request to an object to perform one of its operations is called a message. Operation means function or method. A message does not happen automatically, it creates an interface for the object.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining