0% found this document useful (0 votes)
56 views

Type Conversion

The document discusses type conversions in C++. It covers automatic type conversion by the compiler, conversions between basic types, conversions from basic types to class types using constructors, and conversions from class types to basic types using conversion functions.

Uploaded by

manjeshsingh0245
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Type Conversion

The document discusses type conversions in C++. It covers automatic type conversion by the compiler, conversions between basic types, conversions from basic types to class types using constructors, and conversions from class types to basic types using conversion functions.

Uploaded by

manjeshsingh0245
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Type conversion

Type Conversions
• int m;
float x=3.1415;
m=x;
converting one form of data into another form
Automatic Type Conversion
• Automatic type conversion by the C++ compiler from the type that
doesn’t fit, to the type it wants

• 4 types of situation might arise in the data conversion:


• conversion from basic type to basic type
• Conversion from basic type to class type
• Conversion from class type to basic type
• Conversion from class type to class type(Not in Syllabus)
Basic type to other basic type
int x;
float y=10.00;
x=y;
Basic type to Class type
• It is accomplished by the use of an appropriate constructor with one
parameter.
• This constructor with one parameter should be the member function
of the class and parameters it takes must be of basic data type which
is to be converted.
Example
Class time using construtors
{
int hrs;
public:
time(int t)
{
hrs=t/60;
}
};
void main()
{
time t1(85); output : 1hrs
}
Class type to Basic type
Constructor function does not support this operation
•So C++ allow us to define an overloaded casting
operator that could be used to convert a class type
data to basic type.
•It is also known as conversion function
•The casting operator function should satisfy following
conditions
• It must be a class member
• It must not specify return type
• It must not have any arguments
Class to Basic Type
• Casting opeator is used.
operator float ()
{
return (item* price);
}
main()
{
float a;
a= object;
}
• Syntax:- EXAMPLE
operator typenane() operator double()
{ {
…………//function ……..
statements }
} operator int()
{
………..
}
vector :: operator double()
{
double sum=0;
sum =sum + v;
return (sum);
}
void main()
{
vector v1;
double len=double(v1); OR double len=v1;
}
class Student
{
private:
void main()
int rolln0;
{
float fees; int j;
public: float f;
Student (int a, float m) Student st(5,4200.50);
{
rollno = a; j = st; //operator int() is executed
fees = m;
f = st; //operator float() is executed
}
operator int() cout<<"\nvalue of j: "<<j<<"\n";
{ cout<<"\nvalue of f: "<<f<<"\n";
return(rollno);
} getch();
operator float()
}
{
return(fees);
}
};
What is the return type of the conversion operator?
a) void
b) int
c) float
d) no return type
What is the return type of the conversion operator?
a) void
b) int
c) float
d) no return type
How many parameters does a conversion operator may take?
a) 0
b) 1
c) 2
d) as many as possible
How many parameters does a conversion operator may take?
a) 0
b) 1
c) 2
d) as many as possible
To convert from a user defined class to a basic type, you would most
likely use.
a)Built-in conversion function.
b)b) A one-argument constructor.
c)c) A conversion function that’s a member of the class.
d)d)An overloaded ‘=‘operator.
To convert from a user defined class to a basic type, you would most
likely use.
a)Built-in conversion function.
b)A one-argument constructor.
c)A conversion function that’s a member of the class.
d)An overloaded ‘=‘operator.

You might also like