C++ OOPs Concepts

What is OOP?

OOP (object-oriented programing) is a structure or paradigm based on an object that represents or contains the whole data of a class in the form of data and methods. The concepts of OOP are,

  • Class: class is a user-defined structure that contains data members and member functions and provides various functionalities in a single block of statements.
  • Object : Object represents class. In the object, data and methods of any class are combined, and you can access any data or method of class using its respective object. For performing any operation on class data, the object is commonly used.
  • Encapsulation: Encapsulation refers to the wrapping of various data into a single unit. Encapsulation does wrapping of data which helps to access it easily. Class is the best example of encapsulation where data members and member functions get wrapped up into a single unit known as an object, making it easier to access.
  • Inheritance: Inheritance means accessing data members and member functions of one class into another class or accessing data members and member functions of parent class by child class is known as inheritance. There are five types of inheritance,
  • Single
  • Multiple
  • Multilevel
  • Hierarchical
  • Hybrid
  • Polymorphism: Polymorphism can provide various functionalities with the same
  • Abstraction: Abstraction is the method where the complexity of code is hidden. This shows only the easy part to the user and reduces the complexity.


#include <iostream> #include <string> using namespace std; class MyClass { // The class public: // Access specifier int x; // Attribute (int variable) string str; // Attribute (string variable) void display() { cout <<x << "\n"; cout <<str<<"\n"; } }; int main() { MyClass obj1,obj2; // Create an object of MyClass // Access attributes and set values obj1.x = 15; obj1.str = "Welcome to www.Knowledge2life.com "; obj2.x = 1; obj2.str = "Welcome to www.Knowledge2life.com "; // Print values obj1.display(); obj2.display(); return 0; }


string type