×

Features of OOPS in C++

What is OOPs?

The main reason programmers prefer C ++ language over C is because of the support of object-oriented programing in C++. As the name suggests, object-oriented programming or OOPs uses objects, or a program is designed using classes and objects in object-oriented programming. In OOPs, an object is a real-world entity, such as a pen, a chair, a table, etc. There are mainly three OOPs features that can make it different from non-OOPs language, which are inheritance, data binding, polymorphism, etc.

Features of OOPs

OOPS provides many features, which are:

  1. Classes and objects
  2. Abstraction
  3. Inheritance
  4. Polymorphism
  5. Dynamic Binding
  6. Message Passing

1. Classes and objects

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. Class in a program acts as a blueprint for the object. An object is created for the class, and then we can use the object name to access the class or class member functions. The class generally will not take up any space in the memory

Example:

#include <iostream>
using namespace std;
class Student {
	 Public:
			int id;
			string name;
};
int main() {
		Student s1; //creating an object of Student class
		s1.id = 305; //assigned the value of id using object
		s1.name = "example.com";; //assigned the value of name using object
		cout<<s1.id<<endl;
		cout<<s1.name<<endl;
		return 0;
} 

Output:

Features of OOPS in C++

Explanation:

In the above example, we have created a class with the name student. In the main class, we created an object for the class, and then, using the object name, we assigned values to the variables and then

Abstraction

A user's attention is diverted from irrelevant information by abstraction. This means that the process of abstraction involves hiding irrelevant information from the user. For example, if we open an app on our phone, the information about how the app opens in the background is hidden from the user. This is known as abstraction. This is very useful as the user will not know the unnecessary information in the abstraction process. We can use private keywords in the code so that other users cannot access the code.

#include <iostream>
using namespace std;


class Abstraction
{
	private:
		//private only this class can access the private variables
		int a, b;


	public:




		void set(int x, int y)
		{
			a = x;
			b = y;
		}


		void display()
		{
			cout<<"a = " <<a << endl;
			cout<<"b = " << b << endl;
		}
};


int main()
{
	//object is created for the Abstraction class
	Abstraction obj;
	obj.set(50, 60);
	obj.display();
	return 0;
}

Output:

Features of OOPS in C++

Explanation:

In the above code, we have created a class in which we have written code with some variables under private specifier. The variables in the private code are only accessible to the code in that class. In the main part of the code, we have set the value of the varialbes in the private section of the code using the function members.

Encapsulation

The encapsulation or combination of data and its methods or functions is the process by which they are combined. In oops, we can do encapsulation by making the function members in the class private and only the public function to access the private functions.

Polymorphism

Polymorphism means having many forms. Polymorphism helps the coder to create an overloading function. Polymorphism means we can create different functions that can perform similar operations.

#include <iostream>
using namespace std;
class Animal {
		public:
void eat(){
cout<<"Eating...";
		}
};
class Dog: public Animal
{
 public:
 void eat()
		{           cout<<"Eating bread...";
		}
};
int main(void) {
	 Dog d = Dog();
	 d.eat();
	 return 0;
}    

Output:

Features of OOPS in C++

Explanation:

In the above code, we have created two classes, Animal and dog, and we have inherited the animal characteristics into the dog class. In the main class, when we created an object for the dog class, we called the function present in the dog class to eat().

Dynamic binding

In oop, function calls are resolved at runtime through dynamic binding. When a function call is made, runtime decisions are made about what code to execute.

Message Passing

The messages used in OOPS allow objects to communicate with one another. An object message consists of the object name, method name, and actual data. When objects communicate, information is passed back and forth between the objects.


Related Topics

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

C++ Global Variable

In C++, a global variable is a variable that is defined outside of any function or class and can be accessed by any function or class in the program. Global...

4 minutes read.

Roadmap to C++ Programming

Introduction There are so many programming languages available in the market, but among them, C++ is something that never lost its charm. It has a powerful impact on the programming world....

4 minutes read.

C++ Try-Catch

Every useful program will eventually encounter unexpected outcomes. By entering data that are incorrect, users might create mistakes. Sometimes the program's creator didn't consider all of the options or was...

7 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.

Converting string into integer in C++

When programming in C++, we'll frequently need to change one data type to another. When we use C++ to create apps, we must transform data from one type to another. When...

7 minutes read.

How to calculate size of string in C++

What is string in C++? In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the...

4 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.

C++ Interfaces

The C++ programming language provides programmers with a variety of capabilities and functions. It also enables object-oriented programming, which is essential while working on a project. It will be simple...

7 minutes read.

Static keyword in C++ vs Java

Both in C++ and Java, the static keyword is employed for essentially the same function. But there are some variations. The static keyword's similarities and differences between C++ and Java...

3 minutes read.

Vector resize() in C++

Vector resize() in C++ Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage. The function modifies the...

3 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

Program to convert infix to postfix expression in C++

Parentheses are frequently employed in mathematical formulas to make their interpretation easier to understand. However, with computers, parenthesis in an expression might lengthen the time it takes to find a...

7 minutes read.

C++ Program to find the largest number formed from an array

Given an array, write a program to find the largest number that will be formed from the elements of the array. Arrangement should be done in such a way that...

4 minutes read.

Containership in C++

In C++, it is possible to create an object in one class into another class, and that object is a member of another class. This relationship is known as a...

4 minutes read.

Functions in C++ with Types and Examples

A function is a collection of statements that work together to complete a certain goal. It could consist of statements that execute repetitive operations or statements that conduct specialized jobs...

10 minutes read.

C++ vardiac() function

In the C++ programming language, the flexibility feature is provided by the variadic function. To understand more about flexibility, let's see the following syntax. Syntax: If we have to add two numbers,...

3 minutes read.

What does Buffer Flush mean in C++

A buffer flush, to explain simple layman's terms, is nothing but the transfer of computer data which is being stored in a rentable temporary memory of your computer running either...

3 minutes read.