class to basic conversion in c++

class to basic conversion in c++

The constructors did a fine job in type conversion from a basic to class type. What about the conversion from a class to basic type? The constructor functions do not support this operation. Luckily, C++ programming allows us to define an overloaded casting operator that could be used to convert a class type data tothebasic type. The normal form of an overloaded casting operator function, usually referred to as a conversion function, is:

Operator typename()
{
. . . . .
. . . . . (Function statements)
. . . . .
}

This function coverts a class type data to typename. For example, the operator double() coverts a class object to type double, the operator int() coverts a class type object to type int, and so on.

Consider the following conversion function:

vector : : operator double()
{
double sum = 0;
for(int i=0; i < size; i++)
sum = sum + v[i] * u[i];
return sqrt(sum);
}

This function converts a vector to the corresponding scalar magnitude. Re-call that the magnitude of a vector is given by the square root of the sum of the squares of its components. The operator double() can be used as follows:

double length = double(V1);
Or
double length = V1;

The following conversion statements can be used in a function: Where V1 is the object of type vector. Both statements have exactly the same effect. When the compiler encounters a statement that requires the conversion of a class type to a basic data type, its calls the casting operator function to do this job.

The casting operator function should satisfy the following below conditions:

▪ It must be a class member.
▪ It must not specify a return type.
▪ It must not have any arguments.

Since its a member function, it’s invoked by the object and, therefore, the values used for conversion inside the function belong to the object that invoked the function. This means that the function does’nt need an argument.

In the string example described in the previous section, we can do the conversion from string to char* as follows:

string : : operator char*()
{
return (p);
}

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise