Classes_objects
Classes and Objects
Section titled “Classes and Objects”Object-Oriented Programming (OOP) is a programming paradigm that organizes code around “objects” rather than functions. In this chapter, you’ll learn about classes, the blueprints for creating objects, and how to use them effectively in C++.
What is Object-Oriented Programming?
Section titled “What is Object-Oriented Programming?”OOP is based on several key concepts:
┌─────────────────────────────────────────────────────────────┐│ Object-Oriented Programming Concepts │├─────────────────────────────────────────────────────────────┤│ ││ ┌─────────┐ ┌─────────┐ ┌─────────────┐ ││ │Classes │───▶│ Objects │───▶│ Encapsulation│ ││ │(Blueprints) │(Instances) │ (Data hiding) │ ││ └─────────┘ └─────────┘ └─────────────┘ ││ │ │ │ ││ ▼ ▼ ▼ ││ ┌─────────┐ ┌─────────┐ ┌─────────────┐ ││ │Inheritance│ │ Polymorphism│ │ Abstraction │ ││ │(Reuse) │ │(Many forms) │ (Simplify) │ ││ └─────────┘ └─────────┘ └─────────────┘ ││ │└─────────────────────────────────────────────────────────────┘Classes vs Structs
Section titled “Classes vs Structs”Both classes and structs can contain data and functions. The main difference is default access specifier:
| Feature | Class | Struct |
|---|---|---|
| Default access | private | public |
| Use for | Data hiding, OOP | Simple data containers |
// Class - members are private by defaultclass Circle { double radius; // private by defaultpublic: double getRadius() { return radius; } void setRadius(double r) { radius = r; }};
// Struct - members are public by defaultstruct Point { double x; // public by default double y; // public by default};Defining a Class
Section titled “Defining a Class”class Rectangle {private: double width; double height;
public: // Constructor Rectangle(double w, double h);
// Member functions double area() const; double perimeter() const; void resize(double newWidth, double newHeight);
// Getters double getWidth() const { return width; } double getHeight() const { return height; }};
// Rectangle.cpp#include "Rectangle.h"
Rectangle::Rectangle(double w, double h) : width(w), height(h) {}
double Rectangle::area() const { return width * height;}
double Rectangle::perimeter() const { return 2 * (width + height);}
void Rectangle::resize(double newWidth, double newHeight) { width = newWidth; height = newHeight;}Creating Objects
Section titled “Creating Objects”#include <iostream>
int main() { // Create objects Rectangle rect1(5, 3); Rectangle rect2(10, 7);
// Use member functions std::cout << "Area: " << rect1.area() << std::endl; std::cout << "Perimeter: " << rect2.perimeter() << std::endl;
// Modify object rect1.resize(8, 4); std::cout << "New area: " << rect1.area() << std::endl;
return 0;}Class Members
Section titled “Class Members”Data Members (Properties)
Section titled “Data Members (Properties)”class BankAccount {private: std::string accountNumber; std::string ownerName; double balance; bool isActive;};Member Functions (Methods)
Section titled “Member Functions (Methods)”class BankAccount {public: // Getter double getBalance() const { return balance; }
// Setter void deposit(double amount) { if (amount > 0) { balance += amount; } }
// Withdraw returns success/failure bool withdraw(double amount) { if (amount > 0 && amount <= balance) { balance -= amount; return true; } return false; }};The this Pointer
Section titled “The this Pointer”this is a pointer to the current object:
class Person {private: std::string name; int age;
public: void setName(const std::string& name) { this->name = name; // Distinguish parameter from member }
// Returning reference to object (for chaining) Person& setAge(int age) { this->age = age; return *this; }};Access Specifiers
Section titled “Access Specifiers”C++ provides three levels of access control:
| Specifier | Same Class | Derived Class | Outside |
|---|---|---|---|
private | ✓ | ✗ | ✗ |
protected | ✓ | ✓ | ✗ |
public | ✓ | ✓ | ✓ |
class Base {private: int privateVar; // Only Base can accessprotected: int protectedVar; // Base and derived classespublic: int publicVar; // Everyone can access};Complete Example: Bank Account
Section titled “Complete Example: Bank Account”#include <iostream>#include <string>#include <vector>
class BankAccount {private: std::string accountNumber; std::string ownerName; double balance; bool isActive;
public: // Constructor BankAccount(const std::string& number, const std::string& owner, double initialBalance = 0) : accountNumber(number), ownerName(owner), balance(initialBalance), isActive(true) {}
// Getter methods std::string getAccountNumber() const { return accountNumber; } std::string getOwnerName() const { return ownerName; } double getBalance() const { return balance; } bool getIsActive() const { return isActive; }
// Transaction methods void deposit(double amount) { if (amount > 0 && isActive) { balance += amount; std::cout << "Deposited: $" << amount << std::endl; } }
bool withdraw(double amount) { if (amount > 0 && amount <= balance && isActive) { balance -= amount; std::cout << "Withdrawn: $" << amount << std::endl; return true; } std::cout << "Withdrawal failed!" << std::endl; return false; }
// Account management void close() { isActive = false; std::cout << "Account closed." << std::endl; }
void display() const { std::cout << "Account: " << accountNumber << std::endl; std::cout << "Owner: " << ownerName << std::endl; std::cout << "Balance: $" << balance << std::endl; std::cout << "Status: " << (isActive ? "Active" : "Closed") << std::endl; }};
int main() { // Create account BankAccount account("123456789", "John Doe", 1000);
// Display account info account.display(); std::cout << std::endl;
// Make transactions account.deposit(500); account.withdraw(200); account.withdraw(2000); // Should fail
std::cout << std::endl; account.display();
return 0;}Output:
Account: 123456789Owner: John DoeBalance: $1000Status: Active
Deposited: $500Withdrawn: $200Withdrawal failed!
Account: 123456789Owner: John DoeBalance: $1300Status: ActiveClass vs Object
Section titled “Class vs Object”┌─────────────────────────────────────────────┐│ Class (Blueprint) ││ ┌─────────────────────────────────────┐ ││ │ class BankAccount │ ││ │ { │ ││ │ - accountNumber │ ││ │ - ownerName │ ││ │ - balance │ ││ │ + deposit() │ ││ │ + withdraw() │ ││ │ + getBalance() │ ││ │ } │ ││ └─────────────────────────────────────┘ ││ │ │ ││ │ creates │ uses ││ ▼ ▼ ││ ┌─────────────────────────────────────┐ ││ │ Objects (Instances) │ ││ │ │ ││ │ account1 account2 │ ││ │ ───────── ───────── │ ││ │ "12345" "67890" │ ││ │ "John" "Jane" │ ││ │ $1000 $5000 │ ││ └─────────────────────────────────────┘ │└─────────────────────────────────────────────┘Best Practices
Section titled “Best Practices”1. Use Classes for Data + Behavior
Section titled “1. Use Classes for Data + Behavior”// Bad - just a data containerstruct Person { std::string name; int age;};
// Good - data + behaviorclass Person {private: std::string name; int age;
public: void setName(const std::string& n) { name = n; } void setAge(int a) { if (a >= 0) age = a; } std::string getName() const { return name; } int getAge() const { return age; }};2. Make Getters const
Section titled “2. Make Getters const”class MyClass {public: int getValue() const { // const means doesn't modify object return value; }};3. Initialize Members in Constructor
Section titled “3. Initialize Members in Constructor”class MyClass {private: int value; std::string name;
public: MyClass() : value(0), name("default") {} MyClass(int v, const std::string& n) : value(v), name(n) {}};4. One Class per File
Section titled “4. One Class per File”- Header:
MyClass.h - Implementation:
MyClass.cpp
Key Takeaways
Section titled “Key Takeaways”- A class is a blueprint for creating objects
- Objects are instances of classes
- Classes combine data (attributes) and functions (methods)
- Access specifiers (
public,private,protected) control visibility privateis default for classes,publicis default for structs- The
thispointer refers to the current object - Use meaningful names and keep classes focused on one responsibility
Next Steps
Section titled “Next Steps”Now let’s learn about constructors and destructors, which manage object lifetime.
Next Chapter: 09_constructors_destructors.md - Constructors and Destructors