Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

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

๐ŸŒ™