←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Dynamic Initialization of Objects in C++: Concept & Examples

Study material and explanations for Unit 4

Dynamic Initialization of Objects in C++: Concept & Examples

1. What is Dynamic Initialization of Objects? (In Simple Words)

In simple programs, we often hardcode fixed numbers directly into the constructor (for example: Point p(10, 20);). This is called Compile-Time (Static) Initialization.

However, in real-world software, values are never fixed in code. Values come from users typing at the keyboard (cin), files, or database queries while the program is running!

Dynamic Initialization of Objects means providing initial values to an object’s constructor at runtime (while the program is running) using variables, expressions, or user inputs.

Compile-Time vs. Dynamic Runtime Initialization
Compile-Time (Static) Initialization
Student s1(101, "Aman", 85.0);
Values are hardcoded in the source code. Known before the program even runs.
Dynamic (Runtime) Initialization
cin >> p >> y >> r;
FixedDeposit fd1(p, y, r);
Values are typed in by the user while the program is actually running!

2. Practical Case Study: Bank Fixed Deposit Calculator

A classic example asked in university exams is the Bank Fixed Deposit Calculator.

Different customers enter interest rates in different formats:

  • One customer enters rate as a decimal fraction (e.g., 0.07 for 7%).
  • Another customer enters rate as an integer percentage (e.g., 7 for 7%).

Using constructor overloading, C++ automatically picks the right constructor at runtime depending on what data type the user types in!


3. Complete Working C++ Program

#include <iostream>
using namespace std;

class FixedDeposit {
    private:
        long int principal;
        int years;
        float rate;
        float maturityAmount;

    public:
        // Default Constructor
        FixedDeposit() {
            principal = 0;
            years = 0;
            rate = 0.0f;
            maturityAmount = 0.0f;
        }

        // CONSTRUCTOR 1: When rate is entered as a FLOAT (e.g., 0.075)
        FixedDeposit(long int p, int y, float r) {
            principal = p;
            years = y;
            rate = r;
            maturityAmount = principal;
            for (int i = 1; i <= years; i++) {
                maturityAmount = maturityAmount * (1.0f + rate); // Compound interest
            }
        }

        // CONSTRUCTOR 2: When rate is entered as an INTEGER (e.g., 8 percent)
        FixedDeposit(long int p, int y, int r) {
            principal = p;
            years = y;
            rate = float(r) / 100.0f; // Convert percentage to fraction
            maturityAmount = principal;
            for (int i = 1; i <= years; i++) {
                maturityAmount = maturityAmount * (1.0f + rate);
            }
        }

        void display() const {
            cout << "\n--- Investment Statement ---" << endl;
            cout << "Principal Amount : Rs. " << principal << endl;
            cout << "Time Period      : " << years << " years" << endl;
            cout << "Interest Rate    : " << (rate * 100) << "%" << endl;
            cout << "Maturity Return  : Rs. " << maturityAmount << endl;
        }
};

int main() {
    long int p;
    int y;
    float r_float;
    int r_int;

    cout << "=== Case 1: Decimal Rate Entry ===" << endl;
    cout << "Enter Principal, Years, and Rate (e.g. 10000 3 0.07): ";
    cin >> p >> y >> r_float;
    
    // DYNAMIC INITIALIZATION AT RUNTIME
    FixedDeposit fd1(p, y, r_float); 
    fd1.display();

    cout << "\n=== Case 2: Integer Percentage Entry ===" << endl;
    cout << "Enter Principal, Years, and Rate (e.g. 10000 3 7): ";
    cin >> p >> y >> r_int;

    // DYNAMIC INITIALIZATION AT RUNTIME
    FixedDeposit fd2(p, y, r_int);
    fd2.display();

    return 0;
}

4. Key Advantages of Dynamic Initialization

  1. User Flexibility: The program does not force fixed numbers; it adapts to whatever data the user enters.
  2. Instant Calculations: Complex formulas (like compound interest) execute automatically inside the constructor the moment the object is created.
  3. Guarantees Valid Data: The object is fully calculated and ready to use immediately; zero chance of garbage values.

DAVV Exam Preparation Tips

Frequently Asked 5-Mark Questions:

  1. What is dynamic initialization of objects in C++? How is it different from static initialization?
    Model Answer:
    • Static (Compile-time): Variables are initialized using hardcoded constants in code (Point p(1, 2);).
    • Dynamic (Runtime): Variables are initialized at runtime using keyboard inputs (cin) or runtime expressions (FixedDeposit fd(p, y, r);).
  2. Why is constructor overloading useful with dynamic initialization?
    Model Answer:
    • It allows the program to accept different user input formats (like float rate vs int rate) and automatically bind to the matching constructor at runtime.

Frequently Asked 10-Mark Questions:

  1. Explain the concept of Dynamic Initialization of Objects in C++. Write a complete program modeling a Bank Fixed Deposit system that initializes objects dynamically based on runtime inputs.
    Model Answer:
    • Define dynamic initialization and explain how values are passed during program execution.
    • Explain compound interest formula used in the constructor.
    • Write out the complete C++ FixedDeposit code with 2 overloaded constructors (float and int rates).
    • Show sample terminal run with inputs and calculated maturity outputs.