0% found this document useful (0 votes)
54 views8 pages

Data Conversions

The document contains 3 programming assignments on data type conversions in C++. The first assignment deals with conversions between basic types (like int) and class types. It includes examples of converting an int to a class and vice versa. The second assignment demonstrates the use of the mutable and explicit keywords in C++. It includes an example showing how the mutable keyword allows changing member values in a const member function. It also shows how the explicit keyword prevents implicit conversions for a constructor. The third assignment demonstrates conversion between two user-defined class types by overloading the assignment operator to convert one class to another. It provides an example that sets member values of one class based on values of another class.

Uploaded by

KING AUTHER
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)
54 views8 pages

Data Conversions

The document contains 3 programming assignments on data type conversions in C++. The first assignment deals with conversions between basic types (like int) and class types. It includes examples of converting an int to a class and vice versa. The second assignment demonstrates the use of the mutable and explicit keywords in C++. It includes an example showing how the mutable keyword allows changing member values in a const member function. It also shows how the explicit keyword prevents implicit conversions for a constructor. The third assignment demonstrates conversion between two user-defined class types by overloading the assignment operator to convert one class to another. It provides an example that sets member values of one class based on values of another class.

Uploaded by

KING AUTHER
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/ 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