Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

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

๐ŸŒ™