Object-Oriented Programming (OOP) continues to be a foundational programming paradigm widely used in the software development industry. With its emphasis on organizing code into reusable objects and encapsulating data and behaviour, OOP offers a powerful approach to designing and constructing robust and scalable applications.

In this introductory article, we will delve into the fundamental concepts of OOP, exploring its key structures, principles, advantages and disadvantages.

Basic Structures of Object-Oriented Programming

  1. Object - Objects are runtime entities created as instances of a class. The characteristics of an object are represented by its data and its behaviour by associated functions or methods.
  2. Class - Classes are user-defined data types with a set of data and code. Objects are variables of the variable type class. We can create any number of objects belonging to the class once a class has been defined.
  3. Methods - Methods are functions inside a class and can only be accessed through an object. They express the behaviour of objects.
  4. Attributes - Attributes store data in their field, representing the object's state.

Basic Principles of Object-Oriented Programming

Abstraction

Abstraction is the process of hiding unwanted details from the users, thereby only exposing essential features.

Let's consider the example of an electrical switchboard. We know only to press switches according to our requirements but have no idea what's happening inside the switchboard. This is an abstraction where only the necessary features are known, and the background details are hidden.

Encapsulation

Encapsulation is the process of associating a set of data and functions with a single unit. These data and functions are called members of objects. Members can only be accessed through a unit (Example. object of a class). We can only read, write and manipulate the data through member functions to prevent accidental alteration.

Inheritance

Inheritance allows inheriting properties and methods of another class. This supports the idea of re-usability. It allows adding additional features to an existing code without modification. Inheritance exhibits a transitive nature, i.e. if B class is derived from A and class C is derived from B, then C inherits the properties of A.

Polymorphism

Polymorphism is the capability of an object to perform different actions based on different messages. Operator overloading and virtual functions are examples of polymorphism.

Advantages and Disadvantages of Object-Oriented Programming

Advantages Disadvantages
Accurately represents real-world problems High compile time and execution time
Data is secure through abstraction More complicated to write
Managing and upgrading software is less complicated Compared to other options, Object-Oriented Programming is more CPU intensive
Offers more code reusability Object-Oriented Programming has a steeper learning curve

Examples of Object-Oriented Programming Languages

  1. C++
  2. Java
  3. Lisp
  4. Python
  5. C#
  6. PHP 5

Classes and Objects in C++

Let's implement classes and objects in C++ programming language.

Classes are created using class keyword in C++.

Syntax:

class Test{
    //members of class are defined here
};
Creating a Test class

Access Specifier

In the context of class composition, members can be categorized into three access modifiers: Public, private, and protected, each serving a unique purpose in determining their accessibility.

Public Members - Public members are intended for usage and access from both within and outside the class. This grants broader visibility and allows the members to be accessed and manipulated by external entities, such as other classes or functions.

Private Members - Private members, on the other hand, are restricted solely to the internal workings of the class itself. This encapsulation ensures that private members remain hidden from external entities, preserving data integrity and implementation details while upholding the principles of data encapsulation and information hiding.

Protected Members - Protected members fall in between public and private, allowing access within the class as well as by derived or inherited classes. This level of access provides a controlled means for sub-classes to interact with and extend the functionality of the base class while remaining restricted from general external access.

By employing these access modifiers, class designers can effectively manage the visibility and accessibility of class members, enforcing encapsulation, and promoting code reusability and maintainability.

Objects and Member Access

Objects are instances of class, so memory is allocated only after the creation of objects, not on the definition of class.

Public data members and member functions can be accessed using the dot(.) operator. If a pointer of the class is created, then the members are accessed using the -> operator.  

Test test = Test();
test.Input();
test.name;
Creating an Object called Test of Test class

Defining Member Functions

Member functions can be declared inside or outside of the class. However, member functions can only be defined inside a class.

✍️
The declaration only establishes the names and characteristics of a function; it does not allocate any memory for it. The definition specifies the body of function along with associating memory for it.
  1. Defining member function inside a class
class Test{
    int a;
    public:
    void Input(){
        cout<<"Enter the value of a ";
        cin>>a;
    }
};
Defining a member function, Input, inside the Test class

2. Defining member function outside a class

class Test{
    int a;
    public:
    void Input();
};

void Test::Input(){
    cout<<"Enter the value of a ";
    cin>>a;
}
Defining a member function, Input outside the Test class

Constructor

A constructor is a member function with the same name as the class name. Constructor has no return type and is called automatically when an object of a class is created.

Types of constructors

  1. Default Constructors
  2. Parameterized Constructors
  3. Copy Constructors
1. Default Constructor

Default constructors are constructors with no parameters.

#include <iostream>
using namespace std;

class Test{
    int a;
    public:
    Test(){
        cout<<"test object created"<<endl;
    }
    void Input(){
        cout<<"Input function called"<<endl;
    }
};

int main() {
    // Write C++ code here
    Test t1;
    t1.Input();

    return 0;
}
Creating a default constructor
2. Parameterized Constructor

Parameterized constructors are constructors which take arguments.

#include <iostream>
using namespace std;

class Test{
    int a;
    public:
    Test(int _a){
        a=_a;
    }
    void Input(){
        cout<<a<<endl;
    }
};

int main() {
    // Write C++ code here
    Test t1(5);
    t1.Input();

    return 0;
}
Creating a parameterized constructor
ℹ️
Here, we cannot create an object like Constructor c1 because we have no default constructor.
3. Copy Constructor

The copy constructor is a constructor that takes an argument of an object of the same type or class as a reference. It is used for initializing an object of a class through another object of the same class.

#include <iostream>
using namespace std;

class Test{
    
    int a ;
    int b;
  
    public:
    
    Test(int _a,int _b){
        a = _a;
        b = _b;
    }
    
    Test(Test &t2){
        a = t2.a;
        b = t2.b;
    }
    
    void Display(){
        cout<<a<<" "<<b<<endl;
    }
};

int main() {
    // Write C++ code here
    Test t1(5,6);
    Test t2(10,6);
    Test t3 = t1;
    t2 = t1;
    t1.Display();
    t2.Display();
    t3.Display();

    return 0;
}
Creating a copy constructor

Destructors

It is a member function with the same name as the class preceded by a tilde sign(~). Like a constructor, it does not have a return type, not even a void. It is invoked automatically when the object of the class goes out of the scope or is flushed from the memory.

#include <iostream>
using namespace std;

class Test{
    int a;
    public:
    Test(int _a){
        a=_a;
    }
    void Input(){
        cout<<a<<endl;
    }
    ~Test(){
        cout<<"object destroyed"<<endl;
    }
};

int main() {
    // Write C++ code here
    {
        Test t1(5);
    }
    return 0;
}
Creating a destructor ~Test
ℹ️
The output of the above code is: "The object destroyed"

Initialization List

C++ supports another method of initializing the class objects. This method is known as the initialization list in the constructor function.

#include <iostream>
using namespace std;

class Test{
    int a;
    int b;
    public:
    Test(int _a,int _b):a(_a),b(_b){
    }
    void Display(){
        cout<<a<<endl<<b<<endl;
    }
    ~Test(){
        cout<<"object destroyed"<<endl;
    }
};

int main() {
    // Write C++ code here
    Test t1(5,10);
    t1.Display();
    return 0;
}
Creating an initialization list in the constructor function
Thank you for reading this article. Watch this space for more upcoming articles in this Introduction To OOP article series. Consider subscribing and leaving a comment down below if you loved this article.