/*
Problem Statement:Write a C++ program with a base class convert declares two variables, val1, val2, which hol initial and converted values, respectively.
It also defines the funtions getinit() and getconv(), which return the initial value and converted value.
These elements of convert are fixed and applicable to all derived calsses that will actually perform the conversion, compute(),
is a pure virtual function that must be defined by the classes derived from convert. the specific nature of compute() will be
determined by what type of conversion is taking place.
*/
#include<iostream>
using namespace std;
class convert// ABSTRACT BASE CLASS.//
{
protected:
double val1,val2;
public:
void getinit()
{
cout<<"\n Enter the value to be converted:";
cin>>val1;
}
void getconv()
{
cout<<"\n The value after desired conversion is:"<<val2;
cout<<endl;
}
virtual void compute()=0; //PURE VIRTUAL FUNCTION.//
};
class temp: public convert //DERIVED CLASS 1//
{
protected:
int x;
public:
void compute()//DEFINITION OF PURE VIRTUAL FUNCTION.//
{
cout<<"\n Choose conversion:";
cout<<"\n 1. Kelvin to Celsius.";
cout<<"\n 2. Celsius to Kelvin.";
cout<<"\n 3. Fahrenheit to Celsius.";
cout<<"\n 4. Celsius to Fahrenheit."<<endl;
cin>>x;
switch(x)
{
case 1: getinit();
val2=val1-273;
break;
case 2: getinit();
val2=val1+273;
break;
case 3: getinit();
val2=(val1-32)*5/9;
break;
case 4: getinit();
val2=(val1*1.8)+32;
break;
}
}
};
class length:public convert//DERIVED CLASS 2//
{
protected:
int x;
public:
void compute()//DEFINITION OF PURE VIRTUAL FUNCTION//
{
cout<<"\n Choose Conversion:";
cout<<"\n 1. kilometer to meter.";
cout<<"\n 2. meter to centimeter.";
cout<<"\n 3. kilometer to centimeter.";
cout<<"\n 4. meter to kilometer.";
cout<<"\n 5. centimeter to meter.";
cout<<"\n 6. centimeter to kilometer."<<endl;
cin>>x;
switch(x)
{
case 1:getinit();
val2=val1*1000;
break;
case 2:getinit();
val2=val1*100;
break;
case 3:getinit();
val2=val1*100000;
break;
case 4:getinit();
val2=val1/1000;
case 5:getinit();
val2=val1/100;
break;
case 6:getinit();
val2=val1/100000;
break;
}
}
};
class liter: public convert// DERIVED CLASS 3.//
{
protected:
int x;
public:
void compute()//DEFINITION OF PURE VIRTUAL FUNCTION//
{
cout<<"\n choose conversion:";
cout<<"\n 1. liter to milliliter.";
cout<<"\n 2. milliliter to liter.";
cin>>x;
switch(x)
{
case 1: getinit();
val2=val1*1000;
break;
case 2: getinit();
val2=val1/1000;
break;
}
}
};
int main()
{
temp t;//OBJECT OF DERIVED CLASS 1//
length l;//OBJECT OF DERIVED CLASS 2//
liter i;//OBJECT OF DERIVED CLASS 3//
int y;
char ans;
do{
cout<<"\n Enter your choice:";
cout<<"\n 1. Temperature conversion.";
cout<<"\n 2. Length conversion.";
cout<<"\n 3. Volume conversion.";
cin>>y;
switch(y)
{
case 1: t.compute();
t.getconv();
break;
case 2: l.compute();
l.getconv();
break;
case 3: i.compute();
i.getconv();
break;
}
cout<<"\n Do you want to continue? y/n:";
cin>>ans;
}
while(ans=='y');
return 0;
}
/*Output:
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.1
Choose conversion:
1. Kelvin to Celsius.
2. Celsius to Kelvin.
3. Fahrenheit to Celsius.
4. Celsius to Fahrenheit.
1
Enter the value to be converted:273
The value after desired conversion is:0
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.1
Choose conversion:
1. Kelvin to Celsius.
2. Celsius to Kelvin.
3. Fahrenheit to Celsius.
4. Celsius to Fahrenheit.
2
Enter the value to be converted:0
The value after desired conversion is:273
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.1
Choose conversion:
1. Kelvin to Celsius.
2. Celsius to Kelvin.
3. Fahrenheit to Celsius.
4. Celsius to Fahrenheit.
3
Enter the value to be converted:12
The value after desired conversion is:-11.1111
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.1
Choose conversion:
1. Kelvin to Celsius.
2. Celsius to Kelvin.
3. Fahrenheit to Celsius.
4. Celsius to Fahrenheit.
4
Enter the value to be converted:35
The value after desired conversion is:95
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.2
Choose Conversion:
1. kilometer to meter.
2. meter to centimeter.
3. kilometer to centimeter.
4. meter to kilometer.
5. centimeter to meter.
6. centimeter to kilometer.
1
Enter the value to be converted:10
The value after desired conversion is:10000
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.2
Choose Conversion:
1. kilometer to meter.
2. meter to centimeter.
3. kilometer to centimeter.
4. meter to kilometer.
5. centimeter to meter.
6. centimeter to kilometer.
2
Enter the value to be converted:2
The value after desired conversion is:200
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.2
Choose Conversion:
1. kilometer to meter.
2. meter to centimeter.
3. kilometer to centimeter.
4. meter to kilometer.
5. centimeter to meter.
6. centimeter to kilometer.
3
Enter the value to be converted:1
The value after desired conversion is:100000
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.2
Choose Conversion:
1. kilometer to meter.
2. meter to centimeter.
3. kilometer to centimeter.
4. meter to kilometer.
5. centimeter to meter.
6. centimeter to kilometer.
4
Enter the value to be converted:4
The value after desired conversion is:0.04
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.2
Choose Conversion:
1. kilometer to meter.
2. meter to centimeter.
3. kilometer to centimeter.
4. meter to kilometer.
5. centimeter to meter.
6. centimeter to kilometer.
5
Enter the value to be converted:100
The value after desired conversion is:1
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.2
Choose Conversion:
1. kilometer to meter.
2. meter to centimeter.
3. kilometer to centimeter.
4. meter to kilometer.
5. centimeter to meter.
6. centimeter to kilometer.
6
Enter the value to be converted:10000
The value after desired conversion is:0.1
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.3
choose conversion:
1. liter to milliliter.
2. milliliter to liter.1
Enter the value to be converted:1
The value after desired conversion is:1000
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.3
choose conversion:
1. liter to milliliter.
2. milliliter to liter.2
Enter the value to be converted:1000
The value after desired conversion is:1
Do you want to continue? y/n:n
*/
Problem Statement:Write a C++ program with a base class convert declares two variables, val1, val2, which hol initial and converted values, respectively.
It also defines the funtions getinit() and getconv(), which return the initial value and converted value.
These elements of convert are fixed and applicable to all derived calsses that will actually perform the conversion, compute(),
is a pure virtual function that must be defined by the classes derived from convert. the specific nature of compute() will be
determined by what type of conversion is taking place.
*/
#include<iostream>
using namespace std;
class convert// ABSTRACT BASE CLASS.//
{
protected:
double val1,val2;
public:
void getinit()
{
cout<<"\n Enter the value to be converted:";
cin>>val1;
}
void getconv()
{
cout<<"\n The value after desired conversion is:"<<val2;
cout<<endl;
}
virtual void compute()=0; //PURE VIRTUAL FUNCTION.//
};
class temp: public convert //DERIVED CLASS 1//
{
protected:
int x;
public:
void compute()//DEFINITION OF PURE VIRTUAL FUNCTION.//
{
cout<<"\n Choose conversion:";
cout<<"\n 1. Kelvin to Celsius.";
cout<<"\n 2. Celsius to Kelvin.";
cout<<"\n 3. Fahrenheit to Celsius.";
cout<<"\n 4. Celsius to Fahrenheit."<<endl;
cin>>x;
switch(x)
{
case 1: getinit();
val2=val1-273;
break;
case 2: getinit();
val2=val1+273;
break;
case 3: getinit();
val2=(val1-32)*5/9;
break;
case 4: getinit();
val2=(val1*1.8)+32;
break;
}
}
};
class length:public convert//DERIVED CLASS 2//
{
protected:
int x;
public:
void compute()//DEFINITION OF PURE VIRTUAL FUNCTION//
{
cout<<"\n Choose Conversion:";
cout<<"\n 1. kilometer to meter.";
cout<<"\n 2. meter to centimeter.";
cout<<"\n 3. kilometer to centimeter.";
cout<<"\n 4. meter to kilometer.";
cout<<"\n 5. centimeter to meter.";
cout<<"\n 6. centimeter to kilometer."<<endl;
cin>>x;
switch(x)
{
case 1:getinit();
val2=val1*1000;
break;
case 2:getinit();
val2=val1*100;
break;
case 3:getinit();
val2=val1*100000;
break;
case 4:getinit();
val2=val1/1000;
case 5:getinit();
val2=val1/100;
break;
case 6:getinit();
val2=val1/100000;
break;
}
}
};
class liter: public convert// DERIVED CLASS 3.//
{
protected:
int x;
public:
void compute()//DEFINITION OF PURE VIRTUAL FUNCTION//
{
cout<<"\n choose conversion:";
cout<<"\n 1. liter to milliliter.";
cout<<"\n 2. milliliter to liter.";
cin>>x;
switch(x)
{
case 1: getinit();
val2=val1*1000;
break;
case 2: getinit();
val2=val1/1000;
break;
}
}
};
int main()
{
temp t;//OBJECT OF DERIVED CLASS 1//
length l;//OBJECT OF DERIVED CLASS 2//
liter i;//OBJECT OF DERIVED CLASS 3//
int y;
char ans;
do{
cout<<"\n Enter your choice:";
cout<<"\n 1. Temperature conversion.";
cout<<"\n 2. Length conversion.";
cout<<"\n 3. Volume conversion.";
cin>>y;
switch(y)
{
case 1: t.compute();
t.getconv();
break;
case 2: l.compute();
l.getconv();
break;
case 3: i.compute();
i.getconv();
break;
}
cout<<"\n Do you want to continue? y/n:";
cin>>ans;
}
while(ans=='y');
return 0;
}
/*Output:
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.1
Choose conversion:
1. Kelvin to Celsius.
2. Celsius to Kelvin.
3. Fahrenheit to Celsius.
4. Celsius to Fahrenheit.
1
Enter the value to be converted:273
The value after desired conversion is:0
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.1
Choose conversion:
1. Kelvin to Celsius.
2. Celsius to Kelvin.
3. Fahrenheit to Celsius.
4. Celsius to Fahrenheit.
2
Enter the value to be converted:0
The value after desired conversion is:273
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.1
Choose conversion:
1. Kelvin to Celsius.
2. Celsius to Kelvin.
3. Fahrenheit to Celsius.
4. Celsius to Fahrenheit.
3
Enter the value to be converted:12
The value after desired conversion is:-11.1111
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.1
Choose conversion:
1. Kelvin to Celsius.
2. Celsius to Kelvin.
3. Fahrenheit to Celsius.
4. Celsius to Fahrenheit.
4
Enter the value to be converted:35
The value after desired conversion is:95
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.2
Choose Conversion:
1. kilometer to meter.
2. meter to centimeter.
3. kilometer to centimeter.
4. meter to kilometer.
5. centimeter to meter.
6. centimeter to kilometer.
1
Enter the value to be converted:10
The value after desired conversion is:10000
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.2
Choose Conversion:
1. kilometer to meter.
2. meter to centimeter.
3. kilometer to centimeter.
4. meter to kilometer.
5. centimeter to meter.
6. centimeter to kilometer.
2
Enter the value to be converted:2
The value after desired conversion is:200
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.2
Choose Conversion:
1. kilometer to meter.
2. meter to centimeter.
3. kilometer to centimeter.
4. meter to kilometer.
5. centimeter to meter.
6. centimeter to kilometer.
3
Enter the value to be converted:1
The value after desired conversion is:100000
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.2
Choose Conversion:
1. kilometer to meter.
2. meter to centimeter.
3. kilometer to centimeter.
4. meter to kilometer.
5. centimeter to meter.
6. centimeter to kilometer.
4
Enter the value to be converted:4
The value after desired conversion is:0.04
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.2
Choose Conversion:
1. kilometer to meter.
2. meter to centimeter.
3. kilometer to centimeter.
4. meter to kilometer.
5. centimeter to meter.
6. centimeter to kilometer.
5
Enter the value to be converted:100
The value after desired conversion is:1
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.2
Choose Conversion:
1. kilometer to meter.
2. meter to centimeter.
3. kilometer to centimeter.
4. meter to kilometer.
5. centimeter to meter.
6. centimeter to kilometer.
6
Enter the value to be converted:10000
The value after desired conversion is:0.1
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.3
choose conversion:
1. liter to milliliter.
2. milliliter to liter.1
Enter the value to be converted:1
The value after desired conversion is:1000
Do you want to continue? y/n:y
Enter your choice:
1. Temperature conversion.
2. Length conversion.
3. Volume conversion.3
choose conversion:
1. liter to milliliter.
2. milliliter to liter.2
Enter the value to be converted:1000
The value after desired conversion is:1
Do you want to continue? y/n:n
*/
0 Comments