---
title: "Convert user-defined type to basic type"
subject: "Object Oriented Programming"
chapter: 5
key: "semester_3/object_oriented_programming:user-to-basic-conversion"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/user-to-basic-conversion
site: BCT Engineering
---

# Convert user-defined type to basic type

Object Oriented Programming · Chapter 5: Operator Overloading

- Asked in: 2074 Asoj, 4 marks
- Video: [Convert A User-Defined Class Type To A Primitive Type | C++ Tutorial](https://www.youtube.com/watch?v=zRZLwzoQ1Fo) — Portfolio Courses
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/user-to-basic-conversion
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-5

## Question

How do you convert a user-defined data type to a basic data type?

## Short answer

Define a conversion function (operator type_name()) as a member with no return type and no parameters, returning the basic-type value. It is called automatically on assignment or explicit cast. Example: operator float() in Distance returns feet + inch/12.0.

## Answer

The compiler cannot convert a user-defined type (class object) to a basic type automatically, because it does not know the meaning of the conversion. We tell it how by writing a **conversion function** (casting operator) as a member of the class.

**Syntax:**

```cpp
operator type_name() {
    // code to compute and return a value of type_name
}
```

Rules:
- It must be a member function of the source class.
- It has **no return type** written in the declaration (the type name after `operator` is the return type).
- It takes **no arguments**.
- It is called automatically when an object is assigned to a variable of that basic type, or explicitly with a cast.

**Example** - convert a `Distance` object (feet, inches) to a `float` giving the total in feet:

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

class Distance {
    int feet;
    float inch;
public:
    Distance(int f, float i) { feet = f; inch = i; }
    operator float() {                 // Distance -> float
        return feet + inch / 12.0;
    }
};

int main() {
    Distance d(5, 6);
    float ft = d;                      // calls d.operator float()
    cout << "Distance in feet = " << ft << endl;   // 5.5
    cout << (float)d << endl;          // explicit cast, also 5.5
    return 0;
}
```

**Output:** `Distance in feet = 5.5` then `5.5`.

## Check yourself

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

1. Why must we write a conversion function to convert a class object to a basic type?
   A. Constructors cannot convert types
   B. Basic types are not allowed in classes
   C. Conversion functions are faster
   D. The compiler does not know the meaning of the conversion
   Answer: D. The compiler cannot know how to turn an object into a basic value, so we define it. (Key point: Why needed)

2. Which statement about the conversion function operator type_name() is correct?
   A. It has no return type written and no arguments
   B. It has a return type and one argument
   C. It is written as a friend outside the class
   D. It takes the basic value as argument
   Answer: A. It is a member with no declared return type and no arguments; the type after operator is the return type. (Key point: Syntax rule)

3. In float ft = d; with operator float() defined in Distance, what happens?
   A. A compile error
   B. d.operator float() is called automatically
   C. A constructor of float is called
   D. ft is set to 0
   Answer: B. Assigning an object to a float variable calls the conversion function automatically. (Key point: Invocation)

4. Distance d(5, 6) has feet 5 and inch 6. What does operator float() return?
   A. 56
   B. 11
   C. 5.5
   D. 6.5
   Answer: C. 5 + 6/12 = 5.5 feet. (Key point: Example result)

## Related in Object Oriented Programming

- [Static data member and member function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astatic-member-function) — Object Oriented Programming, chapter 4, asked 3×
- [Time class: +, <= and << overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Atime-operator-overload) — Object Oriented Programming, chapter 5

## Related in other subjects

- [Switch versus user defined function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswitch-vs-user-function) — Computer Programming, chapter 5
- [Significance of user defined functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asignificance-user-defined-functions) — Computer Programming, chapter 6
- [Data structure and basic operations](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adata-structure-basic-operations) — Data Structure and Algorithm, chapter 1
- [Basic data transfer mechanisms in USB](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Ausb-transfer-mechanisms) — Instrumentation II, chapter 3
