Skip to main content

What is constructor? List and explain the different types of constructors supported by C++ with examples. Explain destructor with an example.

 In C++, a constructor is a special member function of a class that gets called automatically when an object of the class is created. Its primary purpose is to initialize the data members of the class.

C++ supports different types of constructors based on the number of arguments and the purpose of initialization. These are:

1. Default Constructor: A default constructor is a constructor that takes no arguments. If a class does not have any user-defined constructors, C++ compiler automatically generates a default constructor. This constructor initializes the data members of the class with default values.

Example:

class MyClass {
public:
    int x;
    MyClass() {
        x = 0;  // default initialization
    }
};

2. Parameterized Constructor: A parameterized constructor is a constructor that takes one or more arguments. It is used to initialize the data members of the class with specific values passed as arguments.

Example:

class MyClass {
public:
    int x;
    MyClass(int n) {
        x = n;  // initialization with argument value
    }
};

3. Copy Constructor: A copy constructor is a constructor that takes an object of the same class as an argument. It is used to create a new object that is a copy of the existing object.

Example:

class MyClass {
public:
    int x;
    MyClass(const MyClass &obj) {
        x = obj.x;  // copy data member value from obj
    }
};

4. Constructor Overloading: Constructor overloading is a technique that allows a class to have multiple constructors with different parameters. It is used to provide flexibility in object initialization.

Example:

class MyClass {
public:
    int x;
    MyClass() {
        x = 0;  // default initialization
    }
    MyClass(int n) {
        x = n;  // initialization with argument value
    }
};

A destructor is a special member function of a class that gets called automatically when an object is destroyed or goes out of scope. Its primary purpose is to release the resources that were allocated by the object during its lifetime.

Example:

class MyClass {
public:
    int *ptr;
    MyClass() {
        ptr = new int;
    }
    ~MyClass() {
        delete ptr;  // release memory allocated by object
    }
};

Comments

Popular posts from this blog

Explain the lifecycle of Servlet with block diagram.

 The lifecycle of a Servlet can be divided into several stages. Here's a block diagram that illustrates the different stages: Servlet API: The Servlet API provides a standard set of interfaces and classes for creating and interacting with Servlets. It is typically included in the web application's classpath as a JAR file. Servlet Container: The Servlet Container is a web server or application server that implements the Servlet API. It provides a runtime environment for executing Servlets and manages their lifecycle. Servlet Class: The Servlet Class is the Java class that implements the javax.servlet.Servlet interface. It contains the logic for processing HTTP requests and generating HTTP responses. init(): The init() method is called once when the Servlet is first loaded by the Servlet Container. It is used for initialization tasks, such as setting up database connections, loading configuration settings, or initializing other resources that will be used by the Servlet. service(...

What are Stub and Skeleton in Distributed Application? Explain its function with block diagram.

 Stub and Skeleton are two important components of distributed applications. A distributed application is a software system that runs on multiple computers connected through a network. It allows users to access resources and services on different computers as if they were on a local computer. In a distributed application, a client program on one computer sends a request to a server program on another computer. The server program processes the request and sends a response back to the client program. Stub and Skeleton help to facilitate this communication between the client and server programs. A Stub is a client-side proxy that represents the remote object on the client machine. It acts as a gateway for the client to communicate with the server. When a client invokes a method on the Stub, it marshals the arguments and sends them to the server over the network. The Stub then waits for the server to send a response. When the response is received, the Stub unmarshals the data and retur...

Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space.

 To solve this problem, we can follow these steps: Open the input file for reading and the output file for writing. Read the input file line by line. For each line, replace every sequence of consecutive blank spaces with a single space using the std::regex_replace function from the regex library. Write the modified line to the output file. Here is the code: #include <iostream> #include <fstream> #include <regex> using namespace std; int main() {     // Open the input file for reading and the output file for writing     ifstream infile("input.txt");     ofstream outfile("output.txt");     if (!infile) {         cerr << "Error: Could not open input file.\n";         return 1;     }     if (!outfile) {         cerr << "Error: Could not open output file.\n";         return 1;     }     string lin...