Data Conversions
Data Conversions
Data Conversions
#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:-
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:-
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";
}
int main ()
{
String mystring('A');
String mystring1(88);
String mystring2(5.5);
return 0;
}
Output:-