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
- 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.
- 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.
- Methods - Methods are functions inside a class and can only be accessed through an object. They express the behaviour of objects.
- Attributes - Attributes store data in their field, representing the object's state.
Basic Principles of Object-Oriented Programming
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 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 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 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
|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
- 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++.
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
Defining Member Functions
Member functions can be declared inside or outside of the class. However, member functions can only be defined inside a class.
- Defining member function inside a class
2. Defining member function outside a class
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
- Default Constructors
- Parameterized Constructors
- Copy Constructors
1. Default Constructor
Default constructors are constructors with no parameters.
2. Parameterized Constructor
Parameterized constructors are constructors which take arguments.
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.
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.
C++ supports another method of initializing the class objects. This method is known as the 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.