When do need to include the explicit destructor in a class?Explain with example.
Write a program to define a class A with data member x and a parameterised constructor to initialize it. Define a class B with data member y and a parameterised constructor to initialize it. Write a function to calculate and print LCM of x & y.
We need to include the explicit destructor in a class only when the object is placed at particular location in memory by using placement new.
#include<iostream>
using namespace std;
class AB
{
private:
int a,b;
public:
// parametarized constructor
AB (int x,int y)
{
a = x;
b = y;
}
void calculate() {
int x=a, y=b;
while (y != 0) {
int t = y;
y = x % y;
x = t;
}
int hcf = x;
int lcm = (a * b) / hcf;
cout<<"LCM = " <<lcm;
}
};
int main()
{
int x,y;
cout<<"Enter first number: ";
cin>>x;
cout<<"Enter second number: ";
cin>>y;
AB obj(x,y);
obj.calculate();
}
Comments
Leave a comment