Binding the data members and member functions together into a single class is called

Pushkar Kumar

Pushkar Kumar

SDE 2 @ Amazon Seattle | YouTuber | Mentor | Teacher System Design & Programming | Full Stack Java SpringBoot React AWS Developer

Published Feb 2, 2019

The four pillars for OOP are Abstraction, Encapsulation, Inheritance, Polymorphism.

  1. Abstraction : Abstraction is the process of showing only essential/necessary features of an entity/object to the outside world and hide the other irrelevant information. For example to open your TV we only have a power button, It is not required to understand how infra-red waves are getting generated in TV remote control.
  2. Encapsulation : Encapsulation means wrapping up data and member function [Method] together into a single unit i.e. class. Encapsulation automatically achieve the concept of data hiding providing security to data by making the variable as private and expose the property to access the private data which would be public.
  3. Inheritance : The ability of creating a new class from an existing class. Inheritance is when an object acquires the property of another object. Inheritance allows a class [subclass] to acquire the properties and behavior of another class [super-class]. It helps to reuse, customize and enhance the existing code. So it helps to write a code accurately and reduce the development time.
  4. Polymorphism: Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means "many forms". A subclass can define its own unique behavior and still share the same functionalities or behavior of its parent/base class. A subclass can have their own behavior and share some of its behavior from its parent class not the other way around. A parent class cannot have the behavior of its subclass.

Correct me if anything is wrong.

Sources: books.

Others also viewed

Explore topics

Get this book -> Problems on Array: For Interviews and Competitive Programming

Reading time: 45 minutes | Coding time: 5 minutes

The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types.

A class is a mechanism for creating user-defined data types. It is similar to the C language structure data type. In C, a structure is composed of a set of data members. In C++, a class type is like a C structure, except that a class is composed of a set of data members and a set of operations that can be performed on the class.

In other words, these are the building block of C++ that leads to Object Oriented programming.It is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.
A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.That means, the variables inside class definition are called as data members and the functions are called member functions.

All in all, it can also be said that a class is a way to bind the data and its associated functions together in a single unit. This process is called Encapsulation.

Example-1:

Class of birds, all birds can fly and they all have wings and beaks. So here flying is a behavior and wings and beaks are part of their characteristics. And there are many different birds in this class with different names but they all posses this behavior and characteristics.

That means, a Class is just a blue print, which declares and defines characteristics and behavior, namely data members and member functions respectively. And all objects of this class will share these characteristics and behavior.

Example-2:

Consider the Class of Cars. There may be many cars with different names and brand but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class and wheels, speed limits, mileage are their properties.
Here, the data member will be speed limit, mileage etc and member functions can be apply brakes, increase speed etc.

C++ Class

When we define a class, we define a blueprint for a data type. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.
A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example, we defined the Box data type using the keyword class as follows −

class Student    
 {    
     public:  
     int id;          //field or data member     
     float salary;   //field or data member  
     String name;   //field or data member    
 };   

In the above example, the keyword public determines the access attributes of the members of the class that follows it. A public member can be accessed from outside the class anywhere within the scope of the class object. We can also specify the members of a class as private or protected.

An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated [i.e. an object is created] memory is allocated.

Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.

Each object has different data variables. Objects are initialised using special class functions called Constructors.

And whenever the object is out of its scope, another special class member function called Destructor is called, to release the memory reserved by the object. C++ doesn't have Automatic Garbage Collector like in JAVA, in C++ Destructor performs this task.

In C++, object is a group of similar objects. It is a template from which objects are created. It can have fields, methods, constructors etc.
We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of any class Box −

    Box Box1;          // Declare Box1 of type Box
    Box Box2;          // Declare Box2 of type Box

Both of the objects Box1 and Box2 will have their own copy of data members.
Syntax :

    ClassName ObjectName;

Accessing data members and member functions :

The data members and member functions of class can be accessed using the dot[‘.’] operator with the object.

1. Accessing data members :-

Example-1 : if the name of object is obj and you want to access the member function with the name printName[] then you will have to write obj.printName[].
Lets now, understand through code :-

Example-2:

# include
using namespace std; 
class Box 
{
   public:
      double length;   // Length of a box
      double breadth;  // Breadth of a box
      double height;   // Height of a box
};

int main[] 
{
   Box Box1;        // Declare Box1 of type Box
   Box Box2;        // Declare Box2 of type Box
   double volume = 0.0;     // Store the volume of a box here
 
   // box 1 specification
   Box1.height = 5.0; 
   Box1.length = 6.0; 
   Box1.breadth = 7.0;

   // box 2 specification
   Box2.height = 10.0;
   Box2.length = 12.0;
   Box2.breadth = 13.0;
   
   // volume of box 1
   volume = Box1.height * Box1.length * Box1.breadth;
   cout 

Chủ Đề