16 Min Read • Study Chapter

Unit 1: Object-Oriented Paradigms, Classes & Memory Mechanics in C++

Procedural vs OOP paradigms, data abstraction, encapsulation boundaries, friend function mechanisms, static class lifetimes, and method chaining via the `this` pointer

C++ is the systems-level powerhouse of computer engineering—powering operating system kernels, game engines, high-frequency algorithmic trading platforms, and the underlying tensor execution engines of PyTorch and TensorFlow. In the Second Year Semester 3 curriculum, Unit 1 provides the transition from structured procedural C into object-oriented architectural design. This chapter covers the four pillars of OOP, class encapsulation, scope resolution mechanisms, friend functions, static class members, and university examination solutions.

1. Procedural Programming (POP) vs. Object-Oriented Programming (OOP)

In structured procedural programming (such as pure C), programs are organized around functions operating on external data. As system complexity scales, this creates severe architectural bottlenecks:

⚙️ Paradigm Comparison

  • Procedural Paradigm (POP): Focuses on procedural steps (algorithms). Data moves openly around the system from function to function. Employs top-down program decomposition. Global data is vulnerable to unauthorized alteration. Adding new data fields requires modifying multiple independent functions.
  • Object-Oriented Paradigm (OOP): Focuses on data security and encapsulation. Data is tied directly to the member functions authorized to operate on it. Employs bottom-up program decomposition. Supports data hiding via access specifiers. Enables code reuse via inheritance and polymorphism.

2. The Four Fundamental Pillars of OOP

  1. Encapsulation: Wrapping data members and member functions together inside a single structural unit (the class). Prevents external functions from directly mutating object state.
  2. Data Abstraction: Displaying only essential conceptual attributes while hiding internal low-level implementation details from client code.
  3. Inheritance: Mechanism by which a newly defined derived class inherits properties and behaviors from an existing base class, maximizing code reusability.
  4. Polymorphism: Ability of entities to take on multiple forms—compile-time (function overloading, operator overloading) and runtime (virtual functions, dynamic dispatch).

3. Classes, Objects & Access Specifiers in C++

A class in C++ is a user-defined composite data type serving as a blueprint. An object is a concrete instance of that class allocated on the stack or heap.

🔒 Access Specifiers Breakdown

  • private: Accessible strictly by member functions and friend functions of the same class. (Default specifier for C++ classes).
  • public: Accessible from any function anywhere in the program. Provides the public interface of the object.
  • protected: Inaccessible outside the class, but accessible by derived child classes in an inheritance tree.
C++ Class Definition, Scope Resolution & Inline Functions
#include <iostream>
#include <string>
using namespace std;

class BankAccount {
private:
    string accountNumber;
    double balance; // Hidden from direct external writes

public:
    // In-class member function (implicitly inline by compiler)
    void initialize(string acc, double initBal) {
        accountNumber = acc;
        balance = initBal;
    }

    // Member function prototypes declared inside class
    void deposit(double amount);
    bool withdraw(double amount);
    void display() const;
};

// Defining member functions outside class using Scope Resolution Operator (::)
void BankAccount::deposit(double amount) {
    if (amount > 0) balance += amount;
}

bool BankAccount::withdraw(double amount) {
    if (amount > 0 && balance >= amount) {
        balance -= amount;
        return true;
    }
    return false;
}

void BankAccount::display() const {
    cout << "Account: " << accountNumber << " | Balance: $" << balance << endl;
}

4. Friend Functions and Friend Classes

Occasionally, an external function or bridge class requires access to an object's private or protected attributes without becoming a formal member. Declaring a function with the friend keyword grants non-member functions complete access to the class's private members:

C++ Bridging Private Attributes via Friend Function
class Box {
private:
    double width;

public:
    Box(double w) : width(w) {}

    // Granting friendship to standalone function
    friend void printWidth(Box b);
};

// Normal standalone function (no scope resolution Box:: required)
void printWidth(Box b) {
    // Allowed direct read of private 'width'
    cout << "Box Width: " << b.width << endl;
}

5. Static Data Members & Static Member Functions

Normal member variables are instantiated per-object. A static data member is a single shared variable bound to the class itself—existing in global memory across all instances:

📌 Rules Governing Static Members in C++

  • Static data members must be declared inside the class, but defined and initialized outside the class at file scope.
  • Static member functions can be invoked directly on the class itself using ClassName::function() without instantiating an object.
  • Static member functions do not possess a this pointer and can only access static data members or other static member functions.
C++ Static Member Tracking Object Instantiation Count
class NodeTracker {
private:
    static int activeNodes; // Shared class-level variable
    int nodeId;

public:
    NodeTracker(int id) : nodeId(id) {
        activeNodes++;
    }
    ~NodeTracker() {
        activeNodes--;
    }

    // Static member function
    static int getActiveCount() {
        return activeNodes;
    }
};

// Mandatory external definition
int NodeTracker::activeNodes = 0;

6. The `this` Pointer and Method Chaining

In C++, every non-static member function receives a hidden implicit parameter: ClassName* const this. It stores the exact heap or stack memory address of the object currently executing the function:

  1. Resolving Identifier Collisions: Differentiates between local parameter names and class attributes: this->sgpa = sgpa;.
  2. Returning Reference for Method Chaining: By returning *this from member functions, operations can be concatenated: obj.setA(10).setB(20).execute();.

7. Typical Autonomous ETE Exam Questions & Full Marks Answers

Q1: What is a friend function? State 4 characteristics and explain when it should be used. (6 Marks)

Answer Strategy: Define a friend function as a non-member function granted explicit access to private and protected class members via the friend keyword in the class definition. List key characteristics: (1) Not in the scope of the class; (2) Cannot be called using the dot operator (invoked like a normal function); (3) Friendship is neither inherited nor transitive; (4) It can be declared in either private or public sections without affecting its behavior. Note use cases: bridging two disparate classes and overloading binary stream operators (<< and >>).

Q2: Why can a static member function access only static members? (4 Marks)

Answer Strategy: Emphasize that non-static members require a specific object instance to exist in memory, resolved via the implicit this pointer. Because static member functions are bound to the class itself and can be called before any object is created (Class::func()), they are never passed a this pointer, making it physically impossible to resolve which object's instance variable to access.

📥 Printable Chapter Reference (PDF Supplement)

Prefer an offline physical copy for revision or exam night preparation? You can download our verified, printer-friendly PDF synthesis for Unit 1 below:

C++ OOP: Unit 1 Classes, Friend Functions & Static Members Notes (PDF) Handwritten & typeset curriculum guide • Pillars of OOP, this pointer, scope resolution & Solved PYQs
Download Printable PDF ↗

🔗 Related Second Year Study Resources