0% found this document useful (0 votes)
52 views16 pages

Data Conversion SASTRA University First Year C++

First year SASTRA University C++

Uploaded by

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

Data Conversion SASTRA University First Year C++

First year SASTRA University C++

Uploaded by

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

TYPE CONVERSION

Basics:

Example of Type Implicit Conversion: (automatic type conversion)

#include<iostream>
using namespace std;
int main()
{
int x = 10; // integer x
char y = 'a'; // character c

// y implicitly converted to int. ASCII


// value of 'a' is 97
x = x + y;

// x is implicitly converted to float


float z = x + 1.55;

cout<<x<<"\t"<<z;
return 0;
}
107 108.55

Explicit Type Conversion

#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

Expression Evaluates to:

static_cast<int>(7.9) 7

static_cast<int>(3.3) 3

static_cast<double>(25) 25.0

static_cast<double>(5 + 3) =static_cast<double>(8) = 8.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

For the next set of examples,


x=15, y=23, & z=3.75.

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...

Conversion from Basic to Class type

#include <iostream> void show()


using namespace std; {
class data cout<<i<<" "<<f;
{ }
int i; };
float f; int main()
public: {
data(int m) data d=86; //data d(86);
{ d.show();
i=m; return 0;
f=23.55; }
} 86 23.55

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> operator float()


using namespace std; {
return(f);
class data }
{ };
int i; int main()
float f; {
public: int a;
data() float b;
{ data d;
i=12; a=d; // operator int() is executed
f=12.55; b=d; // operator float() is executed
} cout<<"Value of a:"<<a<<endl;
operator int() cout<<"Value of b:"<<b;
{ return 0;
return(i); }
} Value of a:12
Value of b:12.55

#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

Conversion between C-Strings and String Objects

Converting C-String to a string object.


#include<string>
#include<iostream>
using namespace std;
int main()
{
/*Initializing a C-String */
char a[] = "Testing";
cout << "This is C-String content: "<< a << endl;

/* string object s is assigned


thorugh a C string ‘a’ */
string s(a);

cout << "This is string object's content : "<< s << endl;


return 0;
}
This is C-String content: Testing
This is string object's content : Testing
Converting a string object to a C style string(Method 1)
#include<iostream>
#include<string>
using namespace std;
int main()
{
/* string object is initialized */
string s = "Testing";
cout << "This is string object's content: "<< s << endl;

/* Address of first character of string object is


stored to char pointer a */
char *a = &(s[0]);

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 2)


#include<iostream>
#include<string>
using namespace std;
int main()
{
/* string object is initialized */
string s = "Testing";
cout << "This is string object's content: "<< s << endl;

/* The string class provides a method called c_str()


that returns a pointer to a char constant */

const char *a = s.c_str();

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());

cout << char_array;

return 0;
}
Object Oriented Programming
Conversion from One Class Type to another Class Type

(OR)

Conversions Between Objects of Different Classes

#include<iostream> class Hours


using namespace std; {
class Minutes int h;
{ public:
int m; void operator = (Minutes x)
public: {
Minutes() h=x.get()/60;
{ }
m=240; void show()
} {
int get() cout<<"Hours="<<h;
{ }
return(m); };
} int main()
void show() {
{ Minutes mt;
cout<<"Minutes="<<m<<endl; Hours hr;
} hr=mt;
}; mt.show();
hr.show();
return 0;
}
Minutes=240
Hours=4
ALTERNATE METHOD

#include<iostream> class square


using namespace std; {
class rect int side;
{ public:
int l,b; square(int t)
public: {
rect(int d) side=t;
{ }
l=d; operator rect()
b=d; {
} return rect(side);
void display() }
{ };

cout<<"Length:"<<l<<"\n"<<"Breadth:"<<b; int main()


} {
}; square s(100);
rect r=s;
r.display();
return 0;
}
Length:100
Breadth:100
Pitfalls of Operator Overloading and Conversion

Use Similar Meanings

When a, b, and c are objects from user-defined classes, and + is


overloaded, the statement

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 to perform operations that are as similar as


possible to those performed on basic data types. You could overload the +
sign to perform subtraction

Use Similar Syntax

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

Suppose you use both a one-argument constructor and a conversion


operator to perform the same conversion. How will the compiler know
which conversion to use?
Example1

#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:

Implicit conversion (int to obj type) Explicit conversion


#include<iostream> #include<iostream>
using namespace std; using namespace std;
class ABC class ABC
{ {
public: public:
int k; int k;
ABC(int n) explicit ABC(int n)
{ {
k=n; k=n;
cout<<n; cout<<k;
} }
}; };
int main() int main()
{ {
ABC a=10;// ABC a(10) (run) ABC a(10);
return 0; // ABC a=10 (won’t work...blocks
} automatic conversion)
10 return 0;
}
10
#include<iostream>
using namespace std;
class ABC
{
public:
int k;
explicit ABC(int n)
{
k=n;
cout<<k;
}
void operator =(int m)
{
cout<<m;
}
};
int main()
{
ABC a(10); //One parameter constructor
a=10; // Operator overloading
return 0;
}

10
10
mutable keyword

Changing const Object Data Using mutable

#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

You might also like