2:23 PM
Program to one class type to another class type conversion c++ program dev c++

 

Develop a Program to one class type to another class type conversion using constructor c++ program (developed in dev c++ ).

#include<iostream>
#include<conio.h>
using namespace std;
class avi;
//write source class first
class source
{
float p;
public:
source(float x)
{
p=x;
}
 float get()
 {
return p;
 }

};
class avi
{
float a;
public:
avi(source x)//like base to class we are doing conversion in CONSTRUCTOR but x is of class type so used source in front of it
{
a=x.get()/2;//p is private so cant use direct
}
void show()
{
cout<<"half value:"<<endl<<a;
}
};
main()
{
cout<<"please enter a no."<<endl;
float b;
cin>>b;
source s1(b);
avi a1=s1;//conversion class source to class avi
a1.show();
getch();
}

Output:
 Please enter a no.
10
half value is:
5

Develop a Program to one class type to another class type conversion using operator function c++ program (developed in dev c++ ).

#include<iostream>
#include<conio.h>
using namespace std;
class avi;
//always write desunition class first
class des
{
float h;
public:
des(float x)//value of operator is passing to constructor becaz op function is returning some value which is equal to in des m
{
h=x;
}
void display()
{
cout<<"half of no."<<endl<<h;
}

};
class avi
{
int a;
public:
avi()
{};
avi(int x)
{
a=x;
}
operator des()//like class to base we are converting class to another class type so use destinition class name after operator
{
des b=a/2;
return (b);
}
};
main()
{
avi a1(10);
des m=a1;//like class to base here we assigning one class to another class
m.display();
        getch();
}

Output:

half of no.
5

Views: 148 | Added by: vijoriya | Tags: Program to one class type to anothe | Rating: 0.0/0
Total comments: 0
avatar