×

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

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

How to Handle Divide by Zero Exception in C++

If you are a programmer or interested in coding then it is obvious that you face some illogical test cases. Suppose, you have written one program that calculates the factorial...

6 minutes read.

How to Reverse a String in C++ using For Loop

For Loop: We may loop through a certain section of C++ code repeatedly using the for loop. A for loop is carried out if the test expression yields a true result. The...

4 minutes read.

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

4 minutes read.

C++ Program to move all zeros to the end of the array

Write a program to move all the zeros in the arr[] to the end. The order of the non-zero elements should not be altered and all the zeros should be...

3 minutes read.

Queue in C++

What is Queue? As the name suggests, the queue is the type of data structure that follows the FIFO (First In - First Out) mechanism. In simple words, it is...

4 minutes read.

C++ vs C#

What exactly is C++ programming? Bjorne Stroustrup is the creator of the C++ programming language. His goal was to create a powerful object-oriented programming language with the capabilities of C. It...

4 minutes read.

C++ Goto

In this article, we will discuss the C++ goto statement with its syntax, use, key features, key points, pseudo code, and examples. What is the goto statement in C++? In C++, the...

4 minutes read.

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

C++ Ternary Operator

In this tutorial, we'll learn about the C++ ternary operator and how to utilise it to manage the program's flow using examples. Ternary Operator: The if-else statement and the conditional operator use...

3 minutes read.

C++ STL (Standard Template Library)

Introduction C++ is a flexible type and general proposed programming language. So we need a standard library that supports C++. C++ STL (Standard Template Library) is a collection of templates that...

6 minutes read.

Parameterize Constructor

C++ Parameterized Constructor A constructor having parameters is known as parameterize constructor. Parameterize constructor is used to assign different values. Syntax: className(data-type argument){   // Constructor definition   }   className(data-type argument, data-type argument){   // Constructor definition   } A parameterized constructor can be passed values to constructor function in two ways: 1)...

1 minute 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.

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++ Features

C++ is a general-purpose programming language that evolved from the C language to include an object-oriented paradigm. It is a compiled and imperative language. Object-Oriented Programming Object-oriented programming language concepts: ClassObjectsEncapsulationPolymorphismInheritanceAbstraction Class: A Class...

4 minutes read.

10 Best C and C++ Books for Beginners & Advanced Programmers

If you want to become a skilled software developer, you should never stop learning, whether you're a working professional or a student. Why, therefore, only C or C++? The fundamental...

6 minutes read.

Iostream in C++

Using Iostream in C++, we can perform input and output operation capabilities. This represents input and output, and the stream is used to carry out this capability. A stream is...

4 minutes read.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

Default arguments in C++

Arguments in a function are defined as the values supplied when the function is called. The source is the values supplied, and the destination is the receiving function. Let us...

3 minutes read.