Answer to Question #236104 in C++ for SOM

Question #236104

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.



1
Expert's answer
2021-09-14T00:43:10-0400

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.

  1. Example: Fee* p = new Fee();
#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();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS