Explain briefly: (i) Destructor (ii) Exception (iii) Container (iv) Inheritance (v) Encapsulation


Q.) Explain briefly: (i) Destructor (ii) Exception (iii) Container (iv) Inheritance (v) Encapsulation

Subject: Object Oriented Programming Methodology

(i) Destructor

  • A destructor is a special member function of a class that is called automatically when an object of that class is destroyed.
  • It is used to release the resources that were allocated by the constructor.
  • Destructors are always declared with the same name as the class, but with a tilde (~) prefix.
  • For example:
class MyClass {
public:
  MyClass() {
    // Constructor code
  }
  ~MyClass() {
    // Destructor code
  }
};

When an object of the MyClass class is destroyed, the destructor will be called automatically to release any resources that were allocated by the constructor.

(ii) Exception

  • An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
  • Exceptions are typically caused by errors, such as division by zero, out-of-bounds array access, or invalid function arguments.
  • C++ provides a mechanism for handling exceptions using the try-catch statement.
  • For example:
try {
  // Code that may throw an exception
} catch (const std::exception& e) {
  // Code to handle the exception
}

If an exception is thrown inside the try block, the program will immediately jump to the catch block and execute the code inside it.

(iii) Container

  • A container is a data structure that stores a collection of elements.
  • C++ provides a number of built-in containers, such as vectors, arrays, and lists.
  • Containers can be used to store any type of data, including primitive types, objects, and pointers.
  • For example:
std::vector myVector;
myVector.push_back(1);
myVector.push_back(2);
myVector.push_back(3);

The above code creates a vector of integers and adds the values 1, 2, and 3 to it.

(iv) Inheritance

  • Inheritance is a mechanism that allows a new class to be created from an existing class.
  • The new class, called the derived class, inherits the properties and behavior of the existing class, called the base class.
  • This allows for code reuse and makes it easier to create new classes.
  • For example:
class Animal {
public:
  Animal() {
    // Constructor code
  }
  void eat() {
    // Eat code
  }
};

class Dog : public Animal {
public:
  Dog() {
    // Constructor code
  }
  void bark() {
    // Bark code
  }
};

The Dog class inherits from the Animal class and inherits all of its properties and behavior. This means that a Dog object can eat and bark.

(v) Encapsulation

  • Encapsulation is a mechanism that allows data and methods to be bundled together into a single unit.
  • This makes it easier to manage and protect data, and it also makes it easier to create reusable components.
  • In C++, encapsulation is achieved using classes.
  • For example:
class MyClass {
private:
  int data;

public:
  void setData(int data) {
    this->data = data;
  }

  int getData() {
    return this->data;
  }
};

The above code creates a class called MyClass that encapsulates an integer data member. The setData() and getData() methods can be used to access and modify the data member.