Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Keywords Conclusion Algorithm Start the program. Read the size of the array. Read the array elements from the user. Call the mer...

C++ Program - Default, Parameterized, and Copy Constructor

C++ Program - Default, Parameterized, and Copy Constructor

✅ C++ Program to Demonstrate Default, Parameterized, and Copy Constructor

#include <iostream>
using namespace std;

class sample {
  private:
    string name;
    int age;
  public:
    // Default constructor
    sample() {
      cout << "Enter name of the student:\n";
      cin.ignore();
      getline(cin, name);
      cout << "Enter age of the student:\n";
      cin >> age;
    }

    // Parameterized constructor
    sample(string n, int a) {  
      name = n;
      age = a;
    }

    // Copy constructor
    sample(sample &obj) {
      name = obj.name;
      age = obj.age;
    }

    void display() {
      cout << "Name: " << name << "\n";
      cout << "Age: " << age << "\n";
    }
};

int main() {
    sample s1;
    cout << "=== DEFAULT CONSTRUCTOR ===\n";
    s1.display();

    sample s2("Esther", 27);
    cout << "=== PARAMETERIZED CONSTRUCTOR ===\n";
    s2.display();

    cout << "=== COPY CONSTRUCTOR ===\n";
    sample s3(s2);
    s3.display();
}
  

๐Ÿ“˜ Explanation:

This C++ program demonstrates the use of three types of constructors in object-oriented programming:

  • Default Constructor → Initializes object when no arguments are passed. Here, it asks the user to input values.
  • Parameterized Constructor → Allows initialization with specific values while creating the object.
  • Copy Constructor → Creates a new object by copying values from an existing object.

๐Ÿงพ Sample Output:

Enter name of the student:
John
Enter age of the student:
22
=== DEFAULT CONSTRUCTOR ===
Name: John
Age: 22

=== PARAMETERIZED CONSTRUCTOR ===
Name: Esther
Age: 27

=== COPY CONSTRUCTOR ===
Name: Esther
Age: 27
  

๐Ÿ”‘ Keywords:

C++ constructor example, default constructor, parameterized constructor, copy constructor, OOPs in C++, class and object in C++, beginner C++ programs

๐Ÿ“Œ Hashtags:

#CPlusPlus #Constructor #OOP #CopyConstructor #ParameterizedConstructor #BeginnerCplusplus

๐Ÿ” Search Description:

This C++ program demonstrates how default, parameterized, and copy constructors work in object-oriented programming using class and objects with examples and output.

Comments

Popular Posts

๐ŸŒ™