Constructors and encapsulation
Constructors and encapsulation
Key Points:
1. When you first turn on the phone (creating an object), it goes through
some initial setups—choosing language, connecting to Wi-Fi, etc.
2. This setup is similar to a constructor. It gets the phone ready to use as
soon as you turn it on.
#include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
public:
// Constructor to initialize name and age
Student(string studentName, int studentAge) {
name = studentName;
age = studentAge;
cout << "Constructor called: " << name << " is
created." << endl;
}
int main() {
// Creating a Student object with name and age
Student student1("Alice", 16);
student1.displayInfo();
return 0;
}
In this example:
What is a Destructor?
A destructor is a special function that is automatically called when an
object goes out of scope or is deleted. It helps clean up resources, like
freeing memory if necessary.
Key Points:
● The destructor has the same name as the class but starts with a
tilde (~), like ~Student().
● It has no return type and takes no parameters.
● It is called automatically when an object is destroyed (e.g., when it
goes out of scope or the program ends).
Real-Life Example of a Destructor
1. Close the app, which saves data, releases memory, and stops using
resources.
2. Similarly, a destructor ensures an object’s resources are released
when it’s no longer needed.
Code Example: Destructor in C++
#include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
public:
// Constructor
Student(string studentName, int studentAge) {
name = studentName;
age = studentAge;
cout << "Constructor called: " << name << " is
created." << endl;
}
// Destructor
~Student() {
cout << "Destructor called: " << name << " is
destroyed." << endl;
}
int main() {
Student student1("Alice", 16);
student1.displayInfo();
In this example:
1. The constructor initializes each Student with name and age values
when an object is created.
2. The destructor is called automatically when the program ends (or
when objects go out of scope).
○ ~Student() prints a message, so we know when an object is
destroyed.
CONSTRUCTORS VS DECONSTRUCTORS
S.
Constructor Destructor
No.
While it can’t be
6. Constructor can be overloaded.
overloaded.
FFD
#include <iostream>
using namespace std;
class Z
{
public:
// constructor
Z()
{
cout<<"Constructor called"<<endl;
}
// destructor
~Z()
{
cout<<"Destructor called"<<endl;
}
};
int main()
{
Z z1; // Constructor Called
int a = 1;
if(a==1)
{
Z z2; // Constructor Called
} // Destructor Called for z2
} // Destructor called for z1
Output:
Constructor called
Constructor called
Destructor called
Destructor called
ENCAPSULATION
What is Encapsulation?
● Deposit money.
● Withdraw money.
● Check your balance.
But you don’t have direct access to the bank's computer system to
change your balance, as that would be unsafe. Instead:
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance; // Private: can only be accessed
within this class
public:
// Constructor to initialize balance
BankAccount(double initialBalance) {
balance = initialBalance;
}
In this example:
● Private: Only the class itself can access private members (like
balance in this example).
● Public: Anyone can access public members, such as
getBalance, deposit, and withdraw.
● Protected: Similar to private, but classes derived from this class
can access protected members.
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance;
public:
// Constructor to initialize balance
BankAccount(double initialBalance) {
balance = (initialBalance > 0) ? initialBalance
: 0;
}
int main() {
BankAccount myAccount(100); // Create an account
with $100
Explanation of Code
1. Private Variable: balance is private, meaning it’s hidden from
outside the class.
2. Getter: getBalance() allows us to read the balance without
modifying it.
3. Setter: setBalance(double newBalance) allows us to
change the balance but only if the new balance is positive. This
keeps our data safe and valid.
Summary