OBJECT ORIENTED PROGRAMMING (OOP): EVERYTHING YOU NEED TO KNOW

Sudarshan S
7 min readSep 17, 2022

--

Object Oriented Programming (OOP), one of the most interesting topics in programming has many advantages over procedural programming. We’ll be also discussing about some important methods in OOPs, which are Encapsulation, Inheritance, Polymorphism, Abstraction. In this series of learning, we’ll be mostly using C++ for better understanding. But the same concepts are applicable for other Object oriented languages.

Class

Classes can be defined as the prototype, idea, template or blueprint of an object. Classes are the “building blocks” of Object Oriented Programming (OOPs). The class holds its own data members and member functions, which can be accessed and used by creating an instance of that class. While declaring a class, no memory is allocated.

Data members: Variables inside the class definition
Member functions: The function definitions that are inside the class or those that are present outside the class but belongs to the class

Class can be defined using the keyword, “Class”, followed by the name of the class. A class can be defined only once within a program, i.e. each class should have unique class names. You can’t define a single class, multiple times in a program.

class Student{
// Access specifier
// Data members
// Member functions
};

Object

An object is the instance of a class. The memory is created only after the initialization of an object, until no memory is allocated. Objects are those which has state and behaviour. Here, state means data and behaviour means functionality.

“n” number of objects can be created for a class. An object can be created by using the class name followed by name of the object.

Student a; // an object 'a' is created for the class Student

Constructor function

The Constructor function is basically a function which is called automatically whenever an object is created for a class. The constructor function is like a default function that will be executed if we access the class.

The Constructor function has the function name as same as the class name. There will be no return values in the constructor function, hence no return type. Constructors are mostly declared in the “public” section of the class.

class Student{
public:
Student(){
cout << "Enter the RollNo:";
cin >> rno;
cout << "Enter the Name:";
cin >> name;
}
}

Here, if the Class Student is accessed, then the constructor function is automatically called and executed.

Even if we don’t declare a constructor function, the compiler automatically creates a constructor function when an object for a particular class is created.

Constructors are of three types:
1. Default
2. Parameterized (constructors with arguments)
3. Copy constructors

Destructor function

Destructor function, when called will destroy the constructor function. It is executed when the function ends, program ends, a block containing local variable ends or the delete operator is called.

Destructors also have the same name as the class preceded by the tilde (~) symbol. Destructor also doesn’t have any return type and arguments.

class Student{
public:
Student(){ //Constructor
cout << "Enter the RollNo:";
cin >> rno;
cout << "Enter the Name:";
cin >> name;
}
~Student(); //Destructor
}

If own destructor wasn’t written in class, compiler creates a default destructor for us. When a class contains a pointer to memory allocated in class, we should write a destructor to release memory before the class instance is destroyed.

Access Specifiers

Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members.

In the above constructor and destructor example codes, hope you have noticed that there is something called “public”. They are called access specifiers. These access specifiers are used to detemine the scope of the variable, i.e. whether the variables declared within a class can be accessed anywhere, within the derived class or cannot access anywhere.

The Access specifiers are:
1. Public
2. Private
3. Protected

Public

The variables or functions that are declared under this “public” access specifiers can be accessed anywhere in the program, i.e. it can be accessed outside the class.

class Student{
public:
int roll_no;
}
int main(){
Student A;
cout >> A.roll_no; //Valid
return 0;
}

Protected

Variables under “protected” access specifiers can be accessed within the class and also within the derived class but can’t accessed outside the class.

class Student{
protected:
int roll_no;
}
class Child_student : public Student{
public:
int new_rollno = roll_no + 1; //Can access protected
roll_no from this derived
class
}
int main(){
Student A;
cout >> A.roll_no; //Invalid
return 0;
}

Private

The Private variables cannot be accessed anywhere outside the same class.

class Student{
private:
int roll_no;
}
class Child_student : public Student{
public:
int new_rollno = roll_no + 1; //Invalid
}
int main(){
Student A;
cout >> A.roll_no; //Invalid
return 0;
}
Photo by Juanjo Jaramillo on Unsplash

Encapsulation

The process of hiding the “sensitive data” from users for accessing is called encapsulation. To achieve this, the data members should be declared as private. If you want to allow the users to modify or access those data members, then you can achieve this by providing public getter and setter methods.

class Employee {
private:
int salary; // private data member
public:
//Setter
void setSalary(int s) {
salary = s;
}
//Getter
{
int getSalary() {
return salary;
}
};
int main() {
Employee myObj;
myObj.setSalary(50000);
cout << myObj.getSalary();
return 0;
}

In the above code, we can set the value of salary to 50000 by the setter method. Then, we can print the salary value by the getter function. So, here, the objective is that we can access the private data members within a class by using some public functions.

Getter

Basically, getter is a function that is used to get the value and display it to the user.

Setter

Setter is a function to assign or set values to a variable which is declared as private within a class.

Inheritance

Inheritance is the capability of a class to derive properties and characteristics of another class.

The syntax to inherit a class from its base class is,

class subclass_name : access_mode base_class_name 
[
//body of the subclass
}

Sub Class

The class that inherits the properties from another class

Super Class

The class whose properties are inherited by a sub class

//Base class or Parent class
class Vehicle {
public:
string brand = "Ford";
void display() {
cout << brand;
}
};
//Derived class or child class
class Car : public Vehicle {
public:
string model = "Mustang";
}
};
int main() {
Car myCar; // Object myCar is created for class Car
myCar.display;
cout << myCar.brand + " " + myCar.model;
return 0;
}

In the above code, though we haven’t declared the string brand in the sub class Car, we can access the attribute brand from its base class because it was inherited in the sub class.

Modes of inheritance

Public

If you derive a sub class from public base class, then the public attributes in the base class will become public and the protected attributes in the base class will become protected.

Protected

If the sub class is derived from the protected base class, then the public and protected attributes will become protected in the sub class.

Private

If the sub class is derived from the private base class, then the public and protected attributes will become private in the sub class.

Types of Inheritance

Single Inheritance

Single inheritance, is deriving a sub class from only one base class.

Multiple Inheritance

Multiple inheritance, is deriving the properties of more than one class to a sub class.

Multi-level Inheritance

This type of inheritance is done by inheriting the properties of a derived class or sub class.

Multi-level inheritance

In the above diagram, we can understand clearly that the Class C is derived from an inherited class “Class B”. Hence, the Class C can also access the attributes and properties of Class A as Class B is inheriting the properties of Class B.

Hierarchial inheritance

In this type of inheritance, more than one sub class is inherited from a base class.

Hierarchial inheritance

Hybrid Inheritance

Hybrid inheritance is basically implementing more than one type of inheritance. For example: in the following diagram, the Hierarchial and Multiple inheritance are implemented.

Hybrid inheritance

Polymorphism

Polymorphism, means “many forms”, and it occurs when there are many classes that are related to each other by inheritance. That is, it allows to use single action in different ways.

So, that’s all about the “Object Oriented Programming”. I have almost covered the basics of OOP. Though I have used C++ for examples and code snippets, the same will be applied for other Object oriented languages.

Happy learning!

--

--

Sudarshan S

Tech enthusiast | Developer | Machine learning | Data science | Cybersecurity