Returning Objects from Functions

Medium Introduction to C++ Classes & Objects
Solve in Playground →

Problem Statement

Define a `Vector2D` class with public integers `x` and `y`. Create a standalone function `Vector2D addVectors(Vector2D v1, Vector2D v2)` that returns a new `Vector2D` representing their sum. Read 4 integers (x1, y1, x2, y2), calculate the sum, and print “Result: x, y”.

Input Format

Four space-separated integers.

Output Format

Print the resulting vector.

Constraints

None

Sample Input

1 2 3 4

Sample Output

Result: 4, 6

Explanation

Functions can construct and return entirely new objects back to the caller.

Starter Code

#include <iostream>

class Vector2D {
public:
    // Define x and y here
};

// Define addVectors() here

int main() {
    // Instantiate vectors, read input, add, and print
    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