Passing Objects via Pointers (Dependency Injection)

Medium Composition & Aggregation
Solve in Playground →

Problem Statement

Create a `Database` class with a `db_name` string attribute. Create a `Server` class whose constructor requires a `Database*` pointer passed into it. The `Server` constructor should print ‘Server connected to [db_name]’.

Input Format

A single string for the database name.

Output Format

Print the connection message.

Constraints

No spaces in the string.

Sample Input

PostgreSQL

Sample Output

Server connected to PostgreSQL

Explanation

Dependency Injection is a form of aggregation where a class receives its dependencies from the outside via pointers rather than creating them internally.

Starter Code

#include <iostream>
#include <string>

class Database {
public:
    // Write your code here
};

class Server {
public:
    // Write your code here
};

int main() {
    // Inject Database pointer into Server
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used