---
title: "Transpose of matrix using OOP"
subject: "Object Oriented Programming"
chapter: 1
key: "semester_3/object_oriented_programming:matrix-transpose-oop"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/matrix-transpose-oop
site: BCT Engineering
---

# Transpose of matrix using OOP

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

- Asked in: 2072 Chaitra, 5 marks
- Video: [how to find transpose a matrix using c++](https://www.youtube.com/watch?v=7a5BNGeip9U) — Kalash Kala
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/matrix-transpose-oop
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-1

## Question

Write a program to find the transpose of given Matrix using the concept of Object Oriented Programming.

## Short answer

Encapsulate the matrix in a class Matrix with read() and display(). The transpose() method creates a new Matrix with swapped rows/cols and assigns t[j][i] = a[i][j], then returns the transposed object.

## Answer

The matrix and the operations on it are placed in a class `Matrix`. The transpose of an *r* × *c* matrix is a *c* × *r* matrix where `t[j][i] = a[i][j]`.

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

class Matrix {
    int a[10][10];
    int r, c;
public:
    void read() {
        cout << "Enter rows and columns: ";
        cin >> r >> c;
        cout << "Enter elements:" << endl;
        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++)
                cin >> a[i][j];
    }
    Matrix transpose() {
        Matrix t;
        t.r = c;                    // rows and columns swap
        t.c = r;
        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++)
                t.a[j][i] = a[i][j];
        return t;
    }
    void display() {
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++)
                cout << a[i][j] << "\t";
            cout << endl;
        }
    }
};

int main() {
    Matrix m, t;
    m.read();
    cout << "Original matrix:" << endl;
    m.display();
    t = m.transpose();
    cout << "Transpose:" << endl;
    t.display();
    return 0;
}
```

**Sample run** (2 × 3 matrix)

```text
Original matrix:
1   2   3
4   5   6
Transpose:
1   4
2   5
3   6
```

## Check yourself

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

1. Which assignment forms the transpose?
   A. t.a[i][j] = a[i][j]
   B. t.a[j][i] = a[i][j]
   C. t.a[j][i] = a[j][i]
   D. t.a[i][i] = a[j][j]
   Answer: B. Rows and columns swap indices. (Key point: Transpose rule)

2. For an r x c matrix, the transpose has dimensions?
   A. r x c
   B. r x r
   C. c x r
   D. c x c
   Answer: C. t.r = c and t.c = r. (Key point: Dimensions)

3. How does transpose() give its result?
   A. Prints directly
   B. Returns an int
   C. Modifies a global array
   D. Creates a new Matrix t and returns it
   Answer: D. It returns an object t of class Matrix. (Key point: Object return)

4. For 1 2 3 / 4 5 6, the transpose's first row is?
   A. 1 4
   B. 1 2 3
   C. 4 5 6
   D. 1 2
   Answer: A. Column 1 of the original, 1 and 4, becomes row 1. (Key point: Output)

## Related in Object Oriented Programming

- [Simple program illustrating OOP concept](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoop-simple-program) — Object Oriented Programming, chapter 1
- [Compare procedural and object oriented programming](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprocedural-vs-oop-compare) — Object Oriented Programming, chapter 1
- [Prime number program: procedural and OOP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprime-procedural-oop) — Object Oriented Programming, chapter 1
- [Advantages of OOP over procedural programming](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoop-vs-procedural-advantages) — Object Oriented Programming, chapter 1, asked 5×
- [How OOP differs from POP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoop-vs-pop-differ) — Object Oriented Programming, chapter 1
- [Concept of class with example](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-concept-example) — Object Oriented Programming, chapter 1

## Related in other subjects

- [Pass array; transpose 3×3 matrix](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-array-transpose-3x3) — Computer Programming, chapter 7
- [Transpose m×n matrix in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-matrix-transpose) — Computer Programming, chapter 11
- [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
