INTRODUCTION
In the realm of programming, Object-Oriented Programming (OOP) stands as a cornerstone, providing a structured approach to designing and implementing software solutions. In C++, OOP manifests through various fundamental concepts that empower developers to build robust and modular code. Let’s embark on a journey to explore these concepts and their practical implications.
WHAT IS OBJECT ORIENTED PROGRAMMING (OOPS)
Object-oriented programming (OOP) is a computer programming model that organizes software design around objects, rather than functions and logic. OOP is characterized by the following concepts:
- Objects: Objects are data fields that have unique attributes and behavior. They can contain data and code that manipulates that data.
- Classes: Classes are templates that define the properties and behavior of the objects they create.
- Methods: Methods are functions associated with classes
Classes and Objects:
At the heart of OOP lies the concept of classes and objects.
Class in C++ is the building block 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 serves as a blueprint for creating objects, encapsulating data and behavior within its scope.
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.
Defining class and objects
A class is defined in C++ using the keyword class followed by the name of the class. The body of the class is defined inside the curly brackets and terminated by a semicolon at the end.
Access Specifiers:
C++ provides access specifiers like public
, private
, and protected
to control the visibility of class members.
Members & Member functions:
Members are the data variables and member functions are the functions used to manipulate these variables together, these data members and member functions define the properties and behavior of the objects in a Class.
#include <iostream>
using namespace std;
class VisitorForm {
private:
string name;
public:
VisitorForm(string n){
name = n;
}
void welcome() {
cout << "Welcome to our office, " << name << endl;
}
};
Constructors:
Constructors are special member functions invoked automatically when an object is created. They initialize object state and can be user-defined or compiler-generated.
Here are some characteristics of constructors:
- Name: The constructor’s name is the same as the class name.
- Argument list: The constructor’s argument list does not define any data types.
- Return type: The constructor does not return any values.
- Multiple constructors: A class can have multiple constructors, as long as their signature (the parameters they take) are not the same.
EXAMPLE CODE
#include <iostream>
using namespace std;
class MyClass {
public:
// Default Constructor
MyClass() {
cout << "Object created!" << endl;
}
// Parameterized Constructor
MyClass(int x) {
cout << "Object created with value " << x << endl;
}
};
Inheritance:
Inheritance enables the creation of new classes based on existing ones, allowing for code reuse and the establishment of hierarchical relationships. Inheritance is one of the most important features of Object-Oriented Programming.
Inheritance is a feature or a process in which, new classes are created from the existing classes. The new class created is called “derived class” or “child class” and the existing class is known as the “base class” or “parent class”. The derived class now is said to be inherited from the base class.
When to Use Inheritance:
Inheritance is best used when you want to create a new class that extends the functionality of an existing class. For example, consider a Vehicle
class and a Car
class, a truck
class, a Bus
class:
For creating a sub-classes that is inherited from the base class we have to follow the below syntax.
Derived Classes: A Derived class is defined as the class derived from the base class.
Syntax:
Let’s create the above given example of Vehicle with sub-classes Car and Bus that will take basic information from object that is total fuel amount and total distance.
#include <iostream>
using namespace std;
class Vehicle {
protected:
int fuelamount;
int totaldistance;
public:
Vehicle(){
cout << "Welcome to your Vehicle section" << endl;
}
void getFuel()
{
cout << "Your fuel amount is " << fuelamount << " litre" << endl;
}
void getDistance()
{
cout << "Your total distance is " << totaldistance << " Km" << endl;
}
};
class Car:public Vehicle{
public:
Car(string name){
cout << "Hello " << name << ", You're at Car portal" << endl;
}
void setValues(int f, int d)
{
fuelamount = f;
totaldistance = d;
}
};
class Bus:public Vehicle{
public:
Bus(string name){
cout << "Hello " << name << ", You're at Bus portal" << endl;
}
void setValues(int f, int d)
{
fuelamount = f;
totaldistance = d;
}
};
int main(){
string A = "Tom";
string B = "Jack";
Car tom(A);
tom.setValues(50, 100);
tom.getFuel();
tom.getDistance();
cout << " " << endl;
Bus jack(B);
jack.setValues(100, 100);
jack.getFuel();
jack.getDistance();
}
Inline Functions in Classes:
Inline functions in C++ provide a mechanism for the compiler to substitute function calls with the actual function code at the call site, eliminating the overhead of function invocation. In the context of classes, inline functions can be declared and defined within the class definition itself, making them part of the class interface. This approach improves code readability by keeping related functionality together and reduces the need for separate function declarations and definitions.
The class by default has inline functions. So even if we doesn’t mention this inline keyword, it will always define the inline function.
class MyClass {
public:
inline void myFunction() {
std::cout << "Helllooo" << std::endl;
}
};
But this inline approach only beneficial when the code is small or simple, otherwise we have to define function outside the class because inside class it will always be inline.
Here we will use the Scope Resolution Operator
Scope Resolution Operator for Defining Class Functions Outside the Class:
There are cases where defining class functions outside the class definition becomes necessary. The scope resolution operator (::) is used to specify that a function is a member of a particular class, even when defined outside the class body.
void MyClass::myFunction() {
std::cout << "Helloooo" << std::endl;
}
Defining class functions outside the class allows for separation of interface and implementation, improving code maintainability and facilitating code reuse. Additionally, it enables the implementation of larger or more complex functions to be placed in separate source files, enhancing modularity and scalability.
But when we use this approach where we define the function outside the class using scope resolution operator, first we have to initiate that function inside the class first, where this function is known as method signature
or prototype.
Prototype/Method Signature in a Class:
The prototype or method signature of a function within a class serves as a declaration of the function’s interface, specifying its name, parameters, and return type. In C++, method signatures are typically declared in the class definition, providing a blueprint for the function’s implementation.
class MyClass {
public:
void myFunction(); // Method signature
};
Interlinking Inline Functions, Method Signatures, and Scope Resolution Operator:
Inline functions, method signatures, and the scope resolution operator are interconnected elements in the design and implementation of C++ classes. Inline functions can be declared and defined within the class definition, serving as inline method implementations. Method signatures define the interface of class functions, specifying their names and parameters. The scope resolution operator facilitates the separation of interface and implementation by allowing class functions to be defined outside the class body, while still maintaining their association with the class
class MyClass {
public:
void myFunction(string name); // Method signature
};
inline void MyClass::myFunction(string name) {
std::cout << "Helloooo" << name << std::endl;
}
Here the code of myFunction() is very small and simple so for this we have used inline keyword, this enhances the readability and management of our code. But if the code is very complex and big in size then it can also be very simple to organize and manage with this approach. In this case, we just have to remove the inline keyword from here.
CONCLUSION
In conclusion, mastering OOP concepts in C++ unlocks the potential for creating modular, scalable, and maintainable software solutions. By leveraging classes, inheritance, constructors, and custom data structures, developers can craft elegant and efficient code that addresses diverse programming challenges.
This article serves as a springboard for deeper exploration into the fascinating realm of C++ OOP. Happy coding! 🚀🔍