Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

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

๐ŸŒ™