Data Conversions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Aim:- Programming Assignments for Data Conversions

Q1. WAP in C++ to perform data conversion


• Conversion from basic type to class type

#include<iostream>
using namespace std;
class A
{
private:
int x;
int y;
public:
void setdata()
{
cout<<"Enter x:";
cin>>x;
cout<<"Enter y;";
cin>>y;
}
void getdata()
{
cout<<"\nValue of x:"<<x;
cout<<"\nValue of y:"<<y;
}
A()
{
x=100;
y=100;
}
A(int a)
{
x=a+10;
y=a+20;
}
};
int main()
{
int result=100;
A aobj;
aobj=result; //basic to class conversion
aobj.getdata();
return 0;
}

Output:-

• Conversion from class type to basic type


#include<iostream>
using namespace std;
class A
{
private:
int x;
int y;
public:
void setdata()
{
cout<<"Enter x:";
cin>>x;
cout<<"Enter y:";
cin>>y;
}
void getdata()
{
cout<<"\nValue of x:"<<x;
cout<<"\nValue of y:"<<y;
}
A()
{
x=100;
y=100;
}
operator int()
{
int value=0;
value=x+y;
return value;
}
};

int main()
{
int result=100;
A aobj;
aobj.setdata();
result=aobj; //class to basic conversion
cout<<"The value of integer ''result'' after type conversion is: "<<result;
return 0;
}

Output:-

• Conversion from one class type to another class type


#include<iostream>
using namespace std;
class A
{
private:
int x;
int y;
public:
void setdata()
{
cout<<"\nEnter x:";
cin>>x;
cout<<"\nEnter y:";
cin>>y;
}
void getdata()
{
cout<<"\nValue of x:"<<x;
cout<<"\nValue of y:"<<y;
}
A()
{
x=0;
y=0;
}
friend class B;
};
class B
{
private:
int a;
int b;
public:
void setdata()
{
cout<<"\nEnter a:";
cin>>a;
cout<<"\nEnter b:";
cin>>b;
}
void getdata()
{
cout<<"\nValue of a:"<<a;
cout<<"\nValue of b:"<<b;
}
B()
{
a=0;
b=0;
}
operator A()
{
A tobj;
tobj.x=a+b;
tobj.y=a*b;
return tobj;
}
};
int main()
{
A aobj;
B bobj;
cout<<"Enter Details of class A:\n";
aobj.setdata();
aobj.getdata();
cout<<"\nEnter Details of class B:\n";
bobj.setdata();
bobj.getdata();
aobj=bobj;
aobj.getdata();
return 0;
}

Output:-
Q2. WAP in C++ to illustrate the use of explicit and mutable keyword.

a. Mutable:
#include<iostream>
using namespace std;
class Mutable_demo
{
private:
mutable int x;
mutable char c;
public:
void setdata();
void change_datamember() const;
void getdata();
};
void Mutable_demo::setdata()
{
cout<<"Enter an integer value:";
cin>>x;
cout<<"Enter a character value:";
cin>>c;
}
void Mutable_demo::change_datamember() const
{
x=100;
c='A';
}
void Mutable_demo::getdata()
{
cout<<"Changed integer value:"<<x;
cout<<"\nChanged character value:"<<c;
}
int main()
{
Mutable_demo md;
md.setdata();
md.change_datamember();
md.getdata();
return 0;
}
Output:

b. Explicit:
#include <iostream>
using namespace std;

class String
{
public:
explicit String(int n); // allocate n bytes to the String object
String(const char *p); // initializes object with char *p
};

String::String(int n)
{
cout<<"Entered int section";
}

String::String(const char *p)


{
cout<<"Entered char section";
}

int main ()
{

String mystring('A');
String mystring1(88);
String mystring2(5.5);
return 0;
}

Output:-

You might also like