0% found this document useful (0 votes)
21 views3 pages

Class To Basic-WPS Office

The document explains how to define an overloaded casting operator in C++ to convert class types to basic types and vice versa. It outlines the requirements for the casting operator, such as being a class member and having no return type or arguments. Additionally, it discusses converting between different class types using either a constructor or a conversion function.

Uploaded by

natapnahk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

Class To Basic-WPS Office

The document explains how to define an overloaded casting operator in C++ to convert class types to basic types and vice versa. It outlines the requirements for the casting operator, such as being a class member and having no return type or arguments. Additionally, it discusses converting between different class types using either a constructor or a conversion function.

Uploaded by

natapnahk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Class to Basic Type

The constructor functions do not support conversion from a class to basic type. C++ allows us to

define a overloaded casting operator that convert a class type data to basic type. The general form of

an overloaded casting operator function, also referred to as a conversion function, is:

operator typename ( )

//Program statmerit .

This function converts a class type data to typename. For example, the operator double( ) converts a

class object to type double, in the following conversion function:

vector:: operator double ( )

double sum = 0 ;

for(int I = 0; ioize;

sum = sum + v[i] * v[i ] ; //scalar magnitude

return sqrt(sum);
}

The casting operator should satisfy the following conditions.

 It must be a class member.

 It must not specify a return type.

 It must not have any arguments. Since it is a member function, it is invoked

by the object and therefore, the values used for, Conversion inside the

function belongs to the object that invoked the function. As a result function

does not need an argument.

In the string example discussed earlier, we can convert the object string to char* as follows:

string:: operator char*( )

return (str) ;

One Class to Another Class Type

We have just seen data conversion techniques from a basic to class type and a class to basic type. But

sometimes we would like to convert one class data type to another class type.
Example

Obj1 = Obj2 ; //Obj1 and Obj2 are objects of different classes.

Objl is an object of class one and Obj2 is an object of class two. The class two type data is converted

to class one type data and the converted value is assigned to the Objl. Since the conversion takes

place from class two to class one, two is known as the source and one is known as the destination

class.

Such conversion between objects of different classes can be carried out by either a

constructor or a conversion function. Which form to use, depends upon where we want the type-

conversion function to be located, whether in the source class or in the destination class.

We studied that the casting operator function

Operator typename( )

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy