10 important concepts of Oops in C++
Object-oriented programming (OOP) in C++ is a programming paradigm that utilizes key concepts such as classes, objects, inheritance, polymorphism, encapsulation, and abstraction to structure and design software. C++ is a versatile programming language that fully supports the principles and features of OOP.
Classes and objects
The foundational principles of Object-Oriented Programming (OOP) revolve around classes and objects. They provide a structured way for data and behaviors in a software system to be defined and interacted with in C++. Classes and objects are central to creating well-organized, modular, and reusable code.
Class
A class is a blueprint or template for the creation of objects. It specifies the structure and behavior of the objects created from it. A class encapsulates data (attributes or member variables) and operations that operate on that data (methods or member functions). The class is a user-defined data type that allows you to create instances (objects) of that type. Here is a simplified C++ class.
Example:
#include <iostream>
#include <string>
class Car {
private:
//variables
std::string name;
int price;
public:
// Constructor
Car() {
std::cout << "Enter car name: ";
std::cin >> name;
std::cout << "Enter price: ";
std::cin >> price;
}
// Member function to display car's information
void displayDetails() const {
std::cout << "Name: " << name << std::endl;
std::cout << "price: " << price << std::endl;
}
};
int main() {
// Create a Car object
Car Model;
// Display car model's information
std::cout << "Model Information:" << std::endl;
Model.displayDetails();
return 0;
Output:
Explanation:
The Car class is defined with two private member variables: name (a string to store the car's name) and price (an integer to store the car's price).
Upon creating an object of the Car class using the constructor (Car()), the user will be prompted to input the car's name and price using std::cin. Additionally, the member function displayDetails() is available to display the car's information using std::cout.
This C++ program defines a class "Car" and demonstrates how to create objects of that class to store and display car information.
Object:
In programming, an object is a specific occurrence of a class. It represents the class blueprint in a tangible form with unique data and actions. Objects are constructed from classes and interact with one another to perform different tasks. They hold state, which is the values of member variables, and behavior, which is the execution of member functions. To create and utilize objects, you must first define a class. At this point, no memory is allocated. However, memory is allocated when you instantiate a class by creating an object.
Example
#include <iostream>
#include <string>
class Person {
private:
//variables
std::string name;
int age;
public:
// Constructor
Person() {
std::cout << "Enter name: ";
std::cin >> name;
std::cout << "Enter age: ";
std::cin >> age;
}
// Member function to display a person's information
void display() const {
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
}
};
int main() {
// Create an object person1
Person person1;
// Display person's information
std::cout << "Person 1 Information:" << std::endl;
person1.display();
return 0;
}
Output:
Explanation:
The above program demonstrated the creation of an object for the class. An object named person1 of the Person class is created. The program then displays the label "Person 1 Information:" followed by the person's information retrieved from the person1 object using the display() member function.
Inheritance
Object-oriented programming involves the concept of inheritance, which allows for creating a new class (known as a subclass or derived class) based on an existing class (known as a parent or base class). The derived class inherits the base class's functions and variables(properties and behaviors), and adding or modifying these properties and behaviors in the derived class is possible.
For instance, C++ provides a straightforward example of inheritance.
Example:
Program:
#include <iostream>
#include <string>
class Father{
public:
//Base class variables
std::string name;
int age;
public:
// Constructor
Father() {
std::cout << "Enter father name: ";
std::cin >> name;
std::cout << "Enter age: ";
std::cin >> age;
}
};
class child: public Father{
private:
// derived class variables
std::string name;
int age;
public:
//constructor
child(){
std::cout<<"Enter child name: ";
std::cin>>name;
std::cout<<"Enter age: ";
std:: cin>>age;
}
//member function to display information
void GetDetails(){
std::cout << "Father Name: " << Father::name << std::endl<<"Age:"<<Father::age<<std::endl;
std::cout << "Child Name: " << name << std::endl<<"Age: "<<age<<std::endl;;
}
};
int main() {
// Create a child object
child f1;
// Display father and child information
std::cout << "Father and Child information: " << std::endl;
f1.GetDetails();
return 0;
}
Output:
Explanation:
The above code demonstrates the concept of class inheritance in C++. It simulates a scenario involving a base class Father and a derived class child, both of which store information about names and ages.
The child class inherits from the Father class using the public access specifier. That means the name and age variables from the base class are accessible within the derived class. The “GetDetails” function displays the details of the Father and Child from the derived class.
Polymorphism
Polymorphism is a powerful feature in object-oriented programming that enables objects of different classes to be treated as objects of a shared base class. One way to achieve polymorphism is by using virtual functions and function overriding. By enabling dynamic method binding, polymorphism allows the appropriate method to be selected at runtime based on the type of object being processed.
C++ has two main types of polymorphism.
1. Static polymorphism
2. Dynamic polymorphism
Static polymorphism
Static polymorphism is a scenario where the method or function to be executed is determined during compilation. This determination is based on the number and types of arguments provided.
Function Overloading
Function Overloading is a type of static polymorphism where functions with the same name but different parameter lists are defined in the same scope. When the function is called, the appropriate function is determined by the number and types of arguments used in the function call. Function overloading allows different function implementations to handle data types or behavior variations.
Here is an example of function overloading:
#include <iostream>
#include <string>
class Subtraction {
public:
int sub(int a, int b) {
return a - b;
}
double sub(double a, double b) {
return a - b;
}
};
int main() {
// Create an object for the subtraction class
Subtraction s1;
std::cout << "Performing function overloading" << std::endl;
std::cout<<"Passing integer variables as input parameters: "<<s1.sub(9,4)<<std::endl;
std::cout<<"Passing double variables as input parameters: "<<s1.sub(8.47,4.52)<<std::endl;
return 0;
}
Output:
Explanation:
This C++ program demonstrates the concept of function overloading using a class called Subtraction.
It provides two overloaded member functions named sub. The first version takes two integer parameters and returns the result of subtracting the second integer from the first. The second version takes two double parameters and returns the result of subtracting the second double from the first. The function chosen for execution depends on the types of arguments passed during the call.
This C++ program demonstrates the concept of function overloading using a class called Subtraction.
Operator Overloading
Operator overloading is another type of static polymorphism, which allows you to define specific operator behavior when applied to user-defined objects. By overloading operators, you can enable natural and meaningful interactions with your class objects. Some operators, such as the scope resolution operator '::', member selection operator '.', and ternary conditional operator '?:', cannot be overloaded.
Example
#include <iostream>
class Complex {
private:
double real;
double imag;
public:
Complex(double r, double i) : real(r), imag(i) {}
// Operator overloading for the + operator
Complex operator+(const Complex& other) {
double newReal = real + other.real;
double newImag = imag + other.imag;
return Complex(newReal, newImag);
}
void display() {
std::cout << real;
if (imag >= 0) {
std::cout << " + " << imag << "i";
} else {
std::cout << " - " << -imag << "i";
}
std::cout << std::endl;
}
};
int main() {
Complex num1(2.0, 3.5);
Complex num2(1.5, -2.0);
Complex result = num1 + num2;
std::cout << "Number 1: ";
num1.display();
std::cout << "Number 2: ";
num2.display();
std::cout << "Result: ";
result.display();
return 0;
}
Output:
Explanation:
In the Complex class above, the + operator is overloaded to perform addition on two Complex objects.
The program demonstrates how operator overloading can make complex number arithmetic more intuitive and readable in C++.
Dynamic polymorphism
Late binding and runtime polymorphism are the other terms for dynamic polymorphism. In this type of polymorphism, the function call is resolved during runtime.
Virtual Functions:
A virtual function is a member function declared with the virtual keyword in the base class. To provide their implementations, derived classes can override (redefine) the virtual function. Late binding is possible with virtual functions because the right function to call is decided at runtime based on the actual object type.
Example:
A virtual function and dynamic polymorphism example:
#include <iostream>
#include <string>
class Employee {
public:
Employee(const std::string& name) : name(name) {}
virtual std::string getRole() {
return "Employee";
}
virtual ~Employee() {
std::cout << "Destructor for Employee: " << name << std::endl;
}
private:
std::string name;
};
class Manager : public Employee {
public:
Manager(const std::string& name) : Employee(name) {}
std::string getRole() override {
return "Manager";
}
~Manager() override {
std::cout << "Destructor for Manager" << std::endl;
}
};
class Developer : public Employee {
public:
Developer(const std::string& name) : Employee(name) {}
std::string getRole() override {
return "Developer";
}
~Developer() override {
std::cout << "Destructor for Developer" << std::endl;
}
};
int main() {
Employee* emp1 = new Manager("Alice");
Employee* emp2 = new Developer("Bob");
std::cout << emp1->getRole() << " " << emp1->getRole() << std::endl;
std::cout << emp2->getRole() << " " << emp2->getRole() << std::endl;
delete emp1;
delete emp2;
return 0;
}
Output:
Explanation:
This example demonstrates how virtual functions enable runtime polymorphism by allowing you to treat objects from various derived classes via a shared base class interface, replicating real-world scenarios more flexibly and dynamically.
Function overriding
In C++, function overriding allows a subclass or derived class to offer a specific implementation of a method already defined in the parent or base class. The subclass function will have the same name, return type, and parameters as the parent class method but will be implemented differently. Function overriding is useful when we tailor a method's behavior to a specific subclass. In C++, function overriding is accomplished by including the virtual keyword in the function declaration in the base class and then redefining the function in the derived class with the keyword override.
Let us understand function overriding by considering an example.
Example
#include <iostream>
// Base class: Animal
class Animal {
public:
// Virtual function to make a sound
virtual void makeSound() {
std::cout << "Animal makes a sound." << std::endl;
}
};
// Derived class: Dog (inherits from Animal)
class Dog : public Animal {
public:
// Override the makeSound function for dogs
void makeSound() override {
std::cout << "Dog barks." << std::endl;
}
};
// Derived class: Cat (inherits from Animal)
class Cat : public Animal {
public:
// Override the makeSound function for cats
void makeSound() override {
std::cout << "Cat meows." << std::endl;
}
};
// Derived class: Lion (inherits from Animal)
class Lion : public Animal {
public:
// Override the makeSound function for lions
void makeSound() override {
std::cout << "Lion roars." << std::endl;
}
};
int main() {
// Create an array of Animal pointers
Animal* animals[] = { new Animal(), new Dog(), new Cat(), new Lion() };
// Loop through the array and call makeSound for each animal
for (Animal* animal : animals) {
animal->makeSound(); // Calls the appropriate overridden function
}
// Clean up memory by deleting the dynamically allocated objects
for (Animal* animal : animals) {
delete animal;
}
return 0;
}
Output:
Explanation
- The base class is Animal, with a virtual function makeSound() representing animal's sounds.
- The Dog, Cat, and Lion classes are all inherited from Animal, and they each override the makeSound() function to give unique noises for each animal type.
- The main function creates an array of pointers to Animal, which contains instances of Animal, Dog, Cat, and Lion.
- The Loop iterates across each pointer, using the makeSound() function each time. Because of dynamic binding, each object's appropriate override function is run based on its actual type.
This example shows how function overriding and dynamic polymorphism work together to produce distinct behaviors for objects of different derived classes accessed via a shared base class interface.
Abstraction
In object-oriented programming, abstraction is a crucial principle in C++. It involves creating models of classes and objects representing real-life entities while concealing unimportant implementation details. The aim is to emphasize necessary features and disregard non-essential aspects to produce a clear and understandable representation.
Abstraction is a fundamental principle in C++ object-oriented programming. This tool assists in generating code that is user-friendly and simple to understand, manage and comprehend by concentrating on essential features and hiding implementation details. C++ has two types of abstractions: data abstraction and procedural abstraction.
Using classes
One way to achieve abstraction is through the use of classes. Abstraction allows for the hiding of data variables and functions, as well as concealing the internal workings of the class from external sources.
Example:
#include <iostream>
// The 'addition' class represents a basic abstraction for performing addition.
class Addition {
private:
int a, b; // Private attributes to hold the operands
public:
// Public method to perform addition
int add(int x, int y) {
a = x; // Store the first operand
b = y; // Store the second operand
return a + b; // Return the sum
}
};
int main() {
Addition a; // Create an instance of the 'Addition' class
// We call the function 'add' by passing 4 and 5 as parameters.
std::cout << "Addition of 4 and 5 is: " << a.add(4, 5);
return 0;
}
Output:
Explanation:
The outside world does not know the implementation details of class 'Addition' and method 'add'. The above program demonstrates abstraction, as users of the class only need to know the interface, not the internals.
In header files
The other way to achieve abstraction in C++ is through header files. In C++, built-in header files provide a type of abstraction called procedural abstraction. The goal of procedural abstraction is to hide the implementation details of a process (function) while exposing its functionality via a well-defined interface. This abstraction type makes complex functionality easier to use by offering high-level functions and classes that do not require the user to understand the underlying implementation.
You use the C++ Standard Library's procedural abstraction when you include an inbuilt header file in your program. By considering an example, let us understand how inherent header files achieve procedural abstraction.
Example
#include <iostream>
#include <algorithm> // Include the algorithm header
int main() {
int numbers[50];
int n;
std::cout << "Enter the number of elements to sort: " <<std::endl;
std::cin>>n;
std::cout << "Enter " << n << " digits" << std::endl;
// Store input from the user to the array
for (int i = 0; i < n; ++i) {
std::cin >> numbers[i];
}
int size = sizeof(numbers) / sizeof(numbers[0]);
//using the sort function with the help of the algorithm header file.
std::sort(numbers, numbers + n);
std::cout << "Sorted array in ascending order: ";
for (int i = 0; i < n; ++i) {
std::cout << numbers[i] << " ";
}
std::cout << std::endl;
return 0;
}
Output:
Explanation:
This program showcases the use of functions, which allows for easier completion of tasks without needing to understand the inner workings of the functions. The header files hide all the complex operations and give us the functions to use easily.
These built-in header files encapsulate complex operations and offer a simple interface for typical programming tasks. You don't need to understand the complex aspects of these operations; use the functions and classes provided to obtain the necessary outcomes. This abstraction simplifies programming processes and improves code reusability using standardized functionality.
Encapsulation
Encapsulation is used in object-oriented programming (OOP), which includes integrating data (attributes) and methods (functions) into a single entity known as a class. Data hiding, abstraction, and modular design are all advantages of this technique. Encapsulation allows you to control who has access to a class's internal workings, preventing unauthorized changes and preserving a clear interface for object interaction.
Example:
#include <iostream>
class ArithmeticOperations {
private:
double operand1;
double operand2;
public:
// Constructor to initialize operands
ArithmeticOperations(double op1, double op2) : operand1(op1), operand2(op2) {}
// Public methods to perform arithmetic operations
double add() const {
return operand1 + operand2;
}
double subtract() const {
return operand1 - operand2;
}
double multiply() const {
return operand1 * operand2;
}
double divide() const {
if (operand2 != 0) {
return operand1 / operand2;
} else {
std::cout << "Division by zero is not allowed." << std::endl;
return 0.0;
}
}
};
int main() {
double num1, num2;
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
ArithmeticOperations operations(num1, num2);
// Perform arithmetic operations using the encapsulated methods
std::cout << "Sum: " << operations.add() << std::endl;
std::cout << "Difference: " << operations.subtract() << std::endl;
std::cout << "Product: " << operations.multiply() << std::endl;
std::cout << "Quotient: " << operations.divide() << std::endl;
return 0;
}
Output:
Explanation:
This program shows encapsulation by utilizing the ArithmeticOperations class. The concept of executing arithmetic operations (addition, subtraction, multiplication, and division) on two operands is encapsulated in the class. The class's public methods offer a controlled way to carry out specific mathematical operations.
Conclusion:
Ultimately, Object-Oriented Programming (OOP) is a core concept in C++ that provides a systematic and efficient software design and development method. OOP encourages data and behavior encapsulation within objects, supporting modularity, reusability, and maintainability. C++ allows building complex structures with organized and interconnected components using the principles of classes and objects, inheritance, polymorphism, and encapsulation. This modular design method improves code organization, reduces code duplication, and makes debugging and maintenance easier. OOP in C++ enables developers to construct robust, scalable, and flexible programs that stick to the principles of software engineering excellence by modeling real-world items and their connections.