class Person {
public:
string name;
int age;
// Constructor — runs automatically when object is created
Person(string n, int a) : name(n), age(a) {}
string greet() {
return "Hi, I'm " + name + " (" + to_string(age) + ")";
}
// Destructor — runs when object is destroyed
~Person() {
// clean up resources here (files, memory, etc.)
}
};
Person p("Alice", 30); // constructor called
cout << p.greet(); // Hi, I'm Alice (30)
// p's destructor runs when p goes out of scope
Person(string n, int a) : name(n), age(a) {}
// More efficient than assigning inside the body
Create a BankAccount class with constructor (string owner, double balance), and methods:
void deposit(double amount)bool withdraw(double amount) — returns false if insufficient fundsdouble getBalance()Click "Run" to execute your code.