---
title: "Vector class addition by object argument"
subject: "Object Oriented Programming"
chapter: 1
key: "semester_3/object_oriented_programming:vector-class-addition"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/vector-class-addition
site: BCT Engineering
---

# Vector class addition by object argument

Object Oriented Programming · Chapter 1: Introduction to Object Oriented Programming

- Asked in: 2069 Chaitra, 5 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/vector-class-addition
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-1

## Question

Using object oriented technique, write a program to create a class vector that reads integer number. Perform vector addition by passing object as argument and returns the object as result. A vector is a class with array as member.

## Short answer

Create a Vector class holding an array and size. The add(Vector) method returns a new Vector with element-wise sum, assuming equal size. For instance, (1,2,3) + (4,5,6) yields (5,7,9).

## Answer

The class `Vector` has an integer array and its size as members. `add()` takes a `Vector` object as an argument and returns a new `Vector` object holding the element-wise sum. Both vectors are assumed to have the same size.

```cpp
#include <iostream>
using namespace std;

class Vector {
    int v[20];
    int n;
public:
    void read() {
        cout << "Enter size: ";
        cin >> n;
        cout << "Enter " << n << " elements: ";
        for (int i = 0; i < n; i++)
            cin >> v[i];
    }
    Vector add(Vector b) {          // object passed and returned
        Vector sum;
        sum.n = n;
        for (int i = 0; i < n; i++)
            sum.v[i] = v[i] + b.v[i];
        return sum;
    }
    void display() {
        cout << "(";
        for (int i = 0; i < n; i++) {
            cout << v[i];
            if (i < n - 1) cout << ", ";
        }
        cout << ")" << endl;
    }
};

int main() {
    Vector a, b, c;
    a.read();
    b.read();            // must have the same size as a
    c = a.add(b);
    cout << "Sum = ";
    c.display();
    return 0;
}
```

**Sample run**

```text
Enter size: 3
Enter 3 elements: 1 2 3
Enter size: 3
Enter 3 elements: 4 5 6
Sum = (5, 7, 9)
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How is the vector sum computed?
   A. Multiply elements
   B. sum.v[i] = v[i] + b.v[i] for each i
   C. Add sizes only
   D. Concatenate arrays
   Answer: B. Element-wise addition. (Key point: Element-wise sum)

2. What must be true of the two vectors?
   A. Different size
   B. Size 20 exactly
   C. Same size
   D. Any, ignored
   Answer: C. The code uses n of calling object. (Key point: Size)

3. What does add() return?
   A. void
   B. An int
   C. The calling object unchanged
   D. A new Vector object holding the sum
   Answer: D. It returns the object sum. (Key point: Return)

4. For (1,2,3) + (4,5,6)?
   A. (5, 7, 9)
   B. (4, 10, 18)
   C. (5, 7, 8)
   D. (1,2,3,4,5,6)
   Answer: A. Add component-wise. (Key point: Result)

## Related in Object Oriented Programming

- [Overload index operator [] for array class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aindex-operator-array-class) — Object Oriented Programming, chapter 5
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6
- [Array class template with max, min, sort](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-array-max-min-sort) — Object Oriented Programming, chapter 9
- [Binary operator member function one argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-member-one-argument) — Object Oriented Programming, chapter 5
- [Default argument and overloading program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-arg-overloading-program) — Object Oriented Programming, chapter 3
- [Exception with argument and specification program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aexception-program-arg-specification) — Object Oriented Programming, chapter 10

## Related in other subjects

- [Create classes from design class diagrams](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aclasses-from-design-class-diagram) — Object Oriented Analysis and Design, chapter 4, asked 2×
- [Object oriented system and core concepts](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aoo-system-concepts-explained) — Object Oriented Analysis and Design, chapter 1
- [Algorithmic vs object-oriented decomposition](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aalgorithmic-vs-oo-decomposition) — Object Oriented Analysis and Design, chapter 1
- [Insert number at position in array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-insert-number-in-array) — Computer Programming, chapter 11
