---
title: "Rectangle to Polar via casting operator"
subject: "Object Oriented Programming"
chapter: 5
key: "semester_3/object_oriented_programming:rectangle-to-polar-cast"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/rectangle-to-polar-cast
site: BCT Engineering
---

# Rectangle to Polar via casting operator

Object Oriented Programming · Chapter 5: Operator Overloading

- Asked in: 2072 Chaitra, 6 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/rectangle-to-polar-cast
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-5

## Question

Write a program that will convert object from a class Rectangle to object of a class Polar using Casting Operator.

## Short answer

Write operator Polar() in Rectangle class that calculates radius = sqrt(x²+y²) and angle = atan2(y,x)*180/π, returning a Polar object. A Rectangle(3,4) converts to radius 5 and angle ~53.13 degrees.

## Answer

This is a conversion between two **different user-defined classes**. It can be done with a **conversion (casting) operator** written in the source class `Rectangle`. The operator function `operator Polar()` returns a `Polar` object, so `p = r;` converts automatically.

Formulae: for a point (x, y), radius r = √(x² + y²) and angle θ = tan⁻¹(y / x) (in degrees here).

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

class Polar {
    float radius, angle;          // angle in degrees
public:
    Polar(float r = 0, float a = 0) { radius = r; angle = a; }
    void show() {
        cout << "Polar: radius = " << radius
             << ", angle = " << angle << " deg" << endl;
    }
};

class Rectangle {
    float x, y;
public:
    Rectangle(float a = 0, float b = 0) { x = a; y = b; }
    void show() { cout << "Rectangle: x = " << x << ", y = " << y << endl; }

    operator Polar() {                         // Rectangle -> Polar
        float r = sqrt(x * x + y * y);
        float a = atan2(y, x) * 180.0 / 3.14159265;
        return Polar(r, a);
    }
};

int main() {
    Rectangle rec(3, 4);
    Polar pol;
    pol = rec;                    // calls rec.operator Polar()
    rec.show();
    pol.show();
    return 0;
}
```

**Output:**

```text
Rectangle: x = 3, y = 4
Polar: radius = 5, angle = 53.1301 deg
```

`Polar` is defined before `Rectangle` because `operator Polar()` needs the complete type. The same conversion could also be done with a one-argument constructor in the destination class `Polar(Rectangle)`.

## Check yourself

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

1. To convert Rectangle to Polar with a casting operator, where is operator Polar() written?
   A. In the destination class Polar
   B. As a global function
   C. In the source class Rectangle
   D. In main()
   Answer: C. A conversion operator is a member of the source class and returns the destination type. (Key point: Where the operator goes)

2. Which formulae convert (x, y) to polar form?
   A. r = x + y, angle = x/y
   B. r = x*y, angle = sin(y)
   C. r = sqrt(x^2 - y^2), angle = x/y
   D. r = sqrt(x^2 + y^2), angle = tan^-1(y/x)
   Answer: D. Radius is the distance from the origin and angle is the arctangent of y/x. (Key point: Formulae)

3. For Rectangle (3, 4), what radius does the program give?
   A. 5
   B. 7
   C. 25
   D. 1
   Answer: A. sqrt(9 + 16) = 5. (Key point: Result)

4. What is the other way to do this conversion?
   A. A one-argument constructor in Polar taking a Rectangle
   B. Overloading + in Rectangle
   C. A virtual function
   D. A destructor
   Answer: A. A constructor in the destination class taking the source object also performs the conversion. (Key point: Alternative method)

## Related in Object Oriented Programming

- [City class with operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acity-class-operator-overload) — Object Oriented Programming, chapter 5
- [Area of triangle and rectangle program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aarea-triangle-rectangle-overload) — Object Oriented Programming, chapter 3
- [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
- [Vector class addition by object argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avector-class-addition) — Object Oriented Programming, chapter 1
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [Polygon abstract class with Area()](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apolygon-abstract-area) — Object Oriented Programming, chapter 7

## Related in other subjects

- [Halve rectangle ABCD about its centre](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Ascale-rectangle-half-size) — Computer Graphics, chapter 3
- [Marina class diagram implementation code](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Amarina-class-diagram-implementation-code) — Object Oriented Analysis and Design, chapter 4
- [Convert case based on first character](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswap-case-first-char-word) — Computer Programming, chapter 7
- [Video rental shop class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Avideo-rental-class-diagram) — Object Oriented Analysis and Design, chapter 2
