## Saturday, October 15, 2011

### C++ Tutorial: Some Lab assignments on data conversions

Questions: Write a program that can convert the Distance (meter, centimeter) to meters measurement in float and vice versa. Make a class distance with two data members, meter and centimeter. You can add function members as per your requirement.
Solution:
#include<iostream>

using std::cout;

using std::cin;

using std::endl;

class distance

{

private:

float meter;

float centimeter;

public:

distance():meter(0),centimeter(0){}

distance(float met) //for basic to userdefined data conversion

{

meter = static_cast<int>(met);

centimeter = static_cast<int>((met-meter)*100);

}

void getdata()

{

cout<<"Enter the value of Meter: ";

cin>>meter;

cout<<"Enter the value of Centimter: ";

cin>>centimeter;

}

operator float() //for user defined to basic data conversion

{

float met;

met = meter+centimeter/100;

return met;

}

void display(void)

{

cout<<"Meter: "<<meter<<"\n"<<" Centimeter: "<<centimeter;

}

};

int main()

{

distance d; //uset defined

float m; //basic

int temp;

cout<<"1. Conversion from (meters, centimeters) to meters "<<endl;

cout<<"2. Conversion from meters to (meters, centimeters) "<<endl;

cout<<"Enter the choice: ";

cin>>temp;

switch(temp)

{

case 1:

d.getdata();

m = d;

cout<<"The Distance in meter is: "<<m;

break;

case 2:

cout<<"Enter the value in Meter: ";

cin>>m;

d = m;

d.display();

break;

default:

cout<<"Enter either 1 or 2"<<endl;

cout<<"You Entered " <<temp;

break;

}

return 0;

}


Questions: Write two classes to store distances in meter-centimeter and feet-inch system respectively. Write conversions functions so that the program can convert objects of both types.

Solution:
#include<iostream>

using namespace std;

class meter_centimeter

{

private:

float meter;

float centimeter;

public:

meter_centimeter():meter(0),centimeter(0){}

meter_centimeter(float met, float cmet)

{

meter = met;

centimeter = cmet;

}

void get_mc()

{

cout<<"Enter Meter: ";

cin>>meter;

cout<<"Enter Centimeter: ";

cin>>centimeter;

}

float return_meter(){return meter;}

float return_centimeter(){return centimeter;}

void display()

{

cout<<"Meter : "<<meter<<endl;

cout<<"Centimeter : "<<centimeter;

}

};

class feet_inch

{

private:

float inch;

float feet;

public:

feet_inch():inch(0.0),feet(0.0){}

void get_fi()

{

cout<<"Enter Feet: ";

cin>>feet;

cout<<"Enter Inch: ";

cin>>inch;

}

feet_inch(meter_centimeter mc)

{

float meters = mc.return_meter() + mc.return_centimeter()/100;

float feets = meters *3.28;

feet = static_cast<int>(meters*3.28);

inch = static_cast<int>((feets - feet)*12);

}

operator meter_centimeter()

{

float feets = feet + inch *12;

float meters = feet/3.28;

float met =  static_cast<int> (meters);

float cmet = static_cast<int>((meters - met)*100);

return meter_centimeter(met, cmet);

}

void display()

{

cout<<"Feet: "<<feet<<endl;

cout<<"Inch: "<<inch;

}

};

int main()

{

int temp;

meter_centimeter mc;

feet_inch fi;

cout<<"1. Conversion from meter-centimeter to feet-inch"<<endl;

cout<<"2. Conversion from feet-inch to meter-centimeter"<<endl;

cin>>temp;

switch(temp)

{

case 1:

mc.get_mc();

fi = mc;

fi.display();

break;

case 2:

fi.get_fi();

mc = fi;

mc.display();

break;

default:

cout<<"Enter only 1 or 2";

break;

}

return 0;

}