2078 3

CSC166 · 2078 · 10 marks

What is meant by type conversion? Define two way of converting one user defined data type (object) to another user defined object? Write a program that converts object of another distance class with data members feet and inch. (Assume 1m = 3.3 feet and 1cm = 0.4 inch)

What is meant by type conversion? Define two way of converting one user defined data type (object) to another user defined object? Write a program that converts object of another distance class with data members feet and inch. (Assume 1m = 3.3 feet and 1cm = 0.4 inch)[10]

Object Oriented ProgrammingChapter 4: Operator OverloadingData ConversionNumerical: type conversion distance class

Type Conversion in C++

Given

  • Source class: Meter with data members meter (m) and centimeter (cm).
  • Destination class: Distance with data members feet and inch.
  • Conversion factors:
    • $1\ \text{m} = 3.3\ \text{feet}$
    • $1\ \text{cm} = 0.4\ \text{inch}$
  • Task: write a program converting an object of one class to a Distance object; explain type conversion and two ways of converting between user-defined types.

No numeric input is missing. (The specific test values like $5\text{ m}, 50\text{ cm}$ are chosen for demonstration.)


Solution

1. What is Type Conversion?

Type conversion is the process of changing a value of one data type into another data type. For built-in types (e.g., int to float), the compiler performs conversions automatically. But for user-defined types (class objects), the compiler does not know the conversion rule, so the programmer must define it explicitly.

Three kinds exist:

  • basic to class type
  • class to basic type
  • class to class type (the focus here)

2. Two Ways of Converting One User-Defined Object to Another

Method 1 - Constructor in the Destination class: A single-argument (conversion) constructor is written in the destination class taking a source object as argument.

class Destination {
public:
    Destination(Source s) { /* extract and assign */ }
};

On Destination d = s; the compiler calls this constructor.

Method 2 - Overloaded Casting (Conversion) Operator in the Source class: A conversion function operator Destination() is written in the source class.

class Source {
public:
    operator Destination() { Destination t; /* fill t */ return t; }
};

On Destination d = s; the compiler calls this operator.

Rule: the conversion routine is placed in the destination class (constructor) or in the source class (operator function), never split.

3. Program (both methods)

#include <iostream>
using namespace std;

class Distance {              // Destination class
    float feet, inch;
public:
    Distance(float f = 0, float i = 0) { feet = f; inch = i; }
    void display() {
        cout << "Feet : " << feet << " ft, Inch : " << inch << " in" << endl;
    }
};

class Meter {                 // Source class
    float meter, centimeter;
public:
    Meter(float m = 0, float cm = 0) { meter = m; centimeter = cm; }

    void display() {
        cout << "Meter: " << meter << " m, cm: " << centimeter << " cm" << endl;
    }

    // Method 2: conversion (casting) operator in source class
    operator Distance() {
        // 1 m = 3.3 feet, 1 cm = 0.4 inch
        return Distance(meter * 3.3f, centimeter * 0.4f);
    }
};

int main() {
    Meter m1(5, 50);           // 5 m, 50 cm
    m1.display();

    Distance d1 = m1;          // calls operator Distance()
    d1.display();

    return 0;
}

(Alternatively, Method 1 uses a Distance(Meter m) constructor with feet = m.getMeter()*3.3; inch = m.getCentimeter()*0.4;.)

4. Verification of Calculation (for 5 m, 50 cm)

$$\text{feet} = 5 \times 3.3 = 16.5\ \text{ft}$$ $$\text{inch} = 50 \times 0.4 = 20.0\ \text{in}$$

Output:

Meter: 5 m, cm: 50 cm
Feet : 16.5 ft, Inch : 20 in

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.