1. Classes and Objects

BASICS

A Class is a user-defined data type that holds its own data members and member functions. An Object is an instance of a class.

#include <iostream>
using namespace std;

class Student {
private:
    int id;       // Hidden from outside
public:
    string name;  // Accessible from outside

    // Method to set ID securely
    void setId(int studentId) {
        id = studentId;
    }
    // Method to get ID securely
    int getId() {
        return id;
    }
};

int main() {
    Student s1;
    s1.name = "Rajan";
    s1.setId(101);
    cout << "Name: " << s1.name << ", ID: " << s1.getId() << endl;
    return 0;
}

Access Specifiers:

2. Constructors & Destructors

FREQUENTLY ASKED

A Constructor is a special member function that is automatically executed when an object is created. It has the exact same name as the class and no return type.

A Destructor is executed when the object is destroyed or goes out of scope. It has a tilde (~) before the class name.

class Book {
public:
    // 1. Default Constructor
    Book() {
        cout << "Default Constructor Called" << endl;
    }
    
    // 2. Parameterized Constructor
    Book(string title) {
        cout << "Parameterized Constructor Called for " << title << endl;
    }
    
    // 3. Copy Constructor
    Book(const Book &obj) {
        cout << "Copy Constructor Called" << endl;
    }
    
    // Destructor
    ~Book() {
        cout << "Destructor Called" << endl;
    }
};
Type of Constructor Description
Default Takes no arguments. Initializes objects with standard default values.
Parameterized Takes arguments. Used to initialize objects with specific values at creation.
Copy Initializes an object using another object of the same class.

3. Operator Overloading

HIGH PRIORITY

Operator Overloading allows C++ operators (like +, -, *, ==) to have user-defined meanings on user-defined types (classes).

class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i = 0) {
        real = r;   imag = i;
    }
    
    // Overloading the '+' operator
    Complex operator + (Complex const &obj) {
        Complex res;
        res.real = real + obj.real;
        res.imag = imag + obj.imag;
        return res;
    }
    
    void print() {
        cout << real << " + i" << imag << endl;
    }
};

int main() {
    Complex c1(10, 5), c2(2, 4);
    Complex c3 = c1 + c2; // Calls the overloaded + operator
    c3.print();           // Output: 12 + i9
    return 0;
}

Operators that CANNOT be overloaded:

  • Scope resolution operator ::
  • Sizeof operator sizeof
  • Member selector .
  • Member pointer selector .*
  • Ternary operator ?:

4. UML Interaction Diagrams

Interaction diagrams show how objects interact with each other to perform a specific behavior. The two main types are Sequence Diagrams and Collaboration (Communication) Diagrams.

Sequence Diagram

A Sequence Diagram emphasizes the time ordering of messages. It shows objects as lifelines (vertical dashed lines) and the messages exchanged between them over time.

Actor          ATM Machine          Bank Server
  |                 |                    |
  |---Insert Card-->|                    |
  |                 |----Verify Card---->|
  |                 |<----Card OK--------|
  |<--Prompt PIN----|                    |
  |---Enter PIN---->|                    |
  |                 |----Verify PIN----->|
  |                 |<----PIN OK---------|
  |                 |                    |

Collaboration Diagram (Communication Diagram)

A Collaboration Diagram emphasizes the structural organization of the objects that send and receive messages. Sequence numbers are used to determine the order of messages.

                       1: insertCard()
                       4: enterPin()
                       8: withdrawCash()
  +---------------+------------------------>+---------------+
  |  :Customer    |                         |     :ATM      |
  +---------------+<------------------------+---------------+
                       5: promptPin()               | ^
                                                    | | 2: verifyCard()
                                                    | | 3: cardOk()
                                                    | | 6: verifyPin()
                                                    | | 7: pinOk()
                                                    v |
                                            +---------------+
                                            | :Bank Server  |
                                            +---------------+

5. Difference between Sequence and Collaboration Diagram

Sequence Diagram Collaboration Diagram
Emphasizes the time sequence of messages. Emphasizes the structural relationship among objects.
Objects are arranged horizontally; time flows vertically. Objects are arranged freely as a network/graph.
Uses lifelines and activation boxes. Does not use lifelines; uses links and sequence numbers (1, 1.1, 2).
Better for visualizing the flow of control over time. Better for visualizing all the effects on a specific object and object connections.

6. Draw UML Interaction Diagram (Scenarios)

PRACTICAL EXAM

For your exams, you should be prepared to draw Interaction Diagrams (Sequence and Collaboration) for various standard scenarios. Below is a comprehensive example for the ATM Management System.

Example: ATM Management System Interaction

This details the interaction mapping for a standard Cash Withdrawal scenario.

Sequence Diagram (Time-focused)

Customer               ATM Machine            Bank Server
   |                        |                      |
   |--- 1: Insert Card ---->|                      |
   |                        |--- 2: Verify Card -->|
   |                        |<-- 3: Card OK -------|
   |<-- 4: Prompt PIN ------|                      |
   |--- 5: Enter PIN ------>|                      |
   |                        |--- 6: Verify PIN --->|
   |                        |<-- 7: PIN OK --------|
   |<-- 8: Show Options ----|                      |
   |--- 9: Withdraw Cash -->|                      |
   |                        |--- 10: Check Funds ->|
   |                        |<-- 11: Sufficient ---|
   |<-- 12: Dispense Cash --|                      |
   |                        |                      |

Collaboration / Communication Diagram (Structure-focused)

                      1: Insert Card
                      5: Enter PIN
                      9: Withdraw Cash
 +--------------+ -------------------------> +--------------+
 |   :Customer  |                            |    :ATM      |
 +--------------+ <------------------------- +--------------+
                      4: Prompt PIN                |  ^
                      8: Show Options              |  | 2: Verify Card
                     12: Dispense Cash             |  | 3: Card OK
                                                   |  | 6: Verify PIN
                                                   |  | 7: PIN OK
                                                   |  | 10: Check Funds
                                                   |  | 11: Sufficient
                                                   v  |
                                             +--------------+
                                             | :Bank Server |
                                             +--------------+

Other Most Expected UML Scenarios

  • Library Management System
  • Hospital Management System
  • Online Shopping System
  • Banking / Account Management System

Night Before Exam Tips 🌙

  • Revise constructor types carefully (Default, Parameterized, Copy).
  • Practice writing the syntax for Operator Overloading (e.g., adding two complex numbers).
  • Practice drawing one Sequence Diagram (ensure you include lifelines, activation boxes, and actors).
  • Remember UML symbols for interaction diagrams.
  • Focus on repeated SRM questions like the differences between Sequence and Collaboration diagrams.
← Previous Unit Back to C++ Course Next Unit →