C++ constructor
#include <iostream>
class MyClass {
public:
// Constructor
MyClass(int x, double y) : myInt(x), myDouble(y) {
std::cout << "Constructor called with values: " << x << " and " << y << std::endl;
}
// Destructor
~MyClass() {
std::cout << "Destructor called" << std::endl;
}
private:
int myInt;
double myDouble;
};
int main() {
// Creating an object of MyClass with arguments for the constructor
MyClass obj(42, 3.14);
// The object goes out of scope, and the destructor is called
return 0;
}