Classes and Objects in C++
Classes and objects are the foundation of object-oriented programming (OOP) in C++. A class is a template or blueprint that defines the structure and behavior of an entity. An object is an instance of that class — a concrete entity created based on the class blueprint.
With classes and objects, we can group data (attributes) and functions (methods) into a single organized unit. This makes code more modular, easier to maintain, and reusable.
Code Example
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
private:
string ownerName;
double balance;
public:
BankAccount(string name, double initialBalance) {
ownerName = name;
balance = initialBalance;
}
void deposit(double amount) {
if (amount > 0) {
balance += amount;
cout << "Deposit successful. Balance: " << balance << endl;
}
}
void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
cout << "Withdrawal successful. Balance: " << balance << endl;
} else {
cout << "Insufficient balance!" << endl;
}
}
void displayInfo() {
cout << "Owner: " << ownerName << ", Balance: " << balance << endl;
}
double getBalance() {
return balance;
}
};
int main() {
BankAccount account1("John Smith", 500000);
account1.displayInfo();
account1.deposit(200000);
account1.withdraw(100000);
account1.displayInfo();
BankAccount* account2 = new BankAccount("Sarah Johnson", 1000000);
account2->deposit(500000);
account2->displayInfo();
delete account2;
return 0;
}
Code Explanation
-
class BankAccount- Defines a class named BankAccount. Inside the class there are data members (member variables) and member functions. -
private:- An access specifier that makesownerNameandbalanceaccessible only from inside the class. They cannot be accessed directly from outside. -
public:- An access specifier that makes the functionsdeposit(),withdraw(),displayInfo(), andgetBalance()accessible from outside the class. -
BankAccount(string name, double initialBalance)- The class constructor. Called automatically when an object is created. Initializes the data members. -
BankAccount account1(...)- Creates the objectaccount1on the stack memory using theBankAccountdata type. -
account1.deposit(200000)- Uses the dot operator (.) to call a member function on an object declared normally. -
BankAccount* account2 = new BankAccount(...)- Creates the objectaccount2on the heap memory using a pointer. -
account2->deposit(500000)- Uses the arrow operator (->) to call a member function through a pointer to the object. -
delete account2- Frees the memory allocated for the object on the heap.
Everyday Use Cases
Imagine you have a bank account. The bank is a class that defines the account's rules (owner name, balance, how to deposit/withdraw). Each customer who opens an account is an object of the BankAccount class. Customer A has their own name and balance, and customer B has their own name and balance. The way they save (deposit) and withdraw money is the same, but each one's balance data is different.
Conclusion
A class is a blueprint that contains the definitions of attributes and methods. An object is a real instance of the class. Use the dot operator (.) for regular objects and the arrow operator (->) for pointer objects. Access specifiers (private, public) control who is allowed to access a class's data and member functions.