Data Conversion SASTRA University First Year C++
Data Conversion SASTRA University First Year C++
Basics:
#include<iostream>
using namespace std;
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
cout<<x<<"\t"<<z;
return 0;
}
107 108.55
#include<iostream>
using namespace std;
int main()
{
double x = 5.27;
// Explicit conversion from double to int
int sum = (int)x + 7;
cout<<sum;
return 0;
}
12
Explicit Type Conversion using cast operator or type casting
#include<iostream>
using namespace std;
int main()
{
char a='A';
cout<<static_cast<int>(a)+5;
return 0;
}
70
static_cast<int>(7.9) 7
static_cast<int>(3.3) 3
static_cast<double>(25) 25.0
static_cast<double>(15) / 2 =15.0/2
static_cast<double>(15 / 2) =static_cast<double>(7)=7.0
static_cast<int>(7.8 =static_cast<int>(7.8
+ static_cast<double>(15) / 2) +7.5)=static_cast<int>(15.3)=15
static_cast<int>(7.8 =static_cast<int>(7.8
+ static_cast<double>(15 / 2)) +7.0)=static_cast<int>(14.8)=14
static_cast<int>(7.9 + 6.7) 14
static_cast<int>(7.9)
13
+ static_cast<int>(6.7)
static_cast<double>(y / x) + z 4.75
static_cast<double>(y) / x + z 5.28333...
This type of conversion is automatically done by the compiler with the help
of in-built routines or by applying type casting.
In this type, the left hand operand of = sign is always class type and right
hand operand is always basic type.
#include<iostream>
using namespace std;
class Time1
{
int hrs,min;
public:
Time1(int t)
{
cout<<"Basic Type to ==> Class Type Conversion"<<endl;
hrs=t/60;
min=t%60;
}
void display()
{
cout<<hrs<< ": Hours";
cout<<min<< " Minutes";
}
};
int main()
{
int duration;
cout<<"Enter time duration in minutes";
cin>>duration;
Time1 t1=duration; // Time1 t1(duration);
t1.display();
return 0;
}
Enter time duration in minutes
150
Basic Type to ==> Class Type Conversion
2: Hours30 Minutes
Conversion from Class type to Basic data type
#include<iostream>
using namespace std;
class Time1
{
int hrs,min;
public:
Time1(int a,int b)
{
hrs=a;
min=b;
}
operator int()
{
cout<<"Class Type to Basic Type Conversion..."<<endl;
return(hrs*60+min);
}
};
int main()
{
int duration;
Time1 t(2,30); //construct object
duration = t; //duration = (int)t
cout<<"Total Minutes are "<<duration<<endl;
cout<<"Another method"<<endl;
duration = t.operator int();
cout<<"Total Minutes are "<<duration;
return 0;
}
Class Type to Basic Type Conversion...
Total Minutes are 150
Another method
Class Type to Basic Type Conversion...
Total Minutes are 150
cout<<"C_String content:"<<a;
return 0;
}
This is string object's content: Testing
C_String content:Testing
cout<<"C_String content:"<<a;
return 0;
}
This is string object's content: Testing
C_String content:Testing
Converting a string object to a C style string(Method 3)
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main()
{
string s = "Object Oriented Programming";
int n = s.length();
char char_array[n+1];
strcpy(char_array, s.c_str());
return 0;
}
Object Oriented Programming
Conversion from One Class Type to another Class Type
(OR)
a = b + c;
can mean something quite different than it does when a, b, and c are
variables of basic data types.
Use overloaded operators in the same way you use basic types
Show Restraint
If the number of overloaded operators grows too large, then the whole point
of using them is lost, and reading the listing becomes harder instead of
easier
Avoid Ambiguity
#include<iostream>
using namespace std;
class ABC
{
public:
int k;
ABC(int n)
{
cout<<n;
}
void operator =(int m)
{
cout<<m;
}
};
int main()
{
ABC a=10; // Compiler gets confused here
return 0;
}
Example2
#include<iostream>
using namespace std;
class ABC
{
public:
int k;
ABC()
{
cout<<"Default Constructor";
}
ABC(int n)
{
cout<<n;
}
void operator =(int m)
{
cout<<m;
}
};
int main()
{
ABC a;
a=10; // Compiler gets confused here
return 0;
}
Keywords explicit and mutable
explicit keyword:
10
10
mutable keyword
#include<iostream> #include<iostream>
using namespace std; using namespace std;
class sample class sample
{ {
public: public:
int a; mutable int a;
sample() sample()
{ {
a=20; a=20;
} }
void change() const void change() const
{ {
a=a+5; a=a+5;
cout<<a; cout<<a;
} }
}; };
int main() int main()
{ {
const sample s; const sample s;
s.change(); s.change();
return 0; return 0;
} }
error: assignment of member
‘sample::a’ in read-only object 25