C++ Program to convert primitive types to objects

Primitive datatypes in C++ are datatypes that are predefined in the language itself; like int, float, double, etc. Objects are instances of a class, and C++ being an object-oriented language, conversion between primitive data types and objects is necessary. A class serves as a data type's architectural plan. Although this doesn't describe any data specifically, it does specify what the class name signifies, i.e., what an object of the class will look like and what operations may be carried out on it.

Conversion between a primitive data type to an object is not defined explicitly in the C++ language compilers, so to convert a primitive data type into an object; the mechanism has to be defined by the programmer. How a primitive type will be translated to a specific object is defined in the constructor of a user-defined class. We take one example to understand this better.

The problem that we are solving is to convert a weight given in grams to kilograms and grams. For that, we have defined a user-defined class weight, that contains two integer members kg and gm. ‘kg’ is the kilogram value of the given weight and the ’gm’ is the weight remaining that is lesser than one kilogram to be converted. The algorithm to solve the problem is given below.


The syntax for the conversion is like below −

class Myclass{
   int classVal;
      classVal = 0;
   MyClass(int val) {
      classVal = val;
int main() {
   Myclass m;
   int val = <integer value>;
   m = val;
   return 0;


  • Take the input weight in an integer variable.
  • Create an object of the class weight.
  • Assign the integer value to the class object;
  • Call the show function of the object.

In the constructor function of the defined class, perform the following:

  • Divide the input value by 1000 to get the kilogram (kg) value as the quotient.
  • Divide the input value by 1000 to get the gram (gm) value as the remainder.

As mentioned earlier, all the conversion mechanisms have to be defined inside a constructor. The constructor has to be parameterized, and the primitive source value has to be passed as an argument to the constructor. The source code of the problem is below.


#include <iostream> using namespace std; //converts weight in grams to kgs and grams class Weight { private: int kg, gm; public: //default constructor Weight() { kg = 0; gm = 0; } //paramaeterized constructor Weight(int ip) { this->kg = ip / 1000; this->gm = ip % 1000; } //shows the output void show() { cout << "The weight is " << this->kg << " kgs and " << this->gm << " grams." << endl; } }; int main() { //weight in grams int ip = 1085; //conversion done here Weight w; w = ip; w.show(); return 0; }


The weight is 1 kgs and 85 grams.

In the example, the input is inside the main function as ‘ip’. There is also an object of class weight ‘w’. We have just assigned the integer value to the class object and an implicit call to the object’s parameterized constructor has been called. The functions defined in the constructor have been performed and at last, the output is displayed by calling the ‘show’ function.


In this example, the conversion from primitive type to user-defined class object is done using the implicit invocation of the constructor. This is fine until the constructor needs more than one primitive value to instantiate the object. For this reason, we have to explicitly invoke the constructor and then pass the primitive values as arguments to the object constructor. The opposite conversion from object to primitive types is different, it needs more complicated procedures to complete.

Updated on: 19-Oct-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started