Simple Parameterized Constructor For Find Prime Number Example Program In C++
Definition
In C++, Constructor is automatically called when an object( the instance of the class) create.It is a special member function of the class.
- It has the same name of the class.
- It must be a public member.
- No Return Values.
- Default constructors are called when constructors are not defined for the classes.
Prime Number Calculation Algorithm/Steps:
- STEP 1: Start the program.
- STEP 2: Declare the class as Prime with data members and Member functions.
- STEP 3: Consider the argument constructor Prime() with integer Argument.
- STEP 4: To call the function calculate() and do the following steps.
- STEP 5: For i=2 to a/2 do
- STEP 6: Check if a%i==0 then set k=0 and break.
- STEP 7: Else set k value as 1.
- STEP 8: Increment the value i as 1.
- STEP 9: Check whether the k value is 1 or 0.
- STEP 10: If it is 1 then display the value is a prime number.
- STEP 11: Else display the value is not prime.
- STEP 12: Stop the program.
Syntax
class class_name {
Access Specifier :
Member_Variables
Member_Functions
public:
class_name(variables) {
// Constructor code
}
//... other Variables & Functions
}
Example Program
/* Simple Parameterized Constructor For Prime Number Example Program In C++
Understanding Class and Constructors */
#include<iostream>
#include<conio.h>
using namespace std;
// Prime Class Declaration
class Prime {
//Member Variable Declaration
int a, k, i;
public:
// Parameterized Constructor definition
Prime(int x) {
a = x;
k = 1;
{
for (i = 2; i <= a / 2; i++)
if (a % i == 0) {
k = 0;
break;
} else {
k = 1;
}
}
}
//Member Function show() for display result.
void show() {
if (k == 1)
cout << a << " is Prime Number.";
else
cout << a << " is Not Prime Numbers.";
}
};
//Main Function
int main() {
int a;
cout << "Simple Parameterized Constructor For Prime Number Example Program In C++\n";
cout << "\nEnter the Number:";
cin>>a;
// Object Creation For Class and Initiate Value from the user input
Prime obj(a);
// Call Member Function show()
obj.show();
// Wait For Output Screen
getch();
return 0;
}
Sample Output
Simple Parameterized Constructor For Prime Number Example Program In C++
Enter the Number:7
7 is Prime Number.
--
Simple Parameterized Constructor For Prime Number Example Program In C++
Enter the Number:10
10 is Not Prime Numbers.
C++ Constructor Example Programs
- Simple Example Program For Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Parameterized Constructor In C++
- Simple Parameterized Constructor For Find Prime Number Example Program In C++
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program For Copy Constructor In C++
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Example Program For Destructor In C++
- Simple Destructor Scope Measurement Example Program In C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Copy Constructor In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Simple Example Program For Constructor In C++
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program for Inline Function Using C++ Programming
- Simple Program for Read user Input Using cin
- Simple Example Program For Constructor Overloading In C++
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Binary Operator Overloading Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++