. Basic type into class type
Aim:
To write a C++ program to convert centimeter to meter using basic to class type.
Algorithm:
Step 1: Create a class as length.
Step 2: Declare the data member as m.
Step 3: Create a default constructor to initialize the data member to zero.
Step 4: Create a constructor with one argument and make the conversion from centimeter
to meter.
Step 5: In the main, create an object for class length.
Step 6: Declare the float variable and get its value.
Step 7: Assign the float variable to the object so that the conversion part is called.
Step 8: Display the result using print() member function.
Program:
#include<iostream.h>
#include<conio.h>
class length
{
float m;
public:
length()
{
m=0.0;
}
length(float a)
{
m=a/100;
}
void print()
{
cout<<"\nTHE METER IS:"<<m;
}
};
void main()
{
clrscr();
length l;
cout<<"ENTER THE CENTIMETER:";
float k;
cin>>k;
l=k;
l.print();
getch();
}
Output:
ENTER THE CENTIMETER:700
THE METER IS:7
Ex.No.12
b.Class type into basic type.
Aim:
To write a C++ program to convert meter to centimeter using class to basic type.
Algorithm:
Step 1: Create a class as length.
Step 2: Declare the data member as m.
Step 3: Declare the member function as
length()
void read()
operator float()
Step 3: Create a default constructor [i.e. length ()] to initialize the data member to zero.
Step 4: read() method is used to get the meter value.
Step 5: operator float() method is used to convert meter to centimeter.
Step 6: In the main, create an object for the class length.
Step 7: Call the read() method.
Step 8: Assign the object to the variable so that the conversion part is called.
Step 9: Display the result.
Program:
#include<iostream.h>
#include<conio.h>
class length
{
float m;
public:
length()
{
m=0.0;
}
void getdata()
{
cout<<”Enter the meter:”;
cin>>m;
}
operator float( )
{
float c;
c=m*100;
return(c);
}
};
void main()
{
clrscr();
length l;
l.getdata();
float f;
f=l;
cout<<”The Centimeter is”<<f;
getch();
}
Output:
Enter the meter:7
The Centimeter is:700
Ex.No.12
c. Class type into class type.
Aim:
To write a C++ program to convert degree to radian using class to class type.
Algorithm:
Step 1: Create a class as degree.
1.1 Declare the data member as d.
1.2 Initialize the d variable to zero using default constructor.
1.3 read() method is used to get the degree value.
1.4 getdegree() method is used to return the degree value.
Step 2: Create a class as radian.
2.1 Declare the data member as r.
2.2 Initialize the r variable to zero using default constructor.
2.3 Specify the constructor with degree as argument to perform the conversion.
2.4 print() method is used to display the radian value.
Step 3: In the main, create objects for the class degree and radian.
Step 4: Assign the object of degree class to the object of radian class to call the
conversion part.
Step 5: Display the value.
Program:
#include<iostream.h>
#include<conio.h>
class degree
{
float d;
public:
degree()
{
d=0.0;
}
void read()
{
cout<<"\n ENTER THE DEGREE:";
cin>>d;
}
float getdegree()
{
return d;
}
};
class radian
{
float r;
public:
radian()
{
r=0.0;
}
radian(degree d)
{
r=d.getdegree()*3.14/180;
}
void print()
{
cout<<"\nTHE RADIAN IS:"<<r;
}
};
void main()
{
clrscr();
radian rr;
degree dd;
dd.read();
rr=dd;
rr.print();
getch();
}
Cutput:
ENTER THE DEGREE:67
THE RADIAN IS:1.168778
0 comments:
Post a Comment