For these types of conversions, we use casting operator function.
Casting operator function is a function whose syntax is defined as shown below-:
operator data_type() //casting operator function
//body of casting operator function
Characteristics of casting operator function-:
- It should be member of a class.
- It don’t take any argument.
- Return type is not specified.
- Although, the return type isn’t specified, it should have return statement.
The program is shown below.
First, understand and make it sure that you are going to convert class type to basic type.
And, I made it clear the you need a casting operator function.
Casting operator function is declared inside class (read point 1-it should be a member function of a class).
(See the code below just lightly scan through it and come back here again)
And, additionally inside the main function, make sure that you convert the required data by doing fer1=cel where cel is of class type .
Fer1 is of basic type.
Hence, in this way, a class type data can be converted into a basic type data.
the source code of this program is given as below.
using namespace std;
cout<<"\nenter value in celsius\n";
cout<<"\nthe equivalent temperature in fahrenheit is=\n"<<fer1;
enter value in celsius
the equivalent temperature in fahrenheit is=
Note-: I am not giving output in form of pictures instead in text.
Do you like text output or not?
Write a program to concatenate two strings using the concept of binary operator overloading?(if you don’t know about operator overloading read the articles given at the bottom of the post)
1) make a string class….declare char name inside it…make a default and parameterized constructor to take string as
then call an operator overloading function as
strings operator +(strings st)
//copy name to temp.name
//then concatenate using strcat…we can use strcat though we are asked to concatenate using operator overloading..
then, display the name as-:
next, inside the main function, initialize a string for constructor as:
strings s1(“my name”),s2(“xyz”),s3;
Try this code in your CodeBlocks. But, don’t copy and paste the code. I hope you can do this.