Type Conversion 2
Type Conversion 2
com/c/ComputerScienceAcademy7
#include<iostream.h>
#include<conio.h>
/*
1. Conversion from built in type to class type.
The constructors can be used for default type conversion from
argument's type to the constructor's class type.
*/
/* class number
{
int a;
public:
void getdata(int x)
{
a = x;
}
void display()
{
cout<<a<<endl;
}
number(){}
number(int x)
{
a = x;
}
};
void main()
{
clrscr();
int n = 15;
cout<<"Int n = "<<n<<endl;
number N1;
N1 = n; //N1 = number(n);
getch();
*/
/*
public:
void getdata(int x)
{
a = x;
}
void display()
{
cout<<a<<endl;
}
operator int()
{
return a;
};
void main()
{
clrscr();
number N1;
N1.getdata(26);
cout<<"Value of object N1 = ";
N1.display();
getch();
*/
/*
C++ allows us to define a overloaded cas ng operator
(also called conversion func on) that could be used to convert
/* a class type data to basic type.
operator int ()
{
/*
*/
class number1
{
int a;
public:
void getdata(int x)
{
a = x;
}
void display()
{
cout<<a<<endl;
}
int getnum()
{
return a;
}
};
class number2
{
int a;
public:
void getdata(int x)
{
a = x;
}
void display()
{
cout<<a<<endl;
}
number2(){}
number2(number1 N1)
{
a = N1.getnum();
}
};
void main()
{
clrscr();
number1 N1;
N1.getdata(25);
cout<<"Value of N1 from number1: ";
N1.display();
number2 N2;
getch();