4.6 Features of Object-Oriented Programming

4.6 Features of Object-Oriented Programming

Introduction to Object-Oriented Paradigm

Object-Oriented Programming (OOP) is a programming paradigm centered around objects rather than functions and logic. It models software as a collection of objects that encapsulate data (attributes) and behavior (methods), closely mirroring real-world entities. OOP addresses the limitations of procedural programming by promoting code reusability, modularity, and easier maintenance through its core principles: Encapsulation, Abstraction, Inheritance, and Polymorphism. This unit introduces key OOP features as implemented in C++, building upon the foundational C concepts.


1. Inline Functions

1.1 Concept and Purpose

  1. Definition: An inline function is a function for which the compiler copies the code directly into the place where the function is called, instead of generating a standard function call (which involves stack operations like pushing parameters and jumping to the function code).

  2. Purpose: To eliminate the overhead of function calls for small, frequently called functions, thereby improving execution speed. It's a request to the compiler, not a command.

  3. Trade-off: Increases the size of the compiled binary (code bloat) because the function body is duplicated at each call site. Used optimally for small functions (1-3 lines).

1.2 Syntax and Usage

Use the inline keyword before the function definition.

#include <iostream>
using namespace std;

// Inline function definition
inline int square(int x) {
    return x * x;
}

int main() {
    int num = 5;
    // The compiler *may* replace this call with the code `5 * 5`
    int result = square(num);
    cout << "Square of " << num << " is " << result << endl;
    return 0;
}

Note: The inline keyword is a request. The compiler may ignore it for complex functions (e.g., those containing loops, recursion, or static variables).

1.3 Comparison: Inline Function vs Macro (#define)

  • Inline Function: Type-safe, evaluated by the compiler, respects scope, and can be debugged.

  • Macro: Simple text substitution by the preprocessor, not type-safe, can cause unexpected side-effects. Prefer inline functions over macros for performance-critical, small operations.


2. Function Overloading

2.1 Concept

  1. Definition: The ability to define multiple functions with the same name within the same scope, as long as they have different parameter lists (different number, type, or order of parameters).

  2. Also Known As: Compile-time polymorphism or static polymorphism.

  3. Purpose: To provide a consistent interface for performing similar operations on different types of data. Improves code readability and usability.

2.2 Rules and Syntax

  • Overloaded functions must differ in their signature (function name + parameter list). The return type alone is not sufficient to distinguish overloaded functions.

How it Works: The compiler determines which function to call based on the number and types of arguments provided at the call site.


3. Classes and Objects: The Foundation

3.1 Class: The Blueprint

  1. Definition: A class is a user-defined data type that serves as a blueprint for creating objects. It bundles data (member variables) and functions (member functions) that operate on that data into a single unit.

  2. Syntax:

3.2 Object: An Instance of a Class

  1. Definition: An object is a concrete instance of a class. Memory is allocated when an object is created. You can create multiple objects from one class.

  2. Syntax for Object Creation:

3.3 Basic Example: Class and Object


4. Member Functions and Data Members

4.1 Data Members (Attributes/Properties)

  • Variables declared inside a class.

  • Represent the state or characteristics of an object.

  • Can be of any data type (primitive, array, other class objects, pointers, etc.).

4.2 Member Functions (Methods/Behavior)

  • Functions declared inside a class.

  • Define the operations or behaviors that can be performed on the object's data.

  • Can be defined inside the class (inline by default) or outside the class using the scope resolution operator ::.

4.3 Defining Member Functions Outside the Class


5. Access Specifiers and Constructors

5.1 Access Specifiers

Control the accessibility of class members from outside the class.

  1. private (Default):

    • Members are accessible only from within the class itself (or by friend functions/classes).

    • Implements data hiding (a key aspect of encapsulation).

  2. public:

    • Members are accessible from anywhere the object is visible.

    • Typically, member functions are public, and data members are private.

  3. protected:

    • Similar to private, but also accessible by derived classes (used in inheritance).

5.2 Constructors: Object Initialization

  1. Definition: A special member function that is automatically called when an object is created. It has the same name as the class and no return type (not even void).

  2. Purpose: To initialize the object's data members to valid initial states.

  3. Types:

    • Default Constructor: Takes no parameters. Provided by the compiler if no constructor is defined.

    • Parameterized Constructor: Takes parameters to initialize objects with specific values.

    • Copy Constructor: Initializes an object using another object of the same class.

5.3 Example with Access Specifiers and Constructors


6. Static Data Members and Static Member Functions

6.1 Static Data Member

  1. Concept: A single copy of the data member is shared by all objects of the class. It is not tied to any individual object but to the class itself.

  2. Declaration: Inside the class with static keyword.

  3. Definition and Initialization: Must be defined outside the class (typically in a source file) and can be initialized there.

  4. Use Case: To maintain data common to all objects (e.g., a counter for the number of objects created).

6.2 Static Member Function

  1. Concept: A function that can be called without creating an object of the class. It can only access static data members and other static member functions (cannot access non-static members directly because they belong to objects).

  2. Syntax: Use static keyword in declaration.

6.3 Example: Static Counter


7. Operator Overloading

7.1 Concept

  1. Definition: Giving special meaning to an existing C++ operator (like +, -, ==, <<) when used with user-defined types (classes). The operator's fundamental meaning for built-in types remains unchanged.

  2. Purpose: To make user-defined types behave like built-in types, enabling intuitive syntax (e.g., obj3 = obj1 + obj2;).

  3. Implementation: Done via special functions called operator functions, which can be member functions or friend functions.

7.2 Syntax for Member Function

7.3 Example: Overloading + Operator

Commonly Overloaded Operators: Arithmetic (+, -, *, /), relational (==, !=, <, >), assignment (=), stream insertion/extraction (<<, >>).


8. Basic Concepts of Inheritance and Polymorphism

8.1 Inheritance: "is-a" Relationship

  1. Concept: A mechanism where a new class (derived/child class) is created from an existing class (base/parent class). The derived class inherits the properties and behaviors of the base class and can add its own.

  2. Purpose: Promotes code reusability and establishes a hierarchical relationship.

  3. Syntax:

    access-specifier can be public, private, or protected. public inheritance is most common.

8.2 Basic Inheritance Example

8.3 Polymorphism: "many forms"

  1. Concept: The ability for objects of different classes related by inheritance to respond differently to the same function call. Enables one interface, multiple implementations.

  2. Types:

    • Compile-time (Static): Achieved through function overloading and operator overloading.

    • Run-time (Dynamic): Achieved through function overriding using virtual functions and base class pointers/references.

  3. Basic Example of Function Overriding:

    Key Point: When a base class pointer points to a derived class object and calls a virtual function, the derived class's version is executed. This is run-time polymorphism.

Conclusion: OOP features transform code from a sequence of procedures into an organized system of interacting objects. Inline functions and overloading improve efficiency and interface clarity. Classes and objects with encapsulation form the core building blocks. Constructors ensure proper initialization. Static members manage class-level data. Operator overloading makes user-defined types intuitive. Inheritance and polymorphism enable hierarchical design and flexible, extensible systems. Together, these features provide the toolkit for designing robust, maintainable, and scalable software systems.

Last updated