Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

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

πŸŒ™